Skip to content

Commit

Permalink
refactor Base#[] handling default
Browse files Browse the repository at this point in the history
  • Loading branch information
notEthan committed Feb 18, 2020
1 parent 35cf076 commit 8d963c0
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions lib/jsi/base.rb
Expand Up @@ -253,17 +253,6 @@ def [](token)
end
token_schema = token_schema && token_schema.match_to_instance(value)

use_default = false
default = nil
if !token_in_range
if token_schema
if token_schema.respond_to?(:to_hash) && token_schema.key?('default')
default = token_schema['default']
use_default = true
end
end
end

if token_in_range
complex_value = token_schema && (value.respond_to?(:to_hash) || value.respond_to?(:to_ary))
schema_value = token_schema && token_schema.describes_schema?
Expand All @@ -274,10 +263,17 @@ def [](token)
value
end
else
if use_default
defaults = Set.new
if token_schema
if token_schema.respond_to?(:to_hash) && token_schema.key?('default')
defaults << token_schema['default']
end
end

if defaults.size == 1
# use the default value
# we are using #dup so that we get a modified copy of self, in which we set dup[token]=default.
dup.tap { |o| o[token] = default }[token]
dup.tap { |o| o[token] = defaults.first }[token]
else
# I kind of want to just return nil here. the preferred mechanism for
# a JSI's default value should be its schema. but returning nil ignores
Expand Down

0 comments on commit 8d963c0

Please sign in to comment.