com.google.protobuf.DescriptorProtos.FileDescriptorProto Java Examples

The following examples show how to use com.google.protobuf.DescriptorProtos.FileDescriptorProto. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServiceResolver.java    From grpc-swagger with MIT License 6 votes vote down vote up
/**
 * Recursively constructs file descriptors for all dependencies of the supplied proto and returns
 * a {@link FileDescriptor} for the supplied proto itself. For maximal efficiency, reuse the
 * descriptorCache argument across calls.
 */
private static FileDescriptor descriptorFromProto(
        FileDescriptorProto descriptorProto,
        ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex,
        Map<String, FileDescriptor> descriptorCache) throws DescriptorValidationException {
    // First, check the cache.
    String descriptorName = descriptorProto.getName();
    if (descriptorCache.containsKey(descriptorName)) {
        return descriptorCache.get(descriptorName);
    }

    // Then, fetch all the required dependencies recursively.
    ImmutableList.Builder<FileDescriptor> dependencies = ImmutableList.builder();
    for (String dependencyName : descriptorProto.getDependencyList()) {
        if (!descriptorProtoIndex.containsKey(dependencyName)) {
            throw new IllegalArgumentException("Could not find dependency: " + dependencyName);
        }
        FileDescriptorProto dependencyProto = descriptorProtoIndex.get(dependencyName);
        dependencies.add(descriptorFromProto(dependencyProto, descriptorProtoIndex, descriptorCache));
    }

    // Finally, construct the actual descriptor.
    FileDescriptor[] empty = new FileDescriptor[0];
    return FileDescriptor.buildFrom(descriptorProto, dependencies.build().toArray(empty));
}
 
Example #2
Source File: GrpcDocStringExtractor.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Nullable
private static String getFullName(FileDescriptorProto descriptor, List<Integer> path) {
    String fullNameSoFar = descriptor.getPackage();
    switch (path.get(0)) {
        case FileDescriptorProto.MESSAGE_TYPE_FIELD_NUMBER:
            final DescriptorProto message = descriptor.getMessageType(path.get(1));
            return appendMessageToFullName(message, path, fullNameSoFar);
        case FileDescriptorProto.ENUM_TYPE_FIELD_NUMBER:
            final EnumDescriptorProto enumDescriptor = descriptor.getEnumType(path.get(1));
            return appendEnumToFullName(enumDescriptor, path, fullNameSoFar);
        case FileDescriptorProto.SERVICE_FIELD_NUMBER:
            final ServiceDescriptorProto serviceDescriptor = descriptor.getService(path.get(1));
            fullNameSoFar = appendNameComponent(fullNameSoFar, serviceDescriptor.getName());
            if (path.size() > 2) {
                fullNameSoFar = appendFieldComponent(
                        fullNameSoFar, serviceDescriptor.getMethod(path.get(3)).getName());
            }
            return fullNameSoFar;
        default:
            return null;
    }
}
 
Example #3
Source File: ServiceResolver.java    From milkman with MIT License 6 votes vote down vote up
/** Creates a resolver which searches the supplied {@link FileDescriptorSet}. */
public static ServiceResolver fromFileDescriptorSet(FileDescriptorSet descriptorSet) {
  ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex =
      computeDescriptorProtoIndex(descriptorSet);
  Map<String, FileDescriptor> descriptorCache = new HashMap<>();

  ImmutableList.Builder<FileDescriptor> result = ImmutableList.builder();
  for (FileDescriptorProto descriptorProto : descriptorSet.getFileList()) {
    try {
      result.add(descriptorFromProto(descriptorProto, descriptorProtoIndex, descriptorCache));
    } catch (DescriptorValidationException e) {
      logger.warn("Skipped descriptor " + descriptorProto.getName() + " due to error", e);
      continue;
    }
  }
  return new ServiceResolver(result.build());
}
 
Example #4
Source File: ServiceResolver.java    From milkman with MIT License 6 votes vote down vote up
/**
 * Recursively constructs file descriptors for all dependencies of the supplied proto and returns
 * a {@link FileDescriptor} for the supplied proto itself. For maximal efficiency, reuse the
 * descriptorCache argument across calls.
 */
