@@ -15,8 +15,9 @@ class ParamConverter
1515 @mutex = Mutex . new
1616 @converters = Hash . new { |h , k | h [ k ] = { } }
1717
18- def initialize ( schema )
18+ def initialize ( schema , convert_structures : true )
1919 @schema = schema
20+ @convert_structures = convert_structures
2021 @opened_files = [ ]
2122 end
2223
@@ -39,67 +40,67 @@ def c(ref, value)
3940 self . class . c ( ref . shape . class , value , self )
4041 end
4142
43+ def shape ( ref , value )
44+ case ref . shape
45+ when ListShape then list ( ref , value )
46+ when MapShape then map ( ref , value )
47+ when StructureShape then structure ( ref , value )
48+ when UnionShape then union ( ref , value )
49+ else c ( ref , value )
50+ end
51+ end
52+
4253 def list ( ref , values )
4354 values = c ( ref , values )
44- if values . is_a? ( Array )
45- values . map { |v | member ( ref . shape . member , v ) }
46- else
47- values
48- end
55+ return values unless values . is_a? ( Array )
56+
57+ values . collect { |v | shape ( ref . shape . member , v ) }
4958 end
5059
5160 def map ( ref , values )
5261 values = c ( ref , values )
53- if values . is_a? ( Hash )
54- values . each . with_object ( { } ) do |( key , value ) , hash |
55- hash [ member ( ref . shape . key , key ) ] = member ( ref . shape . value , value )
56- end
57- else
58- values
59- end
60- end
62+ return values unless values . is_a? ( Hash )
6163
62- def member ( ref , value )
63- case ref . shape
64- when StructureShape then structure ( ref , value )
65- when UnionShape then union ( ref , value )
66- when ListShape then list ( ref , value )
67- when MapShape then map ( ref , value )
68- else c ( ref , value )
64+ values . each . with_object ( { } ) do |( key , value ) , hash |
65+ hash [ shape ( ref . shape . key , key ) ] = shape ( ref . shape . value , value )
6966 end
7067 end
7168
7269 def structure ( ref , values )
7370 values = c ( ref , values )
74- if values . respond_to? ( :each_pair )
75- values . each_pair do |k , v |
76- next if v . nil?
71+ return if values . nil?
7772
78- next unless ref . shape . member? ( k )
73+ type = @convert_structures ? ref . shape . type . new : values
74+ return type unless values . respond_to? ( :each_pair )
7975
80- values [ k ] = member ( ref . shape . member ( k ) , v )
81- end
76+ values . each_pair do |k , v |
77+ next if v . nil?
78+ next unless ref . shape . member? ( k )
79+
80+ type [ k ] = shape ( ref . shape . member ( k ) , v )
8281 end
83- values
82+ type
8483 end
8584
86- def union ( ref , values )
85+ def union ( ref , values ) # rubocop:disable Metrics/AbcSize
8786 values = c ( ref , values )
87+ return if values . nil?
88+
8889 if values . is_a? ( Schema ::Union )
89- member_ref = ref . shape . member_by_type ( values . class )
90- member ( member_ref , values )
90+ name , member_ref = ref . shape . member_by_type ( values . class )
91+ member_type = ref . shape . member_type ( name )
92+ member_type . new ( shape ( member_ref , values . value ) )
9193 else
9294 key , value = values . first
93- values [ key ] = member ( ref . shape . member ( key ) , value )
95+ return { key => shape ( ref . shape . member ( key ) , value ) } unless @convert_structures
96+ return unless ref . shape . member? ( key )
97+
98+ member_type = ref . shape . member_type ( key )
99+ member_type . new ( shape ( ref . shape . member ( key ) , value ) )
94100 end
95- values
96101 end
97102
98103 class << self
99- def convert ( shape , params )
100- new ( shape ) . convert ( params )
101- end
102-
103104 # Registers a new value converter. Converters run in the context
104105 # of a shape and value class.
105106 #
@@ -245,7 +246,6 @@ def each_base_class(shape_class, &)
245246 end
246247
247248 add ( UnionShape , Hash ) { |h , _ | h . dup }
248- add ( UnionShape , Schema ::Union )
249249 end
250250 end
251251end
0 commit comments