typed_data 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a692d748f21fdbfccf38620f4bb2a0845544e4d2b8dd27ac45ae18dd11c7565c
4
- data.tar.gz: 2ed2ade4631169260f48100a5658a4ed4c8f79c25f664ea7ba75700de37cee4c
3
+ metadata.gz: fa8bfbb2363bcf13407a8bb1fdeed0099f16a5cda9296dd77489fb2b2d7bb024
4
+ data.tar.gz: 7c446db80a65cc04d22584f38d53c313092432e71ba64321c22d43967a976f8a
5
5
  SHA512:
6
- metadata.gz: cef89670d16f7d370ca4e5dfe58a4d5e42eb72453e1649488dfccbfd0451602bf55468ab33e3343a274f4a63baddb589cdf82b92e8e7820354cd0489a7670e52
7
- data.tar.gz: c3c1f4a9cfdf5b6833ea92129a981ceaa5e5576b18a62e0044242ffa5f50afd4931f23c1b9f16b1ebebdfd2983bab94d288f2bd3cf2eed75ae0f3f53066f5e75
6
+ metadata.gz: 8e783163ba498a893cacff761e9d502acf0e524e8f57790cf3500ee8e29b2310133dfacd76f017d47cf81bde4f9f0550d4bc78523e7213e7dbd80adae8864215
7
+ data.tar.gz: 31376d90ff56ab09b0a004b0d0bc8efaed10a6ec4d89cf8eee7c4fd2532c5f533507b96f29af365d6d413475f75d2be1a206d157ce3b19bc847ab24d2c52a17f
@@ -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
- type.default_value(union_type_key_formatter)
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
- type.default_value(union_type_key_formatter)
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
- type.default_value(union_type_key_formatter)
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
- type.default_value(union_type_key_formatter)
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)
@@ -1,3 +1,3 @@
1
1
  module TypedData
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typed_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - abicky