From b602097ec36082baa282ea52dd63e8179af1e197 Mon Sep 17 00:00:00 2001 From: Derek Hower Date: Tue, 19 Nov 2024 09:55:27 -0800 Subject: [PATCH] Add ref() to arch_def --- lib/arch_def.rb | 51 ++++++++++++++++++++++++++++++ lib/arch_obj_models/certificate.rb | 2 +- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/arch_def.rb b/lib/arch_def.rb index 5c16477a9..9b5ddb12a 100644 --- a/lib/arch_def.rb +++ b/lib/arch_def.rb @@ -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] List of all implemented CSRs def implemented_csrs return @implemented_csrs unless @implemented_csrs.nil? diff --git a/lib/arch_obj_models/certificate.rb b/lib/arch_obj_models/certificate.rb index 865ae8fa8..5b8f0552a 100644 --- a/lib/arch_obj_models/certificate.rb +++ b/lib/arch_obj_models/certificate.rb @@ -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