inferType method

void inferType(
  1. Type type,
  2. bool isNullable
)

Implementation

void inferType(Type type, bool isNullable) {
  final Map<Type, AbstractType> types = {
    String: StringType(), // default unconstrained
    int: IntType(),
    double: DoubleType(),
    bool: BoolType(),
    DateTime: DateTimeType()
  };

  final Map<Type, AbstractType> nullableTypes = {
    String: StringType().optional(), // default unconstrained
    int: IntType().optional(),
    double: DoubleType().optional(),
    bool: BoolType().optional(),
    DateTime: DateTimeType().optional()
  };

  var result = isNullable ? nullableTypes[type] : types[type];

  if ( result == null)
    if ( type.toString().startsWith("List<")) {
      result = ListType(type: type, elementType: ClassType<dynamic>()); // TODO?
      if ( isNullable )
        result = (result as dynamic).optional();
    }
    else {
      if ( TypeDescriptor.hasType(type)) {
        result = ObjectType(type);
        if ( isNullable )
          result = result.optional() as AbstractType<dynamic, AbstractType>?;
      }
      else {
        // it could be a class type or a object type that hasn't been constructed yet...

        TypePatch(type: type, applyFunc: (resolvedType) => this.type = resolvedType as dynamic);
        return;
      }
    }

  this.type = result as dynamic;
}