FileGenerator constructor

FileGenerator(
  1. FileDescriptorProto descriptor,
  2. GenerationOptions options
)

Implementation

FileGenerator(this.descriptor, this.options)
    : protoFileUri = Uri.file(descriptor.name),
      syntax = descriptor.syntax == 'proto3'
          ? ProtoSyntax.proto3
          : ProtoSyntax.proto2 {
  if (protoFileUri.isAbsolute) {
    // protoc should never generate an import with an absolute path.
    throw 'FAILURE: Import with absolute path is not supported';
  }

  final declaredMixins = _getDeclaredMixins(descriptor);
  final defaultMixinName =
      descriptor.options.getExtension(Dart_options.defaultMixin) as String? ??
          '';
  final defaultMixin =
      declaredMixins[defaultMixinName] ?? findMixin(defaultMixinName);
  if (defaultMixin == null && defaultMixinName.isNotEmpty) {
    throw 'Option default_mixin on file ${descriptor.name}: Unknown mixin '
        '$defaultMixinName';
  }

  // Load and register all enum and message types.
  for (var i = 0; i < descriptor.enumType.length; i++) {
    enumGenerators.add(EnumGenerator.topLevel(
        descriptor.enumType[i], this, usedTopLevelNames, i));
  }
  for (var i = 0; i < descriptor.messageType.length; i++) {
    messageGenerators.add(MessageGenerator.topLevel(descriptor.messageType[i],
        this, declaredMixins, defaultMixin, usedTopLevelNames, i));
  }
  for (var i = 0; i < descriptor.extension.length; i++) {
    extensionGenerators.add(ExtensionGenerator.topLevel(
        descriptor.extension[i], this, usedExtensionNames, i));
  }
  for (final service in descriptor.service) {
    if (options.useGrpc) {
      grpcGenerators.add(GrpcServiceGenerator(service, this));
    } else {
      final serviceGen =
          ServiceGenerator(service, this, usedTopLevelServiceNames);
      serviceGenerators.add(serviceGen);
      clientApiGenerators
          .add(ClientApiGenerator(serviceGen, usedTopLevelNames));
    }
  }
}