fix issues related to union types
This commit is contained in:
parent
76c73977fc
commit
313440646a
|
@ -187,7 +187,7 @@ module ArgParser
|
||||||
#
|
#
|
||||||
# Note: You can override this method to change the way validation errors are handled.
|
# Note: You can override this method to change the way validation errors are handled.
|
||||||
def on_validation_error(key : String, value, errors : Array(String))
|
def on_validation_error(key : String, value, errors : Array(String))
|
||||||
add_validation_error(key, erorrs)
|
add_validation_error(key, errors)
|
||||||
raise ValidationError.new(key, value, errors)
|
raise ValidationError.new(key, value, errors)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -198,9 +198,9 @@ module ArgParser
|
||||||
raise ConversionError.new(key, value, type)
|
raise ConversionError.new(key, value, type)
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_validation_error(key : String, value, errors : Array(String))
|
def add_validation_error(key : String, errors : Array(String))
|
||||||
@errors[key] ||= [] of String
|
@_validation_errors[key] ||= [] of String
|
||||||
@errors[key].concat errors
|
@_validation_errors[key].concat errors
|
||||||
end
|
end
|
||||||
|
|
||||||
# https://en.wikipedia.org/wiki/Quotation_mark#Summary_table
|
# https://en.wikipedia.org/wiki/Quotation_mark#Summary_table
|
||||||
|
|
|
@ -123,3 +123,24 @@ struct UUID
|
||||||
UUID.new(arg)
|
UUID.new(arg)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
struct Nil
|
||||||
|
def self.from_arg(arg : String)
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def Union.from_arg(arg : String)
|
||||||
|
{% begin %}
|
||||||
|
{% for type in T %}
|
||||||
|
begin
|
||||||
|
%val = {{type}}.from_arg(arg)
|
||||||
|
return %val if %val.is_a?({{type}})
|
||||||
|
rescue
|
||||||
|
end
|
||||||
|
|
||||||
|
{% end %}
|
||||||
|
|
||||||
|
raise ArgParser::Error.new("Argument '#{arg}' cannot be converted to any of the union types: {{T}}")
|
||||||
|
{% end %}
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in New Issue