def testIsResource_withPatient_returnsTrue(self): """Test is_resource functionality on non-primitive input.""" patient = patient_pb2.Patient() patient_descriptor_proto = self._descriptor_proto_for_descriptor( patient.DESCRIPTOR) self.assertTrue(annotation_utils.is_resource(patient)) self.assertTrue(annotation_utils.is_resource(patient.DESCRIPTOR)) self.assertTrue(annotation_utils.is_resource(patient_descriptor_proto))
def testIsResource_withPrimitives_returnsFalse(self): """Test is_resource functionality on primitive input.""" boolean = datatypes_pb2.Boolean() code = datatypes_pb2.Code() self.assertFalse(annotation_utils.is_resource(boolean)) self.assertFalse(annotation_utils.is_resource(boolean.DESCRIPTOR)) self.assertFalse(annotation_utils.is_resource(code)) self.assertFalse(annotation_utils.is_resource(code.DESCRIPTOR))
def _print_message(self, msg: message.Message) -> None: """Prints the representation of message.""" self.generator.open_json_object() # Add the resource type preamble if necessary set_fields = msg.ListFields() if (annotation_utils.is_resource(msg) and self.json_format == _FhirJsonFormat.PURE): self.generator.add_field('resourceType', f'"{msg.DESCRIPTOR.name}"') if set_fields: self.generator.push(',') self.generator.add_newline() # Print all fields for (i, (set_field, value)) in enumerate(set_fields): if (annotation_utils.is_choice_type_field(set_field) and self.json_format == _FhirJsonFormat.PURE): self._print_choice_field(set_field.json_name, set_field, value) elif annotation_utils.is_primitive_type(set_field.message_type): self._print_primitive_field(set_field.json_name, set_field, value) else: self._print_message_field(set_field.json_name, set_field, value) if i < (len(set_fields) - 1): self.generator.push(',') self.generator.add_newline() self.generator.close_json_object()
def _merge_message(self, json_value: Dict[str, Any], target: message.Message): """Merges the provided json object into the target Message.""" target_descriptor = target.DESCRIPTOR if target_descriptor.name == 'ContainedResource': self._merge_contained_resource(json_value, target) return # For each field/value present in the JSON value, merge into target... field_map = _get_field_map(target_descriptor) for (field_name, value) in json_value.items(): field = field_map.get(field_name) if field is not None: if annotation_utils.is_choice_type_field(field): self._merge_choice_field(value, field, field_name, target) else: self._merge_field(value, field, target) elif field_name == 'resourceType': if (not annotation_utils.is_resource(target_descriptor) or target_descriptor.name != value): raise ValueError( f'Error merging JSON resource into message of type ' f'{target_descriptor.name}.') else: raise ValueError( f'Unable to merge {field_name!r} into {target_descriptor.name}.' )
def _merge_message(self, json_value: Dict[str, Any], target: message.Message) -> None: """Merges the provided json object into the target Message.""" target_descriptor = target.DESCRIPTOR if target_descriptor.name == 'ContainedResource': self._merge_contained_resource(json_value, target) return # For each field/value present in the JSON value, merge into target... field_map = _get_field_map(target_descriptor) for (field_name, value) in json_value.items(): field = field_map.get(field_name) if field is not None: if annotation_utils.is_choice_type_field(field): self._merge_choice_field(value, field, field_name, target) else: self._merge_field(value, field, target) elif field_name == 'resourceType': if (not annotation_utils.is_resource(target_descriptor) or target_descriptor.name != value): raise ValueError( f'Error merging JSON resource into message of type ' f'{target_descriptor.name}.') elif field_name == 'fhir_comments': # `fhir_comments` can exist in a valid FHIR JSON object, however, it is # not supported in FHIR protos. Hence, we simply ignore it. continue else: raise ValueError( f'Unable to merge {field_name!r} into {target_descriptor.name}.' )