From e5560786a4ddbde7b6b1fc5e869bb3ef4e8b9126 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Sun, 7 Apr 2019 13:39:26 -0500 Subject: [PATCH] Allow imports to be either binary or source form --- lib/dhall.rb | 18 ++++++++++++++++-- lib/dhall/ast.rb | 4 ++-- test/test_resolve.rb | 10 ++++------ 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/dhall.rb b/lib/dhall.rb index 613c9ca..c71b1aa 100644 --- a/lib/dhall.rb +++ b/lib/dhall.rb @@ -1,8 +1,22 @@ # frozen_string_literal: true +module Dhall + def self.load_raw(source) + begin + return from_binary(source) if source.encoding == Encoding::BINARY + rescue + # Parsing CBOR failed, so guess this is source text in standard UTF-8 + return load_raw(source.force_encoding("UTF-8")) + end + + Parser.parse(source.encode("UTF-8")).value + end +end + require "dhall/ast" -require "dhall/builtins" -require "dhall/parser" require "dhall/binary" +require "dhall/builtins" require "dhall/normalize" +require "dhall/parser" require "dhall/resolve" +require "dhall/typecheck" diff --git a/lib/dhall/ast.rb b/lib/dhall/ast.rb index cef6e7c..853b6ab 100644 --- a/lib/dhall/ast.rb +++ b/lib/dhall/ast.rb @@ -835,7 +835,7 @@ module Dhall class Text < Expression include(ValueSemantics.for_attributes do - value ::String, coerce: ->(s) { s.encode("utf-8") } + value ::String, coerce: ->(s) { s.encode("UTF-8") } end) def <<(other) @@ -1102,7 +1102,7 @@ module Dhall class Expression def self.call(import_value) - Dhall.from_binary(import_value) + Dhall.load_raw(import_value) end end diff --git a/test/test_resolve.rb b/test/test_resolve.rb index 033b7cc..fa716db 100644 --- a/test/test_resolve.rb +++ b/test/test_resolve.rb @@ -4,9 +4,7 @@ require "base64" require "webmock/minitest" require "minitest/autorun" -require "dhall/resolve" -require "dhall/normalize" -require "dhall/parser" +require "dhall" class TestResolve < Minitest::Test def setup @@ -187,7 +185,7 @@ class TestResolve < Minitest::Test def test_headers stub_request(:get, "http://e.td/t") .with(headers: { "Th" => "tv" }) - .to_return(status: 200, body: "\x00") + .to_return(status: 200, body: "\x00".b) expr = Dhall::Import.new( Dhall::Import::IntegrityCheck.new, @@ -200,7 +198,7 @@ class TestResolve < Minitest::Test def test_ipfs stub_request(:get, "http://localhost:8000/ipfs/TESTCID") - .to_return(status: 200, body: "\x00") + .to_return(status: 200, body: "\x00".b) expr = Dhall::Import.new( Dhall::Import::IntegrityCheck.new, @@ -216,7 +214,7 @@ class TestResolve < Minitest::Test .to_return(status: 500) stub_request(:get, "https://cloudflare-ipfs.com/ipfs/TESTCID") - .to_return(status: 200, body: "\x00") + .to_return(status: 200, body: "_") expr = Dhall::Import.new( Dhall::Import::IntegrityCheck.new, -- 2.45.2