~singpolyma/dhall-ruby

d15c1db6a2add0c432220f834049171348a992a3 — Stephen Paul Weber 4 years ago 8f58222
Split IntegrityCheck and NoIntegrityCheck
2 files changed, 43 insertions(+), 26 deletions(-)

M lib/dhall/ast.rb
M test/test_resolve.rb
M lib/dhall/ast.rb => lib/dhall/ast.rb +27 -10
@@ 1189,13 1189,13 @@ module Dhall
	class Import < Expression
		class IntegrityCheck
			include(ValueSemantics.for_attributes do
				protocol Either("sha256", :nocheck)
				protocol "sha256"
				data     Either(::String, nil)
			end)

			class FailureException < StandardError; end

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


@@ 1207,12 1207,10 @@ module Dhall
			end

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

			def check(expr)
				return expr if @protocol == :nocheck

				expr = expr.normalize
				return expr if expr.cache_key == to_s



@@ 1220,7 1218,25 @@ module Dhall
			end

			def as_json
				@protocol == :nocheck ? nil : [@protocol, hexdigest]
				[@protocol, hexdigest]
			end
		end

		class NoIntegrityCheck < IntegrityCheck
			def initialize; end

			def to_s
				""
			end

			def hexdigest; end

			def check(expr)
				expr
			end

			def as_json
				nil
			end
		end



@@ 1541,14 1557,14 @@ module Dhall
		].freeze

		include(ValueSemantics.for_attributes do
			integrity_check IntegrityCheck, default: IntegrityCheck.new
			integrity_check IntegrityCheck, default: NoIntegrityCheck.new
			import_type     Class
			path            Either(*PATH_TYPES)
		end)

		def initialize(integrity_check, import_type, path)
			super(
				integrity_check: integrity_check || IntegrityCheck.new,
				integrity_check: integrity_check || NoIntegrityCheck.new,
				import_type:     import_type,
				path:            path
			)


@@ 1571,10 1587,11 @@ module Dhall
		end

		def cache_key(relative_to)
			if integrity_check.protocol == :nocheck
			key = integrity_check.to_s
			if key.empty?
				real_path(relative_to)
			else
				integrity_check.to_s
				key
			end
		end


M test/test_resolve.rb => test/test_resolve.rb +16 -16
@@ 53,7 53,7 @@ class TestResolve < Minitest::Test

	def test_import_as_text
		expr = Dhall::Import.new(
			Dhall::Import::IntegrityCheck.new,
			Dhall::Import::NoIntegrityCheck.new,
			Dhall::Import::Text,
			Dhall::Import::RelativePath.new("text")
		)


