From a0b872dbf8dffe1f203348c999ac723d0e6c43de Mon Sep 17 00:00:00 2001 From: Dylan Hall Date: Thu, 14 Dec 2023 11:24:39 -0500 Subject: [PATCH] review feedback --- lib/inferno/dsl/fhir_resource_validation.rb | 51 ++++++++++++++++--- .../dsl/fhir_resource_validation_spec.rb | 5 +- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/lib/inferno/dsl/fhir_resource_validation.rb b/lib/inferno/dsl/fhir_resource_validation.rb index 24d796925..a23792f0a 100644 --- a/lib/inferno/dsl/fhir_resource_validation.rb +++ b/lib/inferno/dsl/fhir_resource_validation.rb @@ -50,10 +50,13 @@ def url(validator_url = nil) end # Set the IGs that the validator will need to load - # Example: ["hl7.fhir.us.core#4.0.0"] + # @example + # igs "hl7.fhir.us.core#4.0.0" + # @example + # igs("hl7.fhir.us.core#3.1.1", "hl7.fhir.us.core#6.0.0") # @param validator_igs [Array] - def igs(validator_igs = nil) - cli_context.igs(validator_igs) if validator_igs + def igs(*validator_igs) + cli_context(igs: validator_igs) if validator_igs cli_context.igs end @@ -72,11 +75,20 @@ def igs(validator_igs = nil) # end # end # + # @example + # fhir_resource_validator do + # url 'http://example.org/validator' + # cli_context({ + # noExtensibleBindingMessages: true, + # txServer: nil + # }) + # end + # # @param definition [Hash] raw fields to set, optional def cli_context(definition = nil, &) if @cli_context if definition - @cli_context.definition.merge!(definition) + merge_into_cli_context(definition) elsif block_given? @cli_context.instance_eval(&) end @@ -86,6 +98,21 @@ def cli_context(definition = nil, &) @cli_context end + # @private + def merge_into_cli_context(new_def) + @cli_context.definition.merge!(new_def) do |_key, old_val, new_val| + case old_val + when Array + # take the union of the 2 + old_val | new_val + when Hash + old_val.merge(new_val) + else + new_val + end + end + end + # @private def additional_validations @additional_validations ||= [] @@ -302,9 +329,21 @@ def method_missing(method_name, *args) # Interpret any other method as setting a field on cliContext. # Follow the same format as `Validator.url` here: # only set the value if one is provided. + # Array or Hash values will be merged if a value is already present. # args will be an empty array if no value is provided. - definition[method_name] = args[0] unless args.empty? - + unless args.empty? + new_val = args[0] + old_val = definition[method_name] + definition[method_name] = case old_val + when Array + # take the union of the 2 + old_val | new_val + when Hash + old_val.merge(new_val) + else + new_val + end + end definition[method_name] end diff --git a/spec/inferno/dsl/fhir_resource_validation_spec.rb b/spec/inferno/dsl/fhir_resource_validation_spec.rb index e817f3eef..a2691fd65 100644 --- a/spec/inferno/dsl/fhir_resource_validation_spec.rb +++ b/spec/inferno/dsl/fhir_resource_validation_spec.rb @@ -199,11 +199,12 @@ it 'uses the right cli_context when submitting the validation request' do v3 = Inferno::DSL::FHIRResourceValidation::Validator.new do url 'http://example.com' - igs ['hl7.fhir.us.core#1.0.1'] + igs 'hl7.fhir.us.core#1.0.1' cli_context do txServer nil displayWarnings true doNative true + igs ['hl7.fhir.us.core#3.1.1'] end end @@ -212,7 +213,7 @@ sv: '4.0.1', doNative: true, extensions: ['any'], - igs: ['hl7.fhir.us.core#1.0.1'], + igs: ['hl7.fhir.us.core#1.0.1', 'hl7.fhir.us.core#3.1.1'], txServer: nil, displayWarnings: true, profiles: [profile_url]