ServerMimic.fromDartObject constructor

ServerMimic.fromDartObject(
  1. DartObject object,
  2. ElementAnnotation annotation
)

Implementation

factory ServerMimic.fromDartObject(
  DartObject object,
  ElementAnnotation annotation,
) {
  final objectType = object.type;

  final importableElements = <Element?>[objectType?.element];

  if (object.type case final InterfaceType type?) {
    importableElements.addAll(
      type.constructors.map((e) => e.returnType.element),
    );

    if (type.element case final ClassElement element) {
      for (final field in element.fields) {
        importableElements.add(field.type.element);
      }
    }
  }

  return ServerMimic(
    instance: annotation.toSource().replaceFirst('@', ''),
    importPaths: ServerImports.fromElements(importableElements),
  );
}