diff --git a/lib/test/test_yaml_loader.rb b/lib/test/test_yaml_loader.rb index d502643f2..ee3be540b 100644 --- a/lib/test/test_yaml_loader.rb +++ b/lib/test/test_yaml_loader.rb @@ -208,70 +208,73 @@ def test_that_double_inherits_doesnt_delete_keys assert_equal({ "key1" => "value1", "key2" => "value2", "key3" => "value3_new", "key4" => "value4", "key5" => "value5", "key6" => "value6_new" }, doc["child"]) end - def test_refs_in_the_same_document - yaml = <<~YAML - $defs: - target1: A string - target2: - a: hash - - obj1: - $ref: "#/$defs/target2" - - obj2: - $ref: "#/$defs/target2" - target2: Should disappear - - obj3: - target2: Should disappear - $ref: "#/$defs/target2" - YAML - - f = Tempfile.new("yml") - f.write(yaml) - f.flush - - doc = YamlLoader.load(f.path) - assert_equal({ "a" => "hash" }, doc["obj1"]) - assert_equal({ "a" => "hash" }, doc["obj2"]) - assert_equal({ "a" => "hash" }, doc["obj3"]) - end - - def test_refs_in_the_different_document - yaml1 = <<~YAML - $defs: - target1: A string - target2: - a: hash - YAML - - f1 = Tempfile.new("yml") - f1.write(yaml1) - f1.flush - f1_path = Pathname.new(f1.path) - - yaml2 = <<~YAML - obj1: - $ref: "#{f1_path.basename}#/$defs/target2" - - obj2: - $ref: "#{f1_path.basename}#/$defs/target2" - target2: Should disappear - - obj3: - target2: Should disappear - $ref: "#{f1_path.basename}#/$defs/target2" - YAML - - f2 = Tempfile.new("yml") - f2.write(yaml2) - f2.flush - - doc = YamlLoader.load(f2.path) - assert_equal({ "a" => "hash" }, doc["obj1"]) - assert_equal({ "a" => "hash" }, doc["obj2"]) - assert_equal({ "a" => "hash" }, doc["obj3"]) - end +# Removed by @dhower-qc in https://github.com/riscv-software-src/riscv-unified-db/commit/7d10d70f122de6263ce7907dee8daeddc0a0af40 +# Waiting for response in https://github.com/riscv-software-src/riscv-unified-db/pull/271 +# +# def test_refs_in_the_same_document +# yaml = <<~YAML +# $defs: +# target1: A string +# target2: +# a: hash +# +# obj1: +# $ref: "#/$defs/target2" +# +# obj2: +# $ref: "#/$defs/target2" +# target2: Should disappear +# +# obj3: +# target2: Should disappear +# $ref: "#/$defs/target2" +# YAML +# +# f = Tempfile.new("yml") +# f.write(yaml) +# f.flush +# +# doc = YamlLoader.load(f.path) +# assert_equal({ "a" => "hash" }, doc["obj1"]) +# assert_equal({ "a" => "hash" }, doc["obj2"]) +# assert_equal({ "a" => "hash" }, doc["obj3"]) +# end +# +# def test_refs_in_the_different_document +# yaml1 = <<~YAML +# $defs: +# target1: A string +# target2: +# a: hash +# YAML +# +# f1 = Tempfile.new("yml") +# f1.write(yaml1) +# f1.flush +# f1_path = Pathname.new(f1.path) +# +# yaml2 = <<~YAML +# obj1: +# $ref: "#{f1_path.basename}#/$defs/target2" +# +# obj2: +# $ref: "#{f1_path.basename}#/$defs/target2" +# target2: Should disappear +# +# obj3: +# target2: Should disappear +# $ref: "#{f1_path.basename}#/$defs/target2" +# YAML +# +# f2 = Tempfile.new("yml") +# f2.write(yaml2) +# f2.flush +# +# doc = YamlLoader.load(f2.path) +# assert_equal({ "a" => "hash" }, doc["obj1"]) +# assert_equal({ "a" => "hash" }, doc["obj2"]) +# assert_equal({ "a" => "hash" }, doc["obj3"]) +# end def test_inherits_in_the_same_document yaml = <<~YAML diff --git a/lib/yaml_loader.rb b/lib/yaml_loader.rb index c545d369e..6a20ab155 100644 --- a/lib/yaml_loader.rb +++ b/lib/yaml_loader.rb @@ -63,8 +63,6 @@ def self.expand(filename, obj, yaml_opts = {}) # we handle the inherits key first so that any override will take priority inherits = obj["$inherits"] raise ArgumentError, "Missing reference after $inherits (did you forget to put a relative reference in quotes?)" if inherits.nil? - - # Create array of targets. If provided only one target as a string, convert it to an array of 1 element. inherits_targets = inherits.is_a?(String) ? [inherits] : inherits new_obj = {} @@ -87,7 +85,22 @@ def self.expand(filename, obj, yaml_opts = {}) unless File.exist?(target_filename) raise DereferenceError, "While locating $inherits in #{filename}, #{target_filename} does not exist" end - + + YamlLoader.load(target_filename, yaml_opts) + end + + inherits_target_suffix = inherits_target.split("#/")[1] + inherits_target_path = inherits_target_suffix.split("/") + begin + target_obj = target_obj.dig(*inherits_target_path) + rescue TypeError => e + if e.message == "no implicit conversion of String into Integer" + warn "$inherits: \"#{inherits_target}\" found in file #{filename} references an Array but needs to reference a Hash" + end + raise e + end + + raise DereferenceError, "JSON Path #{inherits_target_suffix} in file #{filename} does not exist in #{relative_path}" if target_obj.nil? raise ArgumentError, "$inherits: \"#{inherits_target}\" in file #{filename} references a #{target_obj.class} but needs to reference a Hash" unless target_obj.is_a?(Hash) target_obj = expand(filename, target_obj, yaml_opts)