primitiveValue function

dynamic primitiveValue(
  1. String fhirFieldType,
  2. dynamic oldValue,
  3. String key
)

Implementation

dynamic primitiveValue(
  String fhirFieldType,
  dynamic oldValue,
  String key,
) {
  dynamic value;
  if (fhirFieldType == 'Decimal') {
    value = oldValue is num
        ? oldValue
        : oldValue is String
            ? int.tryParse(oldValue) ?? double.tryParse(oldValue)
            : null;
  } else if (fhirFieldType == 'Integer' ||
      fhirFieldType == 'UnsignedInt' ||
      fhirFieldType == 'PositiveInt') {
    value = oldValue is int
        ? oldValue
        : oldValue is String
            ? int.tryParse(oldValue)
            : null;
  } else if (fhirFieldType == 'Integer64') {
    value = oldValue is int || oldValue is BigInt
        ? oldValue
        : oldValue is String
            ? BigInt.tryParse(oldValue)
            : null;
  } else if (fhirFieldType == 'Boolean') {
    value = oldValue == 'true' || oldValue == true;
  } else if (fhirFieldType == 'String' ||
      //   fhirFieldType == 'Code' ||
      fhirFieldType == 'Markdown') {
    value = oldValue.toString().contains(r'[ \r\n\t\S]+')
        ? oldValue.toString()
        : oldValue
            .toString()
            .replaceAll(r'\\n', '\n')
            .replaceAll(r'\r', '\r')
            .replaceAll(r'\t', '\t');
  } else {
    value = oldValue;
  }
  if (value == null) {
    throw Exception(
        "The field $key is not a '$fhirFieldType' as defined by FHIR");
  }
  return value;
}