addField method

  1. @visibleForOverriding
  2. @visibleForTesting
String? addField(
  1. FieldElement field,
  2. _FieldAnnotation fieldAnnotation
)

Given each field, determine whether it can be added to the serdes function and, more importantly, determine how it should be added. If the field should not be added, return null.

Private fields, methods, static members, and computed setters are automatically ignored. See FieldsForClass#stableInstanceFields.

Implementation

@visibleForOverriding
@visibleForTesting
String? addField(FieldElement field, _FieldAnnotation fieldAnnotation) {
  var wrappedInFuture = false;

  final futureChecker = SharedChecker(field.type);
  var checker = checkerForField(field);
  if (futureChecker.isFuture) {
    wrappedInFuture = true;
    checker = checkerForType(futureChecker.argType);
  }

  final shouldIgnore = ignoreCoderForField(field, fieldAnnotation, checker);
  if (shouldIgnore) return null;

  if (wrappedInFuture && checker.isIterable && checker.isArgTypeAFuture) {
    throw InvalidGenerationSourceError(
      'Future iterable future types are not supported by Brick. Please revise to `Future<Iterable<Type>>` or `Iterable<Future<Type>>`.',
      todo: 'Revise to `Future<Iterable<Type>>` or `Iterable<Future<Type>>`',
      element: field,
    );
  }

  final coder = coderForField(
    field,
    checker,
    fieldAnnotation: fieldAnnotation,
    wrappedInFuture: wrappedInFuture,
  );
  final contents = expandGenerators(fieldAnnotation, field: field, checker: checker) ?? coder;
  if (contents == null) return null;

  final name = providerNameForField(fieldAnnotation.name, checker: checker);
  if (doesDeserialize) {
    final deserializerNullability = deserializerNullableClause(
      field: field,
      fieldAnnotation: fieldAnnotation,
      name: name,
    );
    return '${field.name}: $deserializerNullability $contents';
  }

  return "'$name': $contents";
}