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.
|
||||
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)
|
||||
end
|
||||
|
||||
|
@ -198,9 +198,9 @@ module ArgParser
|
|||
raise ConversionError.new(key, value, type)
|
||||
end
|
||||
|
||||
def add_validation_error(key : String, value, errors : Array(String))
|
||||
@errors[key] ||= [] of String
|
||||
@errors[key].concat errors
|
||||
def add_validation_error(key : String, errors : Array(String))
|
||||
@_validation_errors[key] ||= [] of String
|
||||
@_validation_errors[key].concat errors
|
||||
end
|
||||
|
||||
# https://en.wikipedia.org/wiki/Quotation_mark#Summary_table
|
||||
|
|
|
@ -123,3 +123,24 @@ struct UUID
|
|||
UUID.new(arg)
|
||||
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