convertFromPrimitiveValue method

  1. @override
dynamic convertFromPrimitiveValue(
  1. dynamic value
)
override

Converts a value to a more complex value from a primitive value according to this instance's definition.

This method takes a non-Dart representation of a value (e.g. an HTTP body or database query) and turns it into a Dart representation . How this value is computed depends on this instance's definition.

Implementation

@override
dynamic convertFromPrimitiveValue(dynamic value) {
  if (value == null) {
    return null;
  }

  if (type!.kind == ManagedPropertyType.datetime) {
    if (value is! String) {
      throw ValidationException(["invalid input value for '$name'"]);
    }
    return DateTime.parse(value);
  } else if (type!.kind == ManagedPropertyType.doublePrecision) {
    if (value is! num) {
      throw ValidationException(["invalid input value for '$name'"]);
    }
    return value.toDouble();
  } else if (isEnumeratedValue) {
    if (!enumerationValueMap.containsKey(value)) {
      throw ValidationException(["invalid option for key '$name'"]);
    }
    return enumerationValueMap[value];
  } else if (type!.kind == ManagedPropertyType.document) {
    return Document(value);
  } else if (type!.kind == ManagedPropertyType.list ||
      type!.kind == ManagedPropertyType.map) {
    try {
      return entity.runtime.dynamicConvertFromPrimitiveValue(this, value);
    } on TypeCoercionException {
      throw ValidationException(["invalid input value for '$name'"]);
    }
  }

  return value;
}