private static FileDescriptor descriptorFromProto(
    FileDescriptorProto descriptorProto,
    ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex,
    Map<String, FileDescriptor> descriptorCache) throws DescriptorValidationException {
  // First, check the cache.
  String descritorName = descriptorProto.getName();
  if (descriptorCache.containsKey(descritorName)) {
    return descriptorCache.get(descritorName);
  }

  // Then, fetch all the required dependencies recursively.
  ImmutableList.Builder<FileDescriptor> dependencies = ImmutableList.builder();
  for (String dependencyName : descriptorProto.getDependencyList()) {
    if (!descriptorProtoIndex.containsKey(dependencyName)) {
      throw new IllegalArgumentException("Could not find dependency: " + dependencyName);
    }
    FileDescriptorProto dependencyProto = descriptorProtoIndex.get(dependencyName);
    dependencies.add(descriptorFromProto(dependencyProto, descriptorProtoIndex, descriptorCache));
  }

  // Finally, construct the actual descriptor.
  FileDescriptor[] empty = new FileDescriptor[0];
  return FileDescriptor.buildFrom(descriptorProto, dependencies.build().toArray(empty));
}
 
Example #5
Source File: DescriptorsTest.java    From travelguide with Apache License 2.0 6 votes vote down vote up
public void testInvalidPublicDependency() throws Exception {
  FileDescriptorProto fooProto = FileDescriptorProto.newBuilder()
      .setName("foo.proto") .build();
  FileDescriptorProto barProto = FileDescriptorProto.newBuilder()
      .setName("boo.proto")
      .addDependency("foo.proto")
      .addPublicDependency(1)  // Error, should be 0.
      .build();
  FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom(fooProto,
      new FileDescriptor[0]);
  try {
    Descriptors.FileDescriptor.buildFrom(barProto,
        new FileDescriptor[] {fooFile});
    fail("DescriptorValidationException expected");
  } catch (DescriptorValidationException e) {
    assertTrue(
        e.getMessage().indexOf("Invalid public dependency index.") != -1);
  }
}
 
Example #6
Source File: ServiceResolver.java    From grpc-swagger with MIT License 6 votes vote down vote up
/**
 * Recursively constructs file descriptors for all dependencies of the supplied proto and returns
 * a {@link FileDescriptor} for the supplied proto itself. For maximal efficiency, reuse the
 * descriptorCache argument across calls.
 */
private static FileDescriptor descriptorFromProto(
        FileDescriptorProto descriptorProto,
        ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex,
        Map<String, FileDescriptor> descriptorCache) throws DescriptorValidationException {
    // First, check the cache.
    String descriptorName = descriptorProto.getName();
    if (descriptorCache.containsKey(descriptorName)) {
        return descriptorCache.get(descriptorName);
    }

    // Then, fetch all the required dependencies recursively.
    ImmutableList.Builder<FileDescriptor> dependencies = ImmutableList.builder();
    for (String dependencyName : descriptorProto.getDependencyList()) {
        if (!descriptorProtoIndex.containsKey(dependencyName)) {
            throw new IllegalArgumentException("Could not find dependency: " + dependencyName);
        }
        FileDescriptorProto dependencyProto = descriptorProtoIndex.get(dependencyName);
        dependencies.add(descriptorFromProto(dependencyProto, descriptorProtoIndex, descriptorCache));
    }

    // Finally, construct the actual descriptor.
    FileDescriptor[] empty = new FileDescriptor[0];
    return FileDescriptor.buildFrom(descriptorProto, dependencies.build().toArray(empty));
}
 
Example #7
Source File: ServerReflectionClient.java    From grpc-swagger with MIT License 6 votes vote down vote up
private void processDependencies(FileDescriptorProto fileDescriptor) {
    logger.debug("Processing deps of descriptor: " + fileDescriptor.getName());
    fileDescriptor.getDependencyList().forEach(dep -> {
        if (!resolvedDescriptors.containsKey(dep) && !requestedDescriptors.contains(dep)) {
            requestedDescriptors.add(dep);
            ++outstandingRequests;
            requestStream.onNext(requestForDescriptor(dep));
        }
    });

    --outstandingRequests;
    if (outstandingRequests == 0) {
        logger.debug("Retrieved service definition for [{}] by reflection", serviceName);
        resultFuture.set(FileDescriptorSet.newBuilder()
                .addAllFile(resolvedDescriptors.values())
                .build());
        requestStream.onCompleted();
    }
}
 
