def testGetStructureDefinitionUrl_withFhirType_returnsValue(self): """Test get_structure_definition_url functionality on FHIR types.""" boolean = datatypes_pb2.Boolean() boolean_descriptor_proto = self._descriptor_proto_for_descriptor( boolean.DESCRIPTOR) code = datatypes_pb2.Code() code_descriptor_proto = self._descriptor_proto_for_descriptor( code.DESCRIPTOR) patient = patient_pb2.Patient() patient_descriptor_proto = self._descriptor_proto_for_descriptor( patient.DESCRIPTOR) self.assertEqual( annotation_utils.get_structure_definition_url(boolean), _BOOLEAN_STRUCTURE_DEFINITION_URL) self.assertEqual( annotation_utils.get_structure_definition_url(boolean.DESCRIPTOR), _BOOLEAN_STRUCTURE_DEFINITION_URL) self.assertEqual( annotation_utils.get_structure_definition_url(code), _CODE_STRUCTURE_DEFINITION_URL) self.assertEqual( annotation_utils.get_structure_definition_url(patient), _PATIENT_STRUCTURE_DEFINITION_URL) self.assertEqual( annotation_utils.get_structure_definition_url(patient.DESCRIPTOR), _PATIENT_STRUCTURE_DEFINITION_URL) self.assertEqual( annotation_utils.get_structure_definition_url(boolean_descriptor_proto), _BOOLEAN_STRUCTURE_DEFINITION_URL) self.assertEqual( annotation_utils.get_structure_definition_url(code_descriptor_proto), _CODE_STRUCTURE_DEFINITION_URL) self.assertEqual( annotation_utils.get_structure_definition_url(patient_descriptor_proto), _PATIENT_STRUCTURE_DEFINITION_URL)
def add_message_to_extension(msg: message.Message, extension: message.Message) -> None: """Adds the contents of msg to extension. Args: msg: A FHIR profile of Extension, whose contents should be added to the generic extension. extension: The generic Extension to populate. """ if not fhir_types.is_profile_of_extension(msg): raise ValueError(f'Message: {msg.DESCRIPTOR.full_name} is not a valid ' 'FHIR Extension profile.') if not fhir_types.is_extension(extension): raise ValueError( f'Extension: {extension.DESCRIPTOR.full_name} is not a ' 'valid FHIR Extension.') cast(Any, extension).url.value = annotation_utils.get_structure_definition_url( msg.DESCRIPTOR) # Copy over the id field if present if proto_utils.field_is_set(msg, 'id'): proto_utils.copy_common_field(msg, extension, 'id') # Copy the vlaue fields from message into the extension value_fields = [ field for field in msg.DESCRIPTOR.fields if field.name not in NON_VALUE_FIELDS ] if not value_fields: raise ValueError( f'Extension has no value fields: {msg.DESCRIPTOR.name}.') # Add fields to the extension. If there is a single value field, a simple # value assignment will suffice. Otherwise, we need to loop over all fields # and add them as child extensions if (len(value_fields) == 1 and not proto_utils.field_is_repeated(value_fields[0])): value_field = value_fields[0] _verify_field_is_proto_message_type(value_field) if proto_utils.field_is_set(msg, value_field): value = proto_utils.get_value_at_field(msg, value_field) _add_value_to_extension( value, extension, annotation_utils.is_choice_type_field(value_field)) else: # TODO: Invalid FHIR; throw an error here? pass else: # Add child extensions... _add_fields_to_extension(msg, extension)
def is_type( url: str, message_or_descriptor: proto_utils.MessageOrDescriptorBase) -> bool: """Returns True if message_or_descriptor has a structure definition of url. Args: url: The FHIR structure definition URL to compare against. message_or_descriptor: The Message or Descriptor to examine. Returns: True if message_or_descriptor has a structure definition equal to url. """ return (annotation_utils.get_structure_definition_url( message_or_descriptor) == url)
def _print_contained_resource(self, contained_resource: message.Message): """Prints the set fields of the contained resource. If the _FhirJsonFormat is set to ANALYTIC, this method only prints the url. Args: contained_resource: The contained resource to iterate over and print. """ for (_, set_field_value) in contained_resource.ListFields(): if self.json_format == _FhirJsonFormat.ANALYTIC: structure_definition_url = ( annotation_utils.get_structure_definition_url( set_field_value)) self.generator.push(f'"{structure_definition_url}"') else: # print the entire contained resource... self._print(set_field_value)
def get_repeated_from_extensions(extension_list: List[message.Message], repeated_cls: Type[_T]) -> List[_T]: """Extracts matching extensions from extension_list and serializes to protos. Args: extension_list: The list of FHIR extensions to examine. repeated_cls: The type of message to serialize to. Returns: A list of protos of instance repeated_cls representing the extensions within extension_list. """ result = [] if not extension_list: return result # Early-exit url = annotation_utils.get_structure_definition_url( repeated_cls.DESCRIPTOR) for extension in extension_list: if cast(Any, extension).url.value == url: msg = extension_to_message(extension, repeated_cls) result.append(msg) return result