@@ 65,7 65,7 @@ class TestResolve < Minitest::Test
		expr = Dhall::Function.of_arguments(
			Dhall::Variable["Natural"],
			body: Dhall::Import.new(
				Dhall::Import::IntegrityCheck.new,
				Dhall::Import::NoIntegrityCheck.new,
				Dhall::Import::Expression,
				Dhall::Import::RelativePath.new("var")
			)


@@ 78,7 78,7 @@ class TestResolve < Minitest::Test
		expr = Dhall::Function.of_arguments(
			Dhall::Variable["Natural"],
			body: Dhall::Import.new(
				Dhall::Import::IntegrityCheck.new,
				Dhall::Import::NoIntegrityCheck.new,
				Dhall::Import::Expression,
				Dhall::Import::RelativePath.new("import")
			)


@@ 91,7 91,7 @@ class TestResolve < Minitest::Test
		expr = Dhall::Function.of_arguments(
			Dhall::Variable["Natural"],
			body: Dhall::Import.new(
				Dhall::Import::IntegrityCheck.new,
				Dhall::Import::NoIntegrityCheck.new,
				Dhall::Import::Expression,
				Dhall::Import::RelativePath.new("self")
			)


@@ 106,7 106,7 @@ class TestResolve < Minitest::Test
		expr = Dhall::Function.of_arguments(
			Dhall::Variable["Natural"],
			body: Dhall::Import.new(
				Dhall::Import::IntegrityCheck.new,
				Dhall::Import::NoIntegrityCheck.new,
				Dhall::Import::Expression,
				Dhall::Import::RelativePath.new("a")
			)


@@ 119,7 119,7 @@ class TestResolve < Minitest::Test

	def test_two_references_no_loop
		expr = Dhall::Import.new(
			Dhall::Import::IntegrityCheck.new,
			Dhall::Import::NoIntegrityCheck.new,
			Dhall::Import::Expression,
			Dhall::Import::RelativePath.new("2text")
		)


@@ 155,7 155,7 @@ class TestResolve < Minitest::Test

	def test_missing
		expr = Dhall::Import.new(
			Dhall::Import::IntegrityCheck.new,
			Dhall::Import::NoIntegrityCheck.new,
			Dhall::Import::Expression,
			Dhall::Import::MissingImport.new
		)


@@ 180,7 180,7 @@ class TestResolve < Minitest::Test
	def test_fallback_to_expr
		expr = Dhall::Operator::ImportFallback.new(
			lhs: Dhall::Import.new(
				Dhall::Import::IntegrityCheck.new,
				Dhall::Import::NoIntegrityCheck.new,
				Dhall::Import::Expression,
				Dhall::Import::MissingImport.new
			),


@@ 196,12 196,12 @@ class TestResolve < Minitest::Test
	def test_fallback_to_import
		expr = Dhall::Operator::ImportFallback.new(
			lhs: Dhall::Import.new(
				Dhall::Import::IntegrityCheck.new,
				Dhall::Import::NoIntegrityCheck.new,
				Dhall::Import::Expression,
				Dhall::Import::MissingImport.new
			),
			rhs: Dhall::Import.new(
				Dhall::Import::IntegrityCheck.new,
				Dhall::Import::NoIntegrityCheck.new,
				Dhall::Import::Expression,
				Dhall::Import::RelativePath.new("import")
			)


@@ 216,7 216,7 @@ class TestResolve < Minitest::Test
			.to_return(status: 200, body: "\x00".b)

		expr = Dhall::Import.new(
			Dhall::Import::IntegrityCheck.new,
			Dhall::Import::NoIntegrityCheck.new,
			Dhall::Import::Expression,
			Dhall::Import::RelativePath.new("using")
		)


@@ 226,7 226,7 @@ class TestResolve < Minitest::Test

	def test_env_natural
		expr = Dhall::Import.new(
			Dhall::Import::IntegrityCheck.new,
			Dhall::Import::NoIntegrityCheck.new,
			Dhall::Import::Expression,
			Dhall::Import::EnvironmentVariable.new("NAT")
		)


@@ 236,7 236,7 @@ class TestResolve < Minitest::Test

	def test_env_as_text
		expr = Dhall::Import.new(
			Dhall::Import::IntegrityCheck.new,
			Dhall::Import::NoIntegrityCheck.new,
			Dhall::Import::Text,
			Dhall::Import::EnvironmentVariable.new("NAT")
		)


@@ 246,7 246,7 @@ class TestResolve < Minitest::Test

	def test_env_relative
		expr = Dhall::Import.new(
			Dhall::Import::IntegrityCheck.new,
			Dhall::Import::NoIntegrityCheck.new,
			Dhall::Import::Expression,
			Dhall::Import::EnvironmentVariable.new("PATH")
		)


@@ 290,7 290,7 @@ class TestResolve < Minitest::Test
			.to_return(status: 200, body: "\x00".b)

		expr = Dhall::Import.new(
			Dhall::Import::IntegrityCheck.new,
			Dhall::Import::NoIntegrityCheck.new,
			Dhall::Import::Expression,
			Dhall::Import::AbsolutePath.new("ipfs", "TESTCID")
		)


@@ 306,7 306,7 @@ class TestResolve < Minitest::Test
			.to_return(status: 200, body: "_")

		expr = Dhall::Import.new(
			Dhall::Import::IntegrityCheck.new,
			Dhall::Import::NoIntegrityCheck.new,
			Dhall::Import::Expression,
			Dhall::Import::AbsolutePath.new("ipfs", "TESTCID")
		)