Example #8
Source File: ServerReflectionClient.java    From milkman with MIT License 6 votes vote down vote up
private void processDependencies(FileDescriptorProto fileDescriptor) {
  logger.debug("Processing deps of descriptor: " + fileDescriptor.getName());
  fileDescriptor.getDependencyList().forEach(dep -> {
    if (!resolvedDescriptors.containsKey(dep) && !requestedDescriptors.contains(dep)) {
      requestedDescriptors.add(dep);
      ++outstandingRequests;
      requestStream.onNext(requestForDescriptor(dep));
    }
  });

  --outstandingRequests;
  if (outstandingRequests == 0) {
    logger.debug("Retrieved service definition for [{}] by reflection", serviceName);
    resultFuture.set(FileDescriptorSet.newBuilder()
        .addAllFile(resolvedDescriptors.values())
        .build());
    requestStream.onCompleted();
  }
}
 
Example #9
Source File: ServiceResolver.java    From grpc-swagger with MIT License 6 votes vote down vote up
/**
 * Creates a resolver which searches the supplied {@link FileDescriptorSet}.
 */
public static ServiceResolver fromFileDescriptorSet(FileDescriptorSet descriptorSet) {
    ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex =
            computeDescriptorProtoIndex(descriptorSet);
    Map<String, FileDescriptor> descriptorCache = new HashMap<>();

    ImmutableList.Builder<FileDescriptor> result = ImmutableList.builder();
    for (FileDescriptorProto descriptorProto : descriptorSet.getFileList()) {
        try {
            result.add(descriptorFromProto(descriptorProto, descriptorProtoIndex, descriptorCache));
        } catch (DescriptorValidationException e) {
            logger.warn("Skipped descriptor " + descriptorProto.getName() + " due to error", e);
        }
    }
    return new ServiceResolver(result.build());
}
 
Example #10
Source File: FileDescriptor.java    From servicetalk with Apache License 2.0 6 votes vote down vote up
FileDescriptor(final FileDescriptorProto protoFile) {
    this.protoFile = protoFile;
    sanitizedProtoFileName = sanitizeFileName(protoFile.getName());
    protoPackageName = protoFile.hasPackage() ? protoFile.getPackage() : null;

    if (protoFile.hasOptions()) {
        final FileOptions fileOptions = protoFile.getOptions();
        deprecated = fileOptions.hasDeprecated() && fileOptions.getDeprecated();
        multipleClassFiles = fileOptions.hasJavaMultipleFiles() && fileOptions.getJavaMultipleFiles();
        javaPackageName = fileOptions.hasJavaPackage() ? fileOptions.getJavaPackage() : null;
        outerClassName = fileOptions.hasJavaOuterClassname() ? fileOptions.getJavaOuterClassname() : null;
    } else {
        deprecated = false;
        multipleClassFiles = false;
        javaPackageName = null;
        outerClassName = null;
    }

    serviceClassBuilders = new ArrayList<>(protoFile.getServiceCount());
}
 
Example #11
Source File: ServiceResolver.java    From grpc-swagger with MIT License 6 votes vote down vote up
/**
 * Creates a resolver which searches the supplied {@link FileDescriptorSet}.
 */
public static ServiceResolver fromFileDescriptorSet(FileDescriptorSet descriptorSet) {
    ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex =
            computeDescriptorProtoIndex(descriptorSet);
    Map<String, FileDescriptor> descriptorCache = new HashMap<>();

    ImmutableList.Builder<FileDescriptor> result = ImmutableList.builder();
    for (FileDescriptorProto descriptorProto : descriptorSet.getFileList()) {
        try {
            result.add(descriptorFromProto(descriptorProto, descriptorProtoIndex, descriptorCache));
        } catch (DescriptorValidationException e) {
            logger.warn("Skipped descriptor " + descriptorProto.getName() + " due to error", e);
        }
    }
    return new ServiceResolver(result.build());
}
 
