Skip to content

Commit

Permalink
Fixed typo in mip (spurious character after name:) and fixed $copy pa…
Browse files Browse the repository at this point in the history
…th from mie to mip description to accomodate new schema change.
  • Loading branch information
james-ball-qualcomm committed Nov 17, 2024
1 parent 7eb44b7 commit b0f163f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 80 deletions.
2 changes: 1 addition & 1 deletion arch/csr/mie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ address: 0x304
priv_mode: M
length: MXLEN
definedBy: Sm
description: "$copy: mip.yaml#/mip/description"
description: "$copy: mip.yaml#/description"
fields:
SSIE:
location: 1
Expand Down
2 changes: 1 addition & 1 deletion arch/csr/mip.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

$schema: "csr_schema.json#"
kind: csr
name: mip>
name: mip
long_name: Machine Interrupt Pending
address: 0x344
priv_mode: M
Expand Down
68 changes: 0 additions & 68 deletions lib/test/test_yaml_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,74 +208,6 @@ 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

# 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
$defs:
Expand Down
20 changes: 10 additions & 10 deletions lib/yaml_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def self.expand(filename, obj, yaml_opts = {})
obj[key] =
if value.is_a?(String) && value.start_with?("$copy:")
copy_target = value.delete_prefix("$copy:").lstrip
self.get_ref_target_obj(filename, copy_target, yaml_opts)
self.get_copy_target_obj(filename, copy_target, yaml_opts)
else
expand(filename, value, yaml_opts)
end
Expand All @@ -162,35 +162,35 @@ def self.expand(filename, obj, yaml_opts = {})
end

# @param filename [String,Pathname] path to the YAML file
# @param ref_target [String]
# @param copy_target [String]
# @param yaml_opts [Hash] options to pass to YAML.load_file
# @return [Object]
def self.get_ref_target_obj(filename, ref_target, yaml_opts)
relative_path = ref_target.split("#")[0]
def self.get_copy_target_obj(filename, copy_target, yaml_opts)
relative_path = copy_target.split("#")[0]
if relative_path.empty?
# this is a reference in the same document
obj_doc = YAML.load_file(filename, **yaml_opts)
obj_path = ref_target.split("#")[1].split("/")[1..]
obj_path = copy_target.split("#")[1].split("/")[1..]
target_obj = obj_doc.dig(*obj_path)
raise DereferenceError, "$ref: #{obj_path} cannot be found in file #{filename}" if target_obj.nil?
raise DereferenceError, "$copy: #{obj_path} referenced to same file cannot be found in file #{filename}" if target_obj.nil?

ref = expand(filename, target_obj, yaml_opts)
if ref.nil?
raise DereferenceError, "JSON Path #{obj_path} does not exist in file #{filename}"
raise DereferenceError, "$copy: JSON Path #{obj_path} referenced to same file does not exist in file #{filename}"
end

ref
else
target_filename = File.realpath(File.join(filename.dirname, relative_path))

obj_doc = YamlLoader.load(target_filename, yaml_opts)
obj_path = ref_target.split("#")[1].split("/")[1..]
obj_path = copy_target.split("#")[1].split("/")[1..]
target_obj = obj_doc.dig(*obj_path)
raise "$ref: #{obj_path} cannot be found in file #{target_filename}" if target_obj.nil?
raise DereferenceError, "$copy: #{obj_path} referenced from file #{filename} cannot be found in file #{target_filename}" if target_obj.nil?

ref = expand(target_filename, target_obj, yaml_opts)
if ref.nil?
raise DereferenceError, "JSON Path #{obj_path} does not exist in file #{target_filename}"
raise DereferenceError, "$copy: JSON Path #{obj_path} referenced from file #{filename} does not exist in file #{target_filename}"
end

ref
Expand Down

0 comments on commit b0f163f

Please sign in to comment.