ServerMimic.fromDartObject constructor

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

Implementation

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

  final typeImport = objectType?.element?.librarySource?.uri.toString();
  final importPaths = <String>{
    if (typeImport != null) typeImport,
  };

  if (object.type case final InterfaceType type?) {
    final constructorImports = type.constructors
        .map((e) => e.returnType.element.librarySource.uri.toString());

    importPaths.addAll(constructorImports);

    if (type.element case final ClassElement element) {
      for (final field in element.fields) {
        final fieldImport = field.type.element?.librarySource?.uri.toString();

        if (fieldImport != null) {
          importPaths.add(fieldImport);
        }
      }
    }
  }

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