Example #12
Source File: ProtobufDecompiler.java    From sql-layer with GNU Affero General Public License v3.0 6 votes vote down vote up
public void decompile(FileDescriptorProto fileDescriptor) throws IOException {
    if (fileDescriptor.hasPackage()) {
        indentedFormat("package %s;", fileDescriptor.getPackage());
        absolutePackage = "." + fileDescriptor.getPackage() + ".";
    }
    for (String dependency : fileDescriptor.getDependencyList()) {
        indentedFormat("import \"%s\";", dependency);
    }
    if (fileDescriptor.hasOptions()) {
        decompileOptions(fileDescriptor.getOptions());
    }
    decompileMembers(fileDescriptor.getEnumTypeList(),
                     fileDescriptor.getMessageTypeList(),
                     Collections.<FieldDescriptorProto>emptyList(),
                     Collections.<DescriptorProto.ExtensionRange>emptyList(),
                     fileDescriptor.getExtensionList());
    for (ServiceDescriptorProto serviceDescriptor : fileDescriptor.getServiceList()) {
        decompile(serviceDescriptor);
    }
    newline();
    flush();
}
 
Example #13
Source File: ServerReflectionClient.java    From grpc-swagger with MIT License 6 votes vote down vote up
private void processDependencies(FileDescriptorProto fileDescriptor) {
    logger.debug("Processing deps of descriptor: " + fileDescriptor.getName());
    fileDescriptor.getDependencyList().forEach(dep -> {
        if (!resolvedDescriptors.containsKey(dep) && !requestedDescriptors.contains(dep)) {
            requestedDescriptors.add(dep);
            ++outstandingRequests;
            requestStream.onNext(requestForDescriptor(dep));
        }
    });

    --outstandingRequests;
    if (outstandingRequests == 0) {
        logger.debug("Retrieved service definition for [{}] by reflection", serviceName);
        resultFuture.set(FileDescriptorSet.newBuilder()
                .addAllFile(resolvedDescriptors.values())
                .build());
        requestStream.onCompleted();
    }
}
 
Example #14
Source File: Descriptors.java    From play-store-api with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Replace our {@link FileDescriptorProto} with the given one, which is
 * identical except that it might contain extensions that weren't present
 * in the original.  This method is needed for bootstrapping when a file
 * defines custom options.  The options may be defined in the file itself,
 * so we can't actually parse them until we've constructed the descriptors,
 * but to construct the descriptors we have to have parsed the descriptor
 * protos.  So, we have to parse the descriptor protos a second time after
 * constructing the descriptors.
 */
private void setProto(final FileDescriptorProto proto) {
  this.proto = proto;

  for (int i = 0; i < messageTypes.length; i++) {
    messageTypes[i].setProto(proto.getMessageType(i));
  }

  for (int i = 0; i < enumTypes.length; i++) {
    enumTypes[i].setProto(proto.getEnumType(i));
  }

  for (int i = 0; i < services.length; i++) {
    services[i].setProto(proto.getService(i));
  }

  for (int i = 0; i < extensions.length; i++) {
    extensions[i].setProto(proto.getExtension(i));
  }
}
 
Example #15
Source File: AbstractGenerator.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
private List<ServiceContext> findServices(List<FileDescriptorProto> protos, ProtoTypeMap typeMap) {
    List<ServiceContext> contexts = new ArrayList<>();
    for (FileDescriptorProto fileProto : protos) {
        for (int serviceNumber = 0; serviceNumber < fileProto.getServiceCount(); serviceNumber++) {
            ServiceContext serviceContext = buildServiceContext(
                    fileProto.getService(serviceNumber),
                    typeMap,
                    fileProto.getSourceCodeInfo().getLocationList(),
                    serviceNumber
            );
            serviceContext.setProtoName(fileProto.getName());
            serviceContext.setPackageName(extractPackageName(fileProto));
            contexts.add(serviceContext);
        }
    }

    return contexts;
}
 
