decodingFunction static method

String decodingFunction(
  1. LibraryElement library,
  2. DartType type,
  3. String codec, {
  4. bool nonNull = false,
})

Implementation

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

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

  if (type.element is EnumElement) {
    return '(v, {String? name}) => $codec.decodeEnum(v, ${type.element!.displayName}.values, name: name)';
  }

  if (type.isDartCoreList || type.isDartCoreMap) {
    return '(v, {String? name}) => ${decodingStatement(library, type, codec, 'v', 'name', nonNull: nonNull)}';
  }

  if (TypeChecker.typeNamed(Data).hasAnnotationOf(type.element!)) {
    final dataDeclarationName = typeExpression(type, library);
    final typeName = dataDeclarationName.startsWith('\$')
        ? dataDeclarationName.substring(1)
        : dataDeclarationName;
    return '${typeImportPrefix(type, library)}\$$typeName.bean.fromJson';
  }

  return '$codec.decode${firstUp(type.element!.displayName)}';
}