~singpolyma/dhall-ruby

bd92c8b10aeb3bca6cdadd8d22b6385256b524f1 — Stephen Paul Weber 5 years ago 2189825
Switch IntegrityCheck encoding to multihash
5 files changed, 29 insertions(+), 19 deletions(-)

M dhall.gemspec
M lib/dhall/ast.rb
M lib/dhall/binary.rb
M lib/dhall/parser.rb
M test/test_resolve.rb
M dhall.gemspec => dhall.gemspec +1 -0
@@ 27,6 27,7 @@ Gem::Specification.new do |spec|

	spec.add_dependency "cbor", "~> 0.5.9.3"
	spec.add_dependency "citrus", "~> 3.0"
	spec.add_dependency "multihashes", "~> 0.1.3"
	spec.add_dependency "promise.rb", "~> 0.7.4"
	spec.add_dependency "value_semantics", "~> 3.0"


M lib/dhall/ast.rb => lib/dhall/ast.rb +5 -11
@@ 1,5 1,6 @@
# frozen_string_literal: true

require "multihashes"
require "uri"
require "value_semantics"



@@ 1189,25 1190,18 @@ module Dhall
	class Import < Expression
		class IntegrityCheck
			include(ValueSemantics.for_attributes do
				protocol "sha256"
				data     Either(::String, nil)
				code   ::Integer
				digest ::String
			end)

			class FailureException < StandardError; end

			def initialize(protocol, data=nil)
				super(
					protocol: protocol,
					data:     data
				)
			end

			def to_s
				"#{@protocol}:#{hexdigest}"
				"#{Multihashes::TABLE[code].sub(/\Asha2-/, "sha")}:#{hexdigest}"
			end

			def hexdigest
				@data.unpack("H*").first.encode(Encoding::UTF_8)
				digest.unpack("H*").first.encode(Encoding::UTF_8)
			end

			def check(expr)

M lib/dhall/binary.rb => lib/dhall/binary.rb +14 -5
@@ 2,6 2,7 @@

require "cbor"
require "digest/sha2"
require "multihashes"

require "dhall/ast"
require "dhall/builtins"


@@ 190,15 191,23 @@ module Dhall
	end

	class Import
		class IntegrityCheck
			def self.decode(integrity_check)
				return unless integrity_check

				IntegrityCheck.new(
					Multihashes.decode(integrity_check).select { |k, _|
						[:code, :digest].include?(k)
					}
				)
			end
		end

		def self.decode(integrity_check, import_type, path_type, *parts)
			parts[0] = Dhall.decode(parts[0]) if path_type < 2 && !parts[0].nil?

			check = if integrity_check
				IntegrityCheck.new(integrity_check[0], [integrity_check[1]].pack("H*"))
			end

			new(
				check,
				IntegrityCheck.decode(integrity_check),
				IMPORT_TYPES[import_type],
				PATH_TYPES[path_type].new(*parts)
			)

M lib/dhall/parser.rb => lib/dhall/parser.rb +6 -1
@@ 525,7 525,12 @@ module Dhall
		module Hash
			def value
				protocol, data = string.split(/:/, 2)
				Dhall::Import::IntegrityCheck.new(protocol, [data].pack("H*"))
				Dhall::Import::IntegrityCheck.new(
					code:   Multihashes::TABLE.key(
						protocol.sub(/\Asha(\d{3})/, "sha2-\\1")
					),
					digest: [data].pack("H*")
				)
			end
		end


M test/test_resolve.rb => test/test_resolve.rb +3 -2
@@ 167,7 167,7 @@ class TestResolve < Minitest::Test

	def test_integrity_check_failure
		expr = Dhall::Import.new(
			Dhall::Import::IntegrityCheck.new("sha256", "badhash"),
			Dhall::Import::IntegrityCheck.new(code: 0x12, digest: "badhash".b),
			Dhall::Import::Expression,
			Dhall::Import::RelativePath.new("var")
		)


@@ 260,7 260,8 @@ class TestResolve < Minitest::Test

		expr = Dhall::Import.new(
			Dhall::Import::IntegrityCheck.new(
				"sha256", Dhall::Variable["_"].digest.digest
				code:   0x12,
				digest: Dhall::Variable["_"].digest.digest
			),
			Dhall::Import::Expression,
			Dhall::Import::Http.new(nil, "example.com", "thing.dhall", nil)