Example #16
Source File: Descriptors.java    From play-store-api with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Create a placeholder FileDescriptor for a message Descriptor.
 */
FileDescriptor(String packageName, Descriptor message)
    throws DescriptorValidationException {
  this.pool = new DescriptorPool(new FileDescriptor[0], true);
  this.proto = FileDescriptorProto.newBuilder()
      .setName(message.getFullName() + ".placeholder.proto")
      .setPackage(packageName).addMessageType(message.toProto()).build();
  this.dependencies = new FileDescriptor[0];
  this.publicDependencies = new FileDescriptor[0];

  messageTypes = new Descriptor[] {message};
  enumTypes = new EnumDescriptor[0];
  services = new ServiceDescriptor[0];
  extensions = new FieldDescriptor[0];

  pool.addPackage(packageName, this);
  pool.addSymbol(message);
}
 
Example #17
Source File: Descriptors.java    From play-store-api with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Construct a {@code FileDescriptor}.
 *
 * @param proto The protocol message form of the FileDescriptor.
 * @param dependencies {@code FileDescriptor}s corresponding to all of
 *                     the file's dependencies.
 * @param allowUnknownDependencies If true, non-exist dependenncies will be
 *           ignored and undefined message types will be replaced with a
 *           placeholder type.
 * @throws DescriptorValidationException {@code proto} is not a valid
 *           descriptor.  This can occur for a number of reasons, e.g.
 *           because a field has an undefined type or because two messages
 *           were defined with the same name.
 */
public static FileDescriptor buildFrom(
    final FileDescriptorProto proto, final FileDescriptor[] dependencies,
    final boolean allowUnknownDependencies)
    throws DescriptorValidationException {
  // Building descriptors involves two steps:  translating and linking.
  // In the translation step (implemented by FileDescriptor's
  // constructor), we build an object tree mirroring the
  // FileDescriptorProto's tree and put all of the descriptors into the
  // DescriptorPool's lookup tables.  In the linking step, we look up all
  // type references in the DescriptorPool, so that, for example, a
  // FieldDescriptor for an embedded message contains a pointer directly
  // to the Descriptor for that message's type.  We also detect undefined
  // types in the linking step.
  final DescriptorPool pool = new DescriptorPool(
      dependencies, allowUnknownDependencies);
  final FileDescriptor result = new FileDescriptor(
      proto, dependencies, pool, allowUnknownDependencies);
  result.crossLink();
  return result;
}
 
Example #18
Source File: ReactiveGrpcGenerator.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private List<ServiceContext> findServices(List<FileDescriptorProto> protos, ProtoTypeMap typeMap) {
    List<ServiceContext> contexts = new ArrayList<>();

    protos.forEach(fileProto -> {
        for (int serviceNumber = 0; serviceNumber < fileProto.getServiceCount(); serviceNumber++) {
            ServiceContext serviceContext = buildServiceContext(
                fileProto.getService(serviceNumber),
                typeMap,
                fileProto.getSourceCodeInfo().getLocationList(),
                serviceNumber
            );
            serviceContext.protoName = fileProto.getName();
            serviceContext.packageName = extractPackageName(fileProto);
            contexts.add(serviceContext);
        }
    });

    return contexts;
}
 
Example #19
Source File: CommonProto2Java.java    From saluki with Apache License 2.0 6 votes vote down vote up
public void generateFile(String protoPath) {
  try {
    if (pojoTypes == null) {
      pojoTypes = Maps.newHashMap();
    }
  } finally {
    if (!new File(protoPath).exists()) {
      logger.warn("protoPath:" + protoPath
          + " not exist, it may be in the third party jars, so it can't be generate");
      return;
    }
    FileDescriptorSet fileDescriptorSet = commondProtoc.invoke(protoPath);
    for (FileDescriptorProto fdp : fileDescriptorSet.getFileList()) {
      Pair<String, String> packageClassName = this.packageClassName(fdp.getOptions());
      if (packageClassName == null) {
        continue;
      }
      ProtocolStringList dependencyList = fdp.getDependencyList();
      for (Iterator<String> it = dependencyList.iterator(); it.hasNext();) {
        String dependencyPath = discoveryRoot + "/" + it.next();
        generateFile(dependencyPath);
      }
      doPrint(fdp, packageClassName.getLeft(), packageClassName.getRight());
    }
  }
}
 
