internal static IComparable GetComparableValue(this ITypedElement instance, Type expectedType)
        {
            if (expectedType == typeof(Model.Quantity))
            {
                var q = instance.ParseQuantity();
                // These checks should probably be somewhere else since it has nothing to do with parsing
                if (q.Comparator != null)
                {
                    throw Error.NotSupported("Cannot interpret quantities with a comparison");
                }
                if (q.Value == null)
                {
                    throw Error.NotSupported("Cannot interpret quantities without a value");
                }

                return(new Model.Primitives.Quantity(q.Value.Value, q.Unit, q.System ?? Model.Primitives.Quantity.UCUM));
            }
            else if (instance.Value is IComparable)
            {
                return((IComparable)instance.Value);
            }
            else
            {
                return(null);
            }
        }
Пример #2
0
        /// <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 ITypedElement instance)
        {
            var instanceType = ModelInfo.FhirTypeNameToFhirType(instance.InstanceType);

            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(ITypedElement nav)
            {
                var newCoding = new Coding();
                var q         = instance.ParseQuantity();

                newCoding.Code   = q.Unit;
                newCoding.System = q.System ?? "http://unitsofmeasure.org";
                return(newCoding);
            }

            Element parseExtension(ITypedElement 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();

                return(valueChild?.ParseBindable());
            }
        }