renderConverter<T extends Object?> method

String renderConverter<T extends Object?>(
  1. ClassModel model,
  2. FieldModel field
)

The converter of this field in a form of String.

Implementation

String renderConverter<T extends Object?>(
  final ClassModel model,
  final FieldModel field,
) {
  switch (field.type) {
    case FieldType.$object:
      final String reference = field.reference.split('[').first.normalize();
      if (models.every((final _) => _.reference != reference)) {
        return '';
      }
      final String $type =
          _renderType(model, field, iterable: false, nullable: false);
      String converter =
          field.nullable ? 'optional${$type}' : $type.decapitalize();
      if (converter.endsWith('Model')) {
        converter = converter.substring(0, converter.length - 5);
      }
      return '${converter}Converter';

    case FieldType.$enum:
      final String $type =
          _renderType(model, field, iterable: false, nullable: false);
      return field.nullable
          ? 'const OptionalEnumConverter<${$type}>(${$type}.values,)'
          : 'const EnumConverter<${$type}>(${$type}.values,)';

    case FieldType.$datetime:
      return field.nullable
          ? 'optionalDateTimeConverter'
          : 'dateTimeConverter';
    case FieldType.$timedelta:
      return field.nullable
          ? 'optionalDurationConverter'
          : 'durationConverter';
    case FieldType.$boolean:
    case FieldType.$integer:
    case FieldType.$float:
    case FieldType.$string:
      return '';

    case FieldType.$$object:
      final String reference = field.reference.split('[').first.normalize();
      if (models.every((final _) => _.reference != reference)) {
        return '';
      }
      final String type =
          _renderType(model, field, iterable: false, nullable: false);
      String converter =
          field.nullable ? 'optional$type' : type.decapitalize();
      if (converter.endsWith('Model')) {
        converter = converter.substring(0, converter.length - 5);
      }
      converter += 'Converter';
      return field.nullable
          ? 'const OptionalIterableConverter<$type, '
              'Map<String, Object?>>($converter,)'
          : 'const IterableConverter<$type, '
              'Map<String, Object?>>($converter,)';

    case FieldType.$$enum:
      final String type =
          _renderType(model, field, iterable: false, nullable: false);
      final String converter = field.nullable
          ? 'OptionalEnumConverter<$type>($type.values,)'
          : 'EnumConverter<$type>($type.values,)';
      return field.nullable
          ? 'const OptionalIterableConverter<$type, String>($converter,)'
          : 'const IterableConverter<$type, String>($converter,)';

    case FieldType.$$datetime:
      return field.nullable
          ? 'const OptionalIterableConverter<DateTime, '
              'String>(dateTimeConverter)'
          : 'const IterableConverter<DateTime, String>(dateTimeConverter)';

    case FieldType.$$timedelta:
      return field.nullable
          ? 'const OptionalIterableConverter<Duration, '
              'String>(durationConverter)'
          : 'const IterableConverter<Duration, String>(durationConverter)';

    case FieldType.$$boolean:
    case FieldType.$$integer:
    case FieldType.$$float:
    case FieldType.$$string:
      return '';
  }
}