fromString static method

DataType fromString(
  1. String value
)

Implementation

static DataType fromString(String value) {
  final trimmedValue = value.trim();

  if (value == EmInt().nativeTypeName ||
      value == EmInt(nullable: true).nativeTypeName) {
    return EmInt(nullable: value.endsWith('?'));
  } else if (value == EmDouble().nativeTypeName ||
      value == EmDouble(nullable: true).nativeTypeName) {
    return EmDouble(nullable: value.endsWith('?'));
  } else if (value == EmNum().nativeTypeName ||
      value == EmNum(nullable: true).nativeTypeName) {
    return EmNum(nullable: value.endsWith('?'));
  } else if (value == EmString().nativeTypeName ||
      value == EmString(nullable: true).nativeTypeName) {
    return EmString(nullable: value.endsWith('?'));
  } else if (value == EmBoolean().nativeTypeName ||
      value == EmBoolean(nullable: true).nativeTypeName) {
    return EmBoolean(nullable: value.endsWith('?'));
  } else if (trimmedValue.startsWith('List<') &&
      (trimmedValue.endsWith('>') || trimmedValue.endsWith('>?'))) {
    final nullable = trimmedValue.endsWith('>?');
    final innerType = trimmedValue.substring(
      5,
      trimmedValue.length - (nullable ? 2 : 1),
    );
    final wrappedType = fromString(innerType);
    return EmList(wrapped: wrappedType, nullable: nullable);
  } else if (trimmedValue.startsWith('Map<') &&
      (trimmedValue.endsWith('>') || trimmedValue.endsWith('>?'))) {
    final nullable = trimmedValue.endsWith('>?');
    final innerTypes = trimmedValue
        .substring(4, trimmedValue.length - (nullable ? 2 : 1))
        .split(',');
    if (innerTypes.length == 2) {
      final keyType = fromString(innerTypes[0].trim());
      final valueType = fromString(innerTypes[1].trim());
      return EmMap(
        wrappedKey: keyType,
        wrappedValue: valueType,
        nullable: nullable,
      );
    } else {
      throw Exception(
          'Exception Error when trying to convert Map, "The map inner types not equal 2 is equal (${innerTypes.length})"');
    }
  } else if (trimmedValue.startsWith('Future<') &&
      (trimmedValue.endsWith('>') || trimmedValue.endsWith('>?'))) {
    final nullable = trimmedValue.endsWith('>?');
    final innerType = trimmedValue.substring(
      7,
      trimmedValue.length - (nullable ? 2 : 1),
    );
    final wrappedType = fromString(innerType);
    return EmFuture(wrapped: wrappedType, nullable: nullable);
  } else if (trimmedValue.startsWith('Stream<') &&
      (trimmedValue.endsWith('>') || trimmedValue.endsWith('>?'))) {
    final nullable = trimmedValue.endsWith('>?');
    final innerType = trimmedValue.substring(
      7,
      trimmedValue.length - (nullable ? 2 : 1),
    );
    final wrappedType = fromString(innerType);
    return EmStream(wrapped: wrappedType, nullable: nullable);
  } else if (value == const EmVoid().nativeTypeName) {
    return const EmVoid();
  } else if (value == const EmDynamic().nativeTypeName) {
    return const EmDynamic();
  } else if (value == const EmDateTime().nativeTypeName) {
    return const EmDateTime();
  } else {
    // EmEnum();
    return EmCustomDataType(dataType: trimmedValue);
  }
}