Example #20
Source File: DescriptorGenerator.java    From api-compiler with Apache License 2.0 6 votes vote down vote up
private FileDescriptorProto generateFile(String name, FileContents contents) {
  FileDescriptorProto.Builder fileBuilder = FileDescriptorProto.newBuilder();
  fileBuilder.setName(name);
  if (!Strings.isNullOrEmpty(contents.packageName)) {
    fileBuilder.setPackage(contents.packageName);
  }
  for (Api api : contents.apis) {
    fileBuilder.addService(generateApi(api));
  }
  for (Type type : contents.types.values()) {
    fileBuilder.addMessageType(generateType(type, contents));
  }
  for (Enum e : contents.enums) {
    fileBuilder.addEnumType(generateEnum(e));
  }
  if (imports.containsKey(name)) {
    for (String imported : imports.get(name)) {
      fileBuilder.addDependency(imported);
    }
  }
  return fileBuilder.build();
}
 
Example #21
Source File: ExtensionPool.java    From api-compiler with Apache License 2.0 6 votes vote down vote up
private void add(FileDescriptorProto file) {
  currentFile = file;
  fullNameSegments.push(file.getPackage());
  locationMap = buildLocationMap(file);
  pathSegments.push(FileDescriptorProto.EXTENSION_FIELD_NUMBER);
  add(file.getExtensionList());
  pathSegments.pop();
  pathSegments.push(FileDescriptorProto.MESSAGE_TYPE_FIELD_NUMBER);
  for (int i = 0; i < file.getMessageTypeCount(); i++) {
    pathSegments.push(i);
    add(file.getMessageType(i));
    pathSegments.pop();
  }
  pathSegments.pop();
  fullNameSegments.pop();
}
 
Example #22
Source File: ProtoFile.java    From api-compiler with Apache License 2.0 6 votes vote down vote up
private static Syntax getProtoSyntax(FileDescriptorProto proto) {
  if (!proto.hasSyntax()) {
    // TODO(user): This can be removed once protoc outputs proto2 when proto2 is being used.
    //     According to liujisi@ it would break a lot of tests, so it is currently not done.
    return Syntax.SYNTAX_PROTO2;
  }
  switch (proto.getSyntax()) {
    case "proto2":
      return Syntax.SYNTAX_PROTO2;
    case "proto3":
      return Syntax.SYNTAX_PROTO3;
    default:
      throw new IllegalArgumentException(
          "Illegal proto syntax for file " + proto.getName() + ": " + proto.getSyntax());
  }
}
 
Example #23
Source File: ProtobufStorageDescriptionHelper.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
static ProtobufRowDataConverter buildRowDataConverter(HasStorage object,
                                               FileDescriptorProto fileProto) {
    Group group = (Group)object;
    FileDescriptor fileDescriptor;
    try {
        fileDescriptor = FileDescriptor.buildFrom(fileProto, DEPENDENCIES);
    }
    catch (DescriptorValidationException ex) {
        throw new ProtobufBuildException(ex);
    }
    return ProtobufRowDataConverter.forGroup(group, fileDescriptor);
}
 
Example #24
Source File: FileNodeInfo.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
private void resetPathsForFile(FileDescriptorProto.Builder file) {
  if (modifiedSourceCodeInfo) {
    SourceCodeInfo.Builder sourceCodeInfo = file.getSourceCodeInfoBuilder();
    sourceCodeInfo.clearLocation();

    new LocationInfoUpdater(sourceCodeInfo).visit(pathToLocation);

    modifiedSourceCodeInfo = false;
  }

  elementToOriginalPath.clear();
  pathToLocation.clear();
}
 
