encodingFunction static method

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

Implementation

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

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

  if (TypeChecker.typeNamed(Data).hasAnnotationOf(type.element!)) {
    return '(v) => v.toJson()';
  }

  if (type.element is EnumElement) {
    return '$codec.encodeEnum';
  }

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

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