typed_data 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/typed_data/converter.rb +15 -11
- data/lib/typed_data/schema/union_type.rb +4 -1
- data/lib/typed_data/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa8bfbb2363bcf13407a8bb1fdeed0099f16a5cda9296dd77489fb2b2d7bb024
|
4
|
+
data.tar.gz: 7c446db80a65cc04d22584f38d53c313092432e71ba64321c22d43967a976f8a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e783163ba498a893cacff761e9d502acf0e524e8f57790cf3500ee8e29b2310133dfacd76f017d47cf81bde4f9f0550d4bc78523e7213e7dbd80adae8864215
|
7
|
+
data.tar.gz: 31376d90ff56ab09b0a004b0d0bc8efaed10a6ec4d89cf8eee7c4fd2532c5f533507b96f29af365d6d413475f75d2be1a206d157ce3b19bc847ab24d2c52a17f
|
data/lib/typed_data/converter.rb
CHANGED
@@ -31,7 +31,7 @@ module TypedData
|
|
31
31
|
when Schema::RecordType
|
32
32
|
converted[key] = convert_record(subtype, value)
|
33
33
|
when Schema::UnionType
|
34
|
-
converted[key] = convert_union(subtype, value)
|
34
|
+
converted[key] = convert_union(subtype, value, as_record_field: true)
|
35
35
|
else
|
36
36
|
converted[key] = subtype.coerce(value, formatter: union_type_key_formatter)
|
37
37
|
end
|
@@ -82,24 +82,28 @@ module TypedData
|
|
82
82
|
end
|
83
83
|
|
84
84
|
# @param type [UnionType]
|
85
|
+
# @param as_record_field [Boolean]
|
85
86
|
# @param map [Object]
|
86
|
-
def convert_union(type, value)
|
87
|
+
def convert_union(type, value, as_record_field: false)
|
87
88
|
subtype = type.find_match(value)
|
88
89
|
case subtype
|
89
90
|
when Schema::ArrayType
|
90
|
-
|
91
|
-
.merge!(union_type_key_formatter.call(subtype.to_s) => convert_array(subtype, value))
|
91
|
+
converted_value = convert_array(subtype, value)
|
92
92
|
when Schema::MapType
|
93
|
-
|
94
|
-
.merge!(union_type_key_formatter.call(subtype.to_s) => convert_map(subtype, value))
|
93
|
+
converted_value = convert_map(subtype, value)
|
95
94
|
when Schema::RecordType
|
96
|
-
|
97
|
-
.merge!(union_type_key_formatter.call(subtype.to_s) => convert_record(subtype, value))
|
95
|
+
converted_value = convert_record(subtype, value)
|
98
96
|
when Schema::UnionType
|
99
|
-
|
100
|
-
.merge!(union_type_key_formatter.call(subtype.to_s) => convert_union(subtype, value))
|
97
|
+
converted_value = convert_union(subtype, value)
|
101
98
|
else
|
102
|
-
type.coerce(value, formatter: union_type_key_formatter)
|
99
|
+
return type.coerce(value, formatter: union_type_key_formatter)
|
100
|
+
end
|
101
|
+
|
102
|
+
if as_record_field
|
103
|
+
converted_value
|
104
|
+
else
|
105
|
+
type.default_value(union_type_key_formatter)
|
106
|
+
.merge!(union_type_key_formatter.call(subtype.to_s) => converted_value)
|
103
107
|
end
|
104
108
|
end
|
105
109
|
end
|
@@ -6,13 +6,16 @@ module TypedData
|
|
6
6
|
# @param types [Array<String>]
|
7
7
|
def initialize(types)
|
8
8
|
@types = types.map(&Schema.method(:build_type))
|
9
|
+
@nullable_primitive = @types.size == 2 && @types.any?(&:primitive?) && @types.any? { |t| t.is_a?(NullType) }
|
9
10
|
end
|
10
11
|
|
11
12
|
def to_s
|
12
|
-
"union_#{@types.map(&:to_s).join("_")}"
|
13
|
+
@nullable_primitive ? @types.first.to_s : "union_#{@types.map(&:to_s).join("_")}"
|
13
14
|
end
|
14
15
|
|
15
16
|
def coerce(value, formatter:)
|
17
|
+
return value if @nullable_primitive
|
18
|
+
|
16
19
|
type = find_match(value)
|
17
20
|
if type.is_a?(NullType)
|
18
21
|
default_value(formatter)
|
data/lib/typed_data/version.rb
CHANGED