From 20640b783cf278432500783550abcdd63b4cfc1b Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Sun, 12 May 2019 19:17:21 -0500 Subject: [PATCH] Decode unions the same way as_dhall makes them --- lib/dhall/coder.rb | 6 ++++-- test/test_coder.rb | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/dhall/coder.rb b/lib/dhall/coder.rb index d11c22c..fba4eec 100644 --- a/lib/dhall/coder.rb +++ b/lib/dhall/coder.rb @@ -129,8 +129,10 @@ module Dhall refine Union do def to_ruby - if tag.match(/\A\p{Upper}/) && Object.const_defined?(tag) - yield extract, Object.const_get(tag) + rtag = tag.sub(/_[0-9a-f]{64}\Z/, "") + if tag.match(/\A\p{Upper}/) && + Object.const_defined?(rtag) && !Dhall.const_defined?(rtag, false) + yield extract, Object.const_get(rtag) else yield extract end diff --git a/test/test_coder.rb b/test/test_coder.rb index a7eba88..7684041 100644 --- a/test/test_coder.rb +++ b/test/test_coder.rb @@ -306,4 +306,28 @@ class TestCoder < Minitest::Test ) ) end + + class SomeTestClass + attr_reader :a, :b + + def initialize(a: 1, b: "hai") + @a = a + @b = b + end + + def ==(other) + a == other.a && b == other.b + end + end + + def test_mixed_array_roundtrip + coder = Dhall::Coder.new(safe: Object) # unsafe coder + + array = [ + 1, "hai", nil, :boop, true, false, { "a" => 1 }, [1], + SomeTestClass.new, SomeTestClass.new(a: "hai", b: 1) + ] + + assert_equal(array, coder.load(coder.dump(array))) + end end -- 2.38.4