17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/apiwork/contract/object/transformer.rb', line 17
def transform(params)
return params unless params.is_a?(Hash)
transformed = params.dup
@shape.params.each do |name, param_options|
next unless transformed.key?(name)
value = transformed[name]
if param_options[:as]
transformed[param_options[:as]] = transformed.delete(name)
name = param_options[:as]
value = transformed[name]
end
if param_options[:shape] && value.is_a?(Hash)
transformed[name] = Transformer.transform(param_options[:shape], value)
elsif param_options[:type] == :record && value.is_a?(Hash)
of = param_options[:of]
of_shape = of&.shape
if of_shape
transformed[name] = value.transform_values do |item|
item.is_a?(Hash) ? Transformer.transform(of_shape, item) : item
end
end
elsif param_options[:type] == :array && value.is_a?(Array)
of = param_options[:of]
of_shape = of&.shape
if of&.type == :union
transformed[name] = value
elsif of_shape
transformed[name] = value.map do |item|
item.is_a?(Hash) ? Transformer.transform(of_shape, item) : item
end
elsif of && (array_result = transform_custom_type_array(value, param_options))
transformed[name] = array_result
end
end
end
transformed
end
|