Example #25
Source File: FileNodeInfo.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
private void setupPathsForFile(FileDescriptorProto.Builder file) {
  // Populate location map
  if (file.hasSourceCodeInfo() && manageSourceCodeInfo) {
    for (SourceCodeInfo.Location location : file.getSourceCodeInfo().getLocationList()) {
      pathToLocation.addDataElement(new ProtoPathWrapper(location.getPathList()), location);
    }
  } else {
    // Turn off SourceCodeInfo management if there is none.
    manageSourceCodeInfo = false;
  }
}
 
Example #26
Source File: BuilderVisitor.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
@Accepts
protected void accept(final FileDescriptorProto.Builder file) {
  pushParent(BuilderVisitorNodeInfo.create(file));
  currentFile.setManageSourceCodeInfo(manageSourceCodeInfo);

  visitRepeated(FileDescriptorProto.MESSAGE_TYPE_FIELD_NUMBER);
  visitRepeated(FileDescriptorProto.ENUM_TYPE_FIELD_NUMBER);
  visitRepeated(FileDescriptorProto.EXTENSION_FIELD_NUMBER);
  visitRepeated(FileDescriptorProto.SERVICE_FIELD_NUMBER);

  visit(file.getOptionsBuilder());

  popExpectedParent(file);
}
 
Example #27
Source File: BuilderVisitor.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
protected BuilderVisitorNodeInfo pushParent(BuilderVisitorNodeInfo pushed) {
  ancestors.push(pushed);

  if (pushed instanceof FileNodeInfo) {
    currentFile = (FileNodeInfo) pushed;
    fullyQualifiedNameComponents.push(((FileDescriptorProto.Builder) pushed.node()).getPackage());
  } else {
    fullyQualifiedNameComponents.push(ProtoHelpers.getName(pushed.node()));
  }
  pushed.setFullyQualifiedName(getFullyQualifiedName());
  return pushed;
}
 
Example #28
Source File: AvroToProtoSchema.java    From metastore with Apache License 2.0 5 votes vote down vote up
public ProtoDomain get() {
  rootNamespace = root.path("namespace").asText();

  protoMessageOf(root);

  Map<String, FileDescriptorProto.Builder> fileMap = new HashMap<>();

  messageMap.forEach(
      (fullName, message) -> {
        String packageName =
            fullName.substring(0, fullName.length() - message.getName().length() - 1);
        FileDescriptorProto.Builder fdp = fileMap.get(packageName);
        if (fdp == null) {
          fdp =
              DescriptorProtos.FileDescriptorProto.newBuilder()
                  .setName(packageNameToFileName(packageName))
                  .setPackage(packageName)
                  .setSyntax("proto3");
          fileMap.put(packageName, fdp);
        }
        fdp.addMessageType(message);
      });

  DescriptorProtos.FileDescriptorSet.Builder fds =
      DescriptorProtos.FileDescriptorSet.newBuilder();
  fileMap.forEach(
      (name, fdp) -> {
        Set<String> imports = importMap.get(fdp.getPackage());
        if (imports != null) {
          imports.forEach(im -> fdp.addDependency(im));
        }

        fds.addFile(fdp);
      });
  fds.addFile(Int64Value.getDescriptor().getFile().toProto());

  return ProtoDomain.buildFrom(fds.build());
}
 
Example #29
Source File: ProtobufStorageDescriptionHelper.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
static ProtobufRowConverter buildRowConverter(HasStorage object, FileDescriptorProto fileProto) {
    Group group = (Group) object;
    FileDescriptor fileDescriptor;
    try {
        fileDescriptor = FileDescriptor.buildFrom(fileProto, DEPENDENCIES);
    }
    catch (DescriptorValidationException ex) {
        throw new ProtobufBuildException(ex);
    }
    return ProtobufRowConverter.forGroup(group, fileDescriptor);
}
 
Example #30
Source File: ProtocolBuffersMetaData.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
private FileDescriptor[] getDependencyDescriptors(final FileDescriptorProto fileDescriptorProto) {
	return
		fileDescriptorProto
			.getDependencyList()
			.stream()
			.map(dependency -> findLoadedDescriptor(dependency).orElseThrow(
				() -> new RuntimeException(
					"Dependency " + dependency + " of " + fileDescriptorProto.getName() + " not found"
				)
			))
			.toArray(FileDescriptor[]::new);
}