M lib/dhall/as_dhall.rb => lib/dhall/as_dhall.rb +4 -0
@@ 153,6 153,10 @@ module Dhall
ut, tags = AsDhall.union_of(@values.zip(@types))
List.new(elements: @exprs.zip(tags).map do |(expr, tag)|
+ if expr.is_a?(Dhall::Union) && expr.alternatives.empty?
+ expr = expr.is_a?(Dhall::Enum) ? nil : expr.extract
+ end
+
Dhall::Union.from(ut, tag, expr)
end)
end
M lib/dhall/coder.rb => lib/dhall/coder.rb +7 -4
@@ 121,13 121,16 @@ module Dhall
end
end
+ refine Enum do
+ def to_ruby
+ extract == :None ? nil : extract
+ end
+ end
+
refine Union do
def to_ruby
- if !value.nil? && tag.match(/\A\p{Upper}/) &&
- Object.const_defined?(tag)
+ if tag.match(/\A\p{Upper}/) && Object.const_defined?(tag)
yield extract, Object.const_get(tag)
- elsif extract == :None
- nil
else
yield extract
end
M lib/dhall/normalize.rb => lib/dhall/normalize.rb +6 -0
@@ 335,6 335,12 @@ module Dhall
end
end
+ class Enum
+ def normalize
+ with(alternatives: alternatives.normalize)
+ end
+ end
+
class If
def normalize
normalized = super
M test/test_as_dhall.rb => test/test_as_dhall.rb +5 -1
@@ 122,6 122,8 @@ class TestAsDhall < Minitest::Test
"Natural" => Dhall::Builtins[:Natural],
"Text" => Dhall::Builtins[:Text],
"None" => nil,
+ "boop" => nil,
+ "Object" => Dhall::EmptyRecordType.new,
"Bool" => Dhall::Builtins[:Bool],
hash_key => Dhall::RecordType.new(
record: {
@@ 140,6 142,8 @@ class TestAsDhall < Minitest::Test
Dhall::Union.from(union_type, "Natural", Dhall::Natural.new(value: 1)),
Dhall::Union.from(union_type, "Text", Dhall::Text.new(value: "hai")),
Dhall::Union.from(union_type, "None", nil),
+ Dhall::Union.from(union_type, "boop", nil),
+ Dhall::Union.from(union_type, "Object", Dhall::EmptyRecord.new),
Dhall::Union.from(union_type, "Bool", Dhall::Bool.new(value: true)),
Dhall::Union.from(union_type, "Bool", Dhall::Bool.new(value: false)),
Dhall::Union.from(union_type, hash_key, Dhall::Record.new(
@@ 149,7 153,7 @@ class TestAsDhall < Minitest::Test
elements: [Dhall::Natural.new(value: 1)]
))
]),
- [1, "hai", nil, true, false, { a: 1 }, [1]].as_dhall
+ [1, "hai", nil, :boop, Object.new, true, false, { a: 1 }, [1]].as_dhall
)
end
M test/test_coder.rb => test/test_coder.rb +27 -15
@@ 39,15 39,23 @@ class TestCoder < Minitest::Test
)
end
+ class EmptyObject
+ def ==(other)
+ other.is_a?(EmptyObject)
+ end
+ end
+
def test_dump_array_heterogenous
assert_equal(
- "\x86\x04\xF6\x83\x00\x83\t\x82\v\xA4fDoublefDoublegNatural" \
- "gNaturaldNone\xF6dTextdTextgNatural\x82\x0F\x01\x83\t\x82\v\xA4" \
- "fDoublefDoublegNaturalgNaturaldNone\xF6dTextdTextdNone\x83\x00\x83" \
- "\t\x82\v\xA4fDoublefDoublegNaturalgNaturaldNone\xF6dTextdText" \
- "fDouble\xFA?\x80\x00\x00\x83\x00\x83\t\x82\v\xA4fDoublefDoubleg" \
- "NaturalgNaturaldNone\xF6dTextdTextdText\x82\x12ehello".b,
- Dhall::Coder.dump([1, nil, 1.0, "hello"])
+ "\x86\x04\xF6\x83\x00\x83\t\x82\v\xA4gNaturalgNaturaldNone" \
+ "\xF6vTestCoder::EmptyObject\x82\a\xA0dboop\xF6gNatural\x82" \
+ "\x0F\x01\x83\t\x82\v\xA4gNaturalgNaturaldNone" \
+ "\xF6vTestCoder::EmptyObject\x82\a\xA0dboop\xF6dNone" \
+ "\x83\t\x82\v\xA4gNaturalgNaturaldNone\xF6vTestCoder::EmptyObject" \
+ "\x82\a\xA0dboop\xF6dboop\x83\x00\x83\t\x82\v\xA4" \
+ "gNaturalgNaturaldNone\xF6vTestCoder::EmptyObject" \
+ "\x82\a\xA0dboop\xF6vTestCoder::EmptyObject\x82\b\xA0".b,
+ Dhall::Coder.dump([1, nil, :boop, EmptyObject.new])
)
end
@@ 105,15 113,19 @@ class TestCoder < Minitest::Test
end
def test_load_array_heterogenous
+ coder = Dhall::Coder.new(safe: Object) # unsafe coder
+
assert_equal(
- [1, nil, 1.0, "hello"],
- Dhall::Coder.load(
- "\x86\x04\xF6\x83\x00\x83\t\x82\v\xA4fDoublefDoublegNatural" \
- "gNaturaldNone\xF6dTextdTextgNatural\x82\x0F\x01\x83\t\x82\v\xA4" \
- "fDoublefDoublegNaturalgNaturaldNone\xF6dTextdTextdNone\x83\x00\x83" \
- "\t\x82\v\xA4fDoublefDoublegNaturalgNaturaldNone\xF6dTextdText" \
- "fDouble\xFA?\x80\x00\x00\x83\x00\x83\t\x82\v\xA4fDoublefDoubleg" \
- "NaturalgNaturaldNone\xF6dTextdTextdText\x82\x12ehello".b
+ [1, nil, :boop, EmptyObject.new],
+ coder.load(
+ "\x86\x04\xF6\x83\x00\x83\t\x82\v\xA4gNaturalgNaturaldNone" \
+ "\xF6vTestCoder::EmptyObject\x82\a\xA0dboop\xF6gNatural\x82" \
+ "\x0F\x01\x83\t\x82\v\xA4gNaturalgNaturaldNone" \
+ "\xF6vTestCoder::EmptyObject\x82\a\xA0dboop\xF6dNone" \
+ "\x83\t\x82\v\xA4gNaturalgNaturaldNone\xF6vTestCoder::EmptyObject" \
+ "\x82\a\xA0dboop\xF6dboop\x83\x00\x83\t\x82\v\xA4" \
+ "gNaturalgNaturaldNone\xF6vTestCoder::EmptyObject" \
+ "\x82\a\xA0dboop\xF6vTestCoder::EmptyObject\x82\b\xA0".b
)
)
end