Java Code Examples for com.google.protobuf.Descriptors#FileDescriptor

The following examples show how to use com.google.protobuf.Descriptors#FileDescriptor . 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 Project: js-dossier   File: ProtoDescriptorsToJs.java    License: Apache License 2.0 6 votes vote down vote up
private void processFiles(Descriptors.FileDescriptor... files) throws IOException {
  List<Map<String, ?>> data = new ArrayList<>();

  for (Descriptors.FileDescriptor file : files) {
    collectEnumData(file.getEnumTypes(), data);
    collectMessageData(file.getMessageTypes(), data);
  }

  try (PrintWriter writer =
      new PrintWriter(
          new BufferedWriter(
              new OutputStreamWriter(Files.newOutputStream(output), StandardCharsets.UTF_8)))) {
    tofu.newRenderer("dossier.soy.proto.render")
        .setData(ImmutableMap.of("data", data))
        .setContentKind(ContentKind.JS)
        .render(writer);
    writer.flush();
  }
}
 
Example 2
@Override
protected DynamicMessage readData(byte[] schema, ByteBuffer buffer, int start, int length) {
    try {
        Serde.Schema s = Serde.Schema.parseFrom(schema);
        Descriptors.FileDescriptor fileDescriptor = toFileDescriptor(s);

        byte[] bytes = new byte[length];
        System.arraycopy(buffer.array(), start, bytes, 0, length);
        ByteArrayInputStream is = new ByteArrayInputStream(bytes);

        Serde.Ref ref = Serde.Ref.parseDelimitedFrom(is);

        Descriptors.Descriptor descriptor = fileDescriptor.findMessageTypeByName(ref.getName());
        return DynamicMessage.parseFrom(descriptor, is);
    } catch (IOException | Descriptors.DescriptorValidationException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 3
Source Project: grpc-swagger   File: OpenApiParser.java    License: MIT License 6 votes vote down vote up
/**
 * key : field full name
 * value: definition type
 */
public static SwaggerV2Documentation parseDefinition(ServiceResolver serviceResolver) {
    List<Descriptors.FileDescriptor> fileDescriptors = serviceResolver.getFileDescriptors();

    Map<String, DefinitionType> typeLookupTable = new HashMap<>();
    OpenApiDefinitionHandler definitionHandler = new OpenApiDefinitionHandler(typeLookupTable);
    definitionHandler.parseModelTypes(fileDescriptors);
    definitionHandler.processMessageFields();
    Map<String, PathItem> pathItemMap = definitionHandler.parsePaths(serviceResolver);
    SwaggerV2Documentation swaggerV2Documentation = new SwaggerV2Documentation();
    swaggerV2Documentation.setInfo(new InfoObject.InfoObjectBuilder()
            .title("grpc-swagger").build());

    swaggerV2Documentation.setDefinitions(typeLookupTable);
    swaggerV2Documentation.setPaths(pathItemMap);
    
    registerService(serviceResolver, swaggerV2Documentation);
    return swaggerV2Documentation;
}
 
Example 4
Source Project: metastore   File: ShadowTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void multipleOptionsTest() throws Exception {
  String fileName = "test/v1/simple.proto";
  ProtoDomain baseMultipleOptions = TestSets.baseMultipleOptions();
  ProtoDomain base = TestSets.baseKnownOption();

  ValidationResults results = new ValidationResults();
  ProtoDiff diff = new ProtoDiff(base, baseMultipleOptions, results);
  diff.diffOnFileName(fileName);

  Report result = results.createProto();
  System.out.println(result);

  ProtoDomain shadow = new ShadowApply().applyDelta(base, result);

  Descriptors.FileDescriptor expectedDescriptor =
      baseMultipleOptions.getFileDescriptorByFileName(fileName);
  Descriptors.FileDescriptor actualDescriptor = shadow.getFileDescriptorByFileName(fileName);
  Assert.assertArrayEquals(
      expectedDescriptor.toProto().toByteArray(), actualDescriptor.toProto().toByteArray());
}
 
Example 5
/**
 * Deserializes the meta-data proto into the builder.
 * @param metaDataProto the proto of the {@link RecordMetaData}
 * @param processExtensionOptions whether to add primary keys and indexes based on extensions in the protobuf
 * @return this builder
 */
@Nonnull
public RecordMetaDataBuilder setRecords(@Nonnull RecordMetaDataProto.MetaData metaDataProto,
                                        boolean processExtensionOptions) {
    if (recordsDescriptor != null) {
        throw new MetaDataException("Records already set.");
    }
    // Build the recordDescriptor by de-serializing the metaData proto
    Map<String, DescriptorProtos.FileDescriptorProto> protoDependencies = new TreeMap<>();
    for (DescriptorProtos.FileDescriptorProto dependency : metaDataProto.getDependenciesList()) {
        protoDependencies.put(dependency.getName(), dependency);
    }
    Map<String, Descriptors.FileDescriptor> generatedDependencies = initGeneratedDependencies(protoDependencies);
    Descriptors.FileDescriptor[] dependencies = getDependencies(metaDataProto.getRecords(), generatedDependencies, protoDependencies);
    loadFromProto(metaDataProto, dependencies, processExtensionOptions);
    return this;
}
 
Example 6
Source Project: envelope   File: ProtobufUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a named {@code Message} from a {@code .desc} file into a {@code Descriptor}.
 * <p>
 * This will parse the {@code .desc} file and then extract the encapsulated
 * {@link com.google.protobuf.Descriptors.Descriptor} the designated {@link Message} from the resulting parent
 * {@link Descriptors.FileDescriptor}.
 *
 * @param filePath the location of the .desc file
 * @param messageName the name of the Message
 * @return the constructed Descriptor
 */
public static Descriptors.Descriptor buildDescriptor(String filePath, String messageName) {
  Descriptors.FileDescriptor fileDescriptor = parseFileDescriptor(filePath);
  Descriptors.Descriptor result = null;

  for (Descriptors.Descriptor descriptor : fileDescriptor.getMessageTypes()) {
    if (descriptor.getName().equals(messageName)) {
      result = descriptor;
    }
  }

  if (null == result) {
    throw new RuntimeException("Unable to locate Message '" + messageName + "' in Descriptor");
  }

  return result;
}
 
Example 7
Source Project: beam   File: ProtoDomain.java    License: Apache License 2.0 6 votes vote down vote up
private void indexOptionsByNumber(Collection<Descriptors.FileDescriptor> fileDescriptors) {
  fieldOptionMap = new HashMap<>();
  fileOptionMap = new HashMap<>();
  messageOptionMap = new HashMap<>();
  fileDescriptors.forEach(
      (fileDescriptor) -> {
        fileDescriptor
            .getExtensions()
            .forEach(
                extension -> {
                  switch (extension.toProto().getExtendee()) {
                    case ".google.protobuf.FileOptions":
                      fileOptionMap.put(extension.getNumber(), extension);
                      break;
                    case ".google.protobuf.MessageOptions":
                      messageOptionMap.put(extension.getNumber(), extension);
                      break;
                    case ".google.protobuf.FieldOptions":
                      fieldOptionMap.put(extension.getNumber(), extension);
                      break;
                    default:
                      break;
                  }
                });
      });
}
 
Example 8
Source Project: envelope   File: TestProtobufUtils.java    License: Apache License 2.0 6 votes vote down vote up
private static Descriptors.Descriptor marshalDescriptor(String desc) {
  DescriptorProtos.FileDescriptorSet fileDescriptorSet;

  try (BufferedInputStream dscFile = new BufferedInputStream(new FileInputStream(
      TestProtobufUtils.class.getResource(desc).getPath()
  ))) {
    fileDescriptorSet = DescriptorProtos.FileDescriptorSet.parseFrom(dscFile);
  } catch (Throwable throwable) {
    throw new RuntimeException("Unable to construct test resources", throwable);
  }

  // Get the attached .proto file
  DescriptorProtos.FileDescriptorProto descriptorProto = fileDescriptorSet.getFile(0);

  try {
    Descriptors.FileDescriptor fileDescriptor =  Descriptors.FileDescriptor.buildFrom(descriptorProto,
        new Descriptors.FileDescriptor[]{});
    if (fileDescriptor.getMessageTypes().isEmpty()) {
      throw new RuntimeException("No MessageTypes returned, " + fileDescriptor.getName());
    }

    return fileDescriptor.getMessageTypes().get(0);
  } catch (Descriptors.DescriptorValidationException e) {
    throw new RuntimeException("Error constructing FileDescriptor", e);
  }
}
 
Example 9
Source Project: metastore   File: ProtoLint.java    License: Apache License 2.0 6 votes vote down vote up
public void lintOnVersion(Descriptors.Descriptor ref) {
  Descriptors.FileDescriptor fileDescriptor =
      proto.getFileDescriptorByFileName(ref.getFile().getName());
  List<Descriptors.FileDescriptor> dependencies = ref.getFile().getDependencies();
  for (Descriptors.FileDescriptor dependency : dependencies) {
    String dependencyPackage = dependency.getFile().getPackage();
    String protoPackage = ref.getFile().getPackage();

    String protoVersion =
        protoPackage.substring(protoPackage.lastIndexOf(".") + 1, protoPackage.length());
    String dependencyVersion =
        dependencyPackage.substring(
            dependencyPackage.lastIndexOf(".") + 1, dependencyPackage.length());
    if (!dependencyVersion.equals(protoVersion)) {
      results.addResult(
          fileDescriptor,
          RuleInfo.newBuilder()
              .setLintRule(LintRule.LINT_PACKAGE_NO_VERSION_ALIGNMENT)
              .setCode(String.format("L%d/00", LintRule.LINT_PACKAGE_NO_VERSION_ALIGNMENT_VALUE))
              .build());
    }
  }
}
 
Example 10
Source Project: datacollector   File: ProtobufTypeUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Generates a protobuf descriptor instance from a FileDescriptor set.
 *
 * @param set               set of file descriptors
 * @param fileDescriptorMap map of message types to file descriptors
 * @param descriptorFile    descriptor file for message to be decoded
 * @param qualifiedMessageType       the name of the message to be decoded
 * @return protobuf descriptor instance
 * @throws StageException
 */
public static Descriptors.Descriptor getDescriptor(
    DescriptorProtos.FileDescriptorSet set,
    Map<String, Descriptors.FileDescriptor> fileDescriptorMap,
    String descriptorFile,
    String qualifiedMessageType
) throws StageException {

  // find the FileDescriptorProto which contains the message type
  // IF cannot find, then bail out
  String packageName = null;
  String messageType = qualifiedMessageType;
  int lastIndex = qualifiedMessageType.lastIndexOf('.');
  if (lastIndex != -1) {
    packageName = qualifiedMessageType.substring(0, lastIndex);
    messageType = qualifiedMessageType.substring(lastIndex + 1);
  }
  DescriptorProtos.FileDescriptorProto file = getFileDescProtoForMsgType(packageName, messageType, set);
  if (file == null) {
    // could not find the message type from all the proto files contained in the descriptor file
    throw new StageException(Errors.PROTOBUF_00, qualifiedMessageType, descriptorFile);
  }
  // finally get the FileDescriptor for the message type
  Descriptors.FileDescriptor fileDescriptor = fileDescriptorMap.get(file.getName());
  // create builder using the FileDescriptor
  // this can only find the top level message types
  return fileDescriptor.findMessageTypeByName(messageType);

}
 
Example 11
Source Project: karate-grpc   File: ServiceResolver.java    License: MIT License 5 votes vote down vote up
private Descriptors.ServiceDescriptor findService(String serviceName, String packageName) {
    for (Descriptors.FileDescriptor fileDescriptor : fileDescriptors) {
        if (!fileDescriptor.getPackage().equals(packageName)) {
            // Package does not match this file, ignore.
            continue;
        }

        Descriptors.ServiceDescriptor serviceDescriptor = fileDescriptor.findServiceByName(serviceName);
        if (serviceDescriptor != null) {
            return serviceDescriptor;
        }
    }
    throw new IllegalArgumentException("Can't find service with name: " + serviceName);
}
 
Example 12
Source Project: metastore   File: Convert.java    License: Apache License 2.0 5 votes vote down vote up
private static Map<String, Descriptors.FileDescriptor> convertToFileDescriptorMap(
    Map<String, DescriptorProtos.FileDescriptorProto> inMap) {
  Map<String, Descriptors.FileDescriptor> outMap = new HashMap<>();
  ExtensionRegistry registry = ExtensionRegistry.newInstance();
  inMap
      .keySet()
      .forEach(fileName -> convertToFileDescriptorMap(fileName, null, inMap, outMap, registry));
  return outMap;
}
 
Example 13
@Nonnull
private FDBMetaDataStore createMetaDataStore(@Nonnull FDBRecordContext context, @Nonnull KeySpacePath metaDataPath,
                                             @Nonnull Subspace metaDataSubspace,
                                             @Nullable Descriptors.FileDescriptor localFileDescriptor) {
    FDBMetaDataStore metaDataStore = new FDBMetaDataStore(context, metaDataPath);
    metaDataStore.setMaintainHistory(false);
    assertEquals(metaDataSubspace, metaDataStore.getSubspace());
    metaDataStore.setDependencies(new Descriptors.FileDescriptor[]{RecordMetaDataOptionsProto.getDescriptor()});
    metaDataStore.setLocalFileDescriptor(localFileDescriptor);
    return metaDataStore;
}
 
Example 14
@Test
public void writeEnumFromFile() throws Exception {
  ProtoDomain PContainer = TestSets.baseComplexMessageOptions();
  Descriptors.FileDescriptor fileDescriptor =
      PContainer.getFileDescriptorByFileName("test/v1/proto3_enum.proto");

  assertEnum(fileDescriptor.toProto(), PContainer);
}
 
Example 15
private static void addMessages(
    Map<String, Descriptors.GenericDescriptor> descriptors,
    Descriptors.FileDescriptor proto,
    String packageName) {
  for (Descriptors.Descriptor message : proto.getMessageTypes()) {
    String fullName = packageName + message.getName();
    descriptors.put(fullName, message);
  }
}
 
Example 16
Source Project: metastore   File: ProtoDomain.java    License: Apache License 2.0 4 votes vote down vote up
public static ProtoDomain buildFrom(Descriptors.FileDescriptor fileDescriptor) {
  HashMap<String, Descriptors.FileDescriptor> fileDescriptorMap = new HashMap<>();
  visitFileDescriptorTree(fileDescriptorMap, fileDescriptor);
  return buildFrom(fileDescriptorMap);
}
 
Example 17
Source Project: metastore   File: ProtoDomain.java    License: Apache License 2.0 4 votes vote down vote up
public Descriptors.FileDescriptor getFileDescriptor(String name) {
  return fileDescriptorMap.get(name);
}
 
Example 18
private void write() {
  PathLocation location = new PathLocation();
  writeLeadingComment(commentIndexer.getSyntaxLocation(), 0);
  switch (fd.getSyntax()) {
    case PROTO2:
      writer.print("syntax = \"proto2\";");
      break;
    case PROTO3:
      writer.print("syntax = \"proto3\";");
      break;
    default:
      break;
  }
  writeTrailingComment(commentIndexer.getSyntaxLocation(), 0);
  writer.println();

  if (!fd.getPackage().isEmpty()) {
    writeLeadingComment(commentIndexer.getPackageLocation(), 0);
    writer.print("package ");
    writer.print(fd.getPackage());
    writer.print(";");
    writeTrailingComment(commentIndexer.getPackageLocation(), 0);
    writer.println();
  }

  List<Descriptors.FileDescriptor> dependencies = fd.getDependencies();
  if (dependencies.size() > 0) {
    int index = 0;
    for (Descriptors.FileDescriptor dependency : dependencies) {
      writeLeadingComment(commentIndexer.importLocations.get(index++), 0);
      writer.print("import \"");
      writer.print(dependency.getName());
      writer.print("\";");
      writeTrailingComment(commentIndexer.importLocations.get(index++), 0);
    }
    writer.println();
  }

  writeOptionsForBlock(fd.getOptions(), 0, "File");

  extensions();

  for (Descriptors.EnumDescriptor enumDescriptor : fd.getEnumTypes()) {
    writer.println();
    writeEnumDescriptor(enumDescriptor, location, 0);
  }

  for (Descriptors.ServiceDescriptor serviceDescriptor : fd.getServices()) {
    writer.println();
    writeServiceDescriptor(serviceDescriptor, location);
  }

  for (Descriptors.Descriptor messageType : fd.getMessageTypes()) {
    writer.println();
    writeMessageDescriptor(messageType, location, 0);
  }
}
 
Example 19
Source Project: datacollector   File: ProtobufTypeUtil.java    License: Apache License 2.0 4 votes vote down vote up
private static Set<Descriptors.FileDescriptor> getDependencies(
    Map<String, Set<Descriptors.FileDescriptor>> dependenciesMap,
    Map<String, Descriptors.FileDescriptor> fileDescriptorMap,
    DescriptorProtos.FileDescriptorProto file,
    DescriptorProtos.FileDescriptorSet set
) throws StageException {
  Set<Descriptors.FileDescriptor> result = new LinkedHashSet<>();
  for (String name : file.getDependencyList()) {
    DescriptorProtos.FileDescriptorProto fileDescriptorProto = null;
    for (DescriptorProtos.FileDescriptorProto fdp : set.getFileList()) {
      if (name.equals(fdp.getName())) {
        fileDescriptorProto = fdp;
        break;
      }
    }
    if (fileDescriptorProto == null) {
      // could not find the message type from all the proto files contained in the descriptor file
      throw new StageException(Errors.PROTOBUF_01, file.getName());
    }
    Descriptors.FileDescriptor fileDescriptor;
    if (fileDescriptorMap.containsKey(fileDescriptorProto.getName())) {
      fileDescriptor = fileDescriptorMap.get(fileDescriptorProto.getName());
    } else {
      Set<Descriptors.FileDescriptor> deps = new LinkedHashSet<>();
      if (dependenciesMap.containsKey(name)) {
        deps.addAll(dependenciesMap.get(name));
      } else {
        deps.addAll(getDependencies(dependenciesMap, fileDescriptorMap, fileDescriptorProto, set));
      }
      try {
        fileDescriptor = Descriptors.FileDescriptor.buildFrom(
            fileDescriptorProto,
            deps.toArray(new Descriptors.FileDescriptor[deps.size()])
        );
      } catch (Descriptors.DescriptorValidationException e) {
        throw new StageException(Errors.PROTOBUF_07, e.getDescription(), e);
      }
    }
    result.add(fileDescriptor);
  }
  return result;
}
 
Example 20
Source Project: rejoiner   File: SchemaBundle.java    License: Apache License 2.0 votes vote down vote up
public abstract ImmutableSet.Builder<Descriptors.FileDescriptor> fileDescriptorsBuilder();