/// <summary> /// Parses a bindeable type into either a Coding (code, Coding, Quantity, string, uri) or CodeableConcept /// </summary> /// <param name="instance"></param> /// <param name="codedType"></param> /// <returns>An object, which is either a Coding or CodeableConcept</returns> public static object ParseBindable(this IElementNavigator instance, FHIRDefinedType codedType) { // 'code','Coding','CodeableConcept','Quantity','Extension', 'string', 'uri' if (codedType == FHIRDefinedType.Code) { return(instance.ParsePrimitiveCode()); } else if (codedType == FHIRDefinedType.Coding) { return(instance.ParseCoding()); } else if (codedType == FHIRDefinedType.CodeableConcept) { return(instance.ParseCodeableConcept()); } else if (codedType == FHIRDefinedType.Quantity) { var newCoding = new Coding(); var q = instance.ParseQuantity(); newCoding.Code = q.Unit; newCoding.System = q.System; return(newCoding); } else if (codedType == FHIRDefinedType.String) { return(instance.ParsePrimitiveCode()); } else if (codedType == FHIRDefinedType.Uri) { return(instance.ParsePrimitiveCode()); } else if (codedType == FHIRDefinedType.Extension) { throw new NotSupportedException($"The validator does not support binding to Extension values"); } else { throw new NotSupportedException($"FHIR type '{codedType}' is not bindeable"); } }
/// <summary> /// Parses a bindeable type (code, Coding, CodeableConcept, Quantity, string, uri) into a FHIR coded datatype. /// Extensions will be parsed from the 'value' of the (simple) extension. /// </summary> /// <param name="instance"></param> /// <returns>An Element of a coded type (code, Coding or CodeableConcept) dependin on the instance type, /// or null if no bindable instance data was found</returns> /// <remarks>The instance type is mapped to a codable type as follows: /// 'code' => code /// 'Coding' => Coding /// 'CodeableConcept' => CodeableConcept /// 'Quantity' => Coding /// 'Extension' => depends on value[x] /// 'string' => code /// 'uri' => code /// </remarks> public static Element ParseBindable(this IElementNavigator instance) { var instanceType = ModelInfo.FhirTypeNameToFhirType(instance.Type); switch (instanceType) { case FHIRDefinedType.Code: return(instance.ParsePrimitive <Code>()); case FHIRDefinedType.Coding: return(instance.ParseCoding()); case FHIRDefinedType.CodeableConcept: return(instance.ParseCodeableConcept()); case FHIRDefinedType.Quantity: return(parseQuantity(instance)); case FHIRDefinedType.String: return(new Code(instance.ParsePrimitive <FhirString>()?.Value)); case FHIRDefinedType.Uri: return(new Code(instance.ParsePrimitive <FhirUri>()?.Value)); case FHIRDefinedType.Extension: return(parseExtension(instance)); case null: //HACK: fall through - IElementNav did not provide a type //should not happen, and I have no intention to handle it. default: // Not bindable return(null); } Coding parseQuantity(IElementNavigator nav) { var newCoding = new Coding(); var q = instance.ParseQuantity(); newCoding.Code = q.Unit; newCoding.System = q.System ?? "http://unitsofmeasure.org"; return(newCoding); } Element parseExtension(IElementNavigator nav) { // HACK: For now, assume this is a typed navigator, so we have "value", // not the unparsed "valueCode" etc AND we have Type (in ParseBindable()) var valueChild = instance.Children("value").FirstOrDefault(); if (valueChild != null) { return(valueChild.ParseBindable()); } else { return(null); } } }