decodingStatement static method

String decodingStatement(
  1. LibraryElement library,
  2. DartType type,
  3. String codec,
  4. String value,
  5. String name, {
  6. bool nonNull = false,
})

Implementation

static String decodingStatement(
  LibraryElement library,
  DartType type,
  String codec,
  String value,
  String name, {
  bool nonNull = false,
}) {
  if (type.nullabilitySuffix == NullabilitySuffix.question && !nonNull) {
    final decodeItem = decodingFunction(library, type, codec, nonNull: true);
    return '$codec.decodeNullable($value, $decodeItem, name: $name)';
  }

  if (type.element == null) {
    throw Exception('Invalid type for decoding: $type');
  }

  if (type.element is EnumElement) {
    final typeName =
        typeImportPrefix(type, library) + typeExpression(type, library);
    return '$codec.decodeEnum($value, $typeName.values, name: name)';
  }

  if (type.isDartCoreList) {
    final valueType = (type as ParameterizedType).typeArguments[0];
    final typeName =
        typeImportPrefix(valueType, library) +
        typeExpression(valueType, library);
    final decodeItem = decodingFunction(library, valueType, codec);
    return '$codec.decodeList<$typeName>($value, $decodeItem, name: $name)';
  } else if (type.isDartCoreMap) {
    final valueType = (type as ParameterizedType).typeArguments[1];
    final typeName =
        typeImportPrefix(valueType, library) +
        typeExpression(valueType, library);
    final decodeItem = decodingFunction(library, valueType, codec);
    return '$codec.decodeMap<$typeName>($value, $decodeItem, name: $name)';
  } else {
    return '${decodingFunction(library, type, codec)}($value, name: $name)';
  }
}