Skip to content

Commit

Permalink
Merge pull request #286 from riscv-software-src/ref_resolve
Browse files Browse the repository at this point in the history
Add ref() to arch_def
  • Loading branch information
dhower-qc authored Nov 22, 2024
2 parents 5e6c568 + 34f6f87 commit 3f2695d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
51 changes: 51 additions & 0 deletions lib/arch_def.rb
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,57 @@ def data
@arch_def
end

# given a `$ref` target, return the Ruby object
#
# @params uri [String] JSON Reference pointer
# @return [Object] The pointed-to object
def ref(uri)
raise ArgumentError, "JSON Reference must contain one '#'" unless uri.count("#") == 1

file_path, obj_path = uri.split("#")
obj =
case file_path
when /^certificate_class.*/
cert_class_name = File.basename(file_path, ".yaml")
cert_class(cert_class_name)
when /^certificate_model.*/
cert_mode_name = File.basename(file_path, ".yaml")
cert_model(cert_model_name)
when /^csr.*/
csr_name = File.basename(file_path, ".yaml")
csr(csr_name)
when /^ext.*/
ext_name = File.basename(file_path, ".yaml")
extension(ext_name)
when /^inst.*/
inst_name = File.basename(file_path, ".yaml")
instruction(inst_name)
when /^manual.*/
manual_name = File.basename(file_path, ".yaml")
manual(manual_name)
when /^profile_class.*/
profile_class_name = File.basename(file_path, ".yaml")
profile_class(profile_class_name)
when /^profile_release.*/
profile_release_name = File.basename(file_path, ".yaml")
profile_release(profile_release_name)
else
raise "Unhandled ref object: #{file_path}"
end

if obj_path.nil?
obj
else
parts = obj_path.split("/")
parts.each do |part|
raise "Error in $ref. There is no method '#{part}' for a #{obj.class.name}" unless obj.respond_to?(part.to_sym)

obj = obj.send(part)
end
obj
end
end

# @return [Array<Csr>] List of all implemented CSRs
def implemented_csrs
return @implemented_csrs unless @implemented_csrs.nil?
Expand Down
2 changes: 1 addition & 1 deletion lib/arch_obj_models/certificate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def tsc_profile

# @return [CertClass] The certification class that this model belongs to.
def cert_class
cert_class = @arch_def.cert_class(File.basename(@data["class"]['$ref'].split("#")[0], ".yaml"))
cert_class = @arch_def.ref(@data["class"]['$ref'])
raise "No certificate class named '#{@data["class"]}'" if cert_class.nil?

cert_class
Expand Down

0 comments on commit 3f2695d

Please sign in to comment.