ServerBindsAnnotation.fromElement constructor

ServerBindsAnnotation.fromElement(
  1. DartObject object,
  2. ElementAnnotation annotation
)

Implementation

factory ServerBindsAnnotation.fromElement(
  DartObject object,
  // ignore: avoid_unused_constructor_parameters
  ElementAnnotation annotation,
) {
  final bind = object.getField('bind')?.toTypeValue();
  if (bind == null) {
    throw ArgumentError('Invalid type');
  }

  final bindSuper = (bind.element as ClassElement?)
      ?.allSupertypes
      .firstWhereOrNull((element) {
    return element.element.name == (Bind).name;
  });

  final firstTypeArg = bindSuper?.typeArguments.first;

  return ServerBindsAnnotation(
    bind: ServerBind.fromType(bind),
    acceptsNull: switch (firstTypeArg?.nullabilitySuffix) {
      final prefix? => prefix == NullabilitySuffix.question,
      _ => null,
    },
  );
}