~singpolyma/dhall-ruby

bd80dfe9ecd83be8aa937d45a09c3b2d0860c271 — Stephen Paul Weber 2 years ago eef5a51
Self-describe CBOR for Expression#to_binary

CBOR.encode(expression) and Expression#to_cbor remain untagged "standard
Dhall" but the default Expression#to_binary now always tags the output
with self-describe CBOR.  This is supported by the standard for decoding
the output will still be compatible with all compliant implementations,
and it makes the binary format easier to identify in the wild.
5 files changed, 9 insertions(+), 6 deletions(-)

M bin/json-to-dhall
M bin/yaml-to-dhall
M lib/dhall/binary.rb
M lib/dhall/resolve.rb
M test/test_parser.rb
M bin/json-to-dhall => bin/json-to-dhall +1 -1
@@ 5,4 5,4 @@ require "dhall"
require "json"
using Dhall::AsDhall

STDOUT.write(CBOR.encode(JSON.parse(STDIN.read).as_dhall))
STDOUT.write(JSON.parse(STDIN.read).as_dhall.to_binary)

M bin/yaml-to-dhall => bin/yaml-to-dhall +1 -1
@@ 5,4 5,4 @@ require "dhall"
require "yaml"
using Dhall::AsDhall

STDOUT.write(CBOR.encode(YAML.safe_load(STDIN.read, [Symbol]).as_dhall))
STDOUT.write(YAML.safe_load(STDIN.read, [Symbol]).as_dhall.to_binary)

M lib/dhall/binary.rb => lib/dhall/binary.rb +5 -2
@@ 35,10 35,13 @@ module Dhall
				CBOR.encode(as_json)
			end
		end
		alias to_binary to_cbor

		def to_binary
			CBOR.encode(::CBOR::Tagged.new(55799, self))
		end

		def digest(digest: Digest::SHA2.new(256))
			(digest << normalize.to_binary).freeze
			(digest << normalize.to_cbor).freeze
		end

		def cache_key

M lib/dhall/resolve.rb => lib/dhall/resolve.rb +1 -1
@@ 186,7 186,7 @@ module Dhall
					return Dhall.from_binary(file.binread) if file.exist?

					Promise.resolve(nil).then(&block).then do |result|
						file.open("wb") { |fh| fh.write(result.to_binary) }
						file.open("wb") { |fh| fh.write(result.to_cbor) }
						result
					end
				else

M test/test_parser.rb => test/test_parser.rb +1 -1
@@ 21,7 21,7 @@ class TestParser < Minitest::Test
			assert_kind_of(Dhall::Expression, match.value)
			assert_equal(
				(TESTS + "#{test}B.dhallb").binread,
				match.value.to_binary
				match.value.to_cbor
			)
		end
	end