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

The following examples show how to use com.google.protobuf.Descriptors#GenericDescriptor . 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: ProtoDiff.java    From metastore with Apache License 2.0 6 votes vote down vote up
private <T extends Descriptors.GenericDescriptor> void diffGenericDescriptor(
    List<T> mt_ref,
    List<T> mt_new,
    Consumer<T> removal,
    Consumer<T> addition,
    BiConsumer<T, T> diff) {
  Map<String, T> m_ref = toMap4Descriptor(mt_ref);
  Map<String, T> m_new = toMap4Descriptor(mt_new);

  Set<String> onlyRef = onlyInLeft(m_ref, m_new);
  onlyRef.forEach(k -> removal.accept(m_ref.get(k)));

  Set<String> onlyNew = onlyInLeft(m_new, m_ref);
  onlyNew.forEach(k -> addition.accept(m_new.get(k)));

  Set<String> common = onlyInCommon(m_new, m_ref);
  common.forEach(k -> diff.accept(m_ref.get(k), m_new.get(k)));
}
 
Example 2
Source File: JsonModule.java    From stateful-functions with Apache License 2.0 6 votes vote down vote up
private static Router<Message> dynamicRouter(JsonNode router) {
  String addressTemplate = Selectors.textAt(router, Pointers.Routers.SPEC_TARGET);
  String descriptorSetPath = Selectors.textAt(router, Pointers.Routers.SPEC_DESCRIPTOR);
  String messageType = Selectors.textAt(router, Pointers.Routers.SPEC_MESSAGE_TYPE);

  ProtobufDescriptorMap descriptorPath = protobufDescriptorMap(descriptorSetPath);
  Optional<Descriptors.GenericDescriptor> maybeDescriptor =
      descriptorPath.getDescriptorByName(messageType);
  if (!maybeDescriptor.isPresent()) {
    throw new IllegalStateException(
        "Error while processing a router definition. Unable to locate a message "
            + messageType
            + " in a descriptor set "
            + descriptorSetPath);
  }
  return ProtobufRouter.forAddressTemplate(
      (Descriptors.Descriptor) maybeDescriptor.get(), addressTemplate);
}
 
Example 3
Source File: Renderer.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@Inject
Renderer(
    Provider<SoyFileSet.Builder> filesetBuilderProvider,
    ImmutableSet<Descriptors.GenericDescriptor> descriptors,
    JsonRenderer jsonRenderer) {
  this.filesetBuilderProvider = filesetBuilderProvider;
  this.tofu =
      filesetBuilderProvider
          .get()
          .add(Renderer.class.getResource("resources/types.soy"))
          .add(Renderer.class.getResource("resources/dossier.soy"))
          .addProtoDescriptors(descriptors)
          .build()
          .compileToTofu();
  this.jsonRenderer = jsonRenderer;
}
 
Example 4
Source File: RouterJsonEntity.java    From flink-statefun with Apache License 2.0 6 votes vote down vote up
private static Router<Message> dynamicRouter(JsonNode router) {
  String addressTemplate = Selectors.textAt(router, SpecPointers.TARGET);
  String descriptorSetPath = Selectors.textAt(router, SpecPointers.DESCRIPTOR);
  String messageType = Selectors.textAt(router, SpecPointers.MESSAGE_TYPE);

  ProtobufDescriptorMap descriptorPath = protobufDescriptorMap(descriptorSetPath);
  Optional<Descriptors.GenericDescriptor> maybeDescriptor =
      descriptorPath.getDescriptorByName(messageType);
  if (!maybeDescriptor.isPresent()) {
    throw new IllegalStateException(
        "Error while processing a router definition. Unable to locate a message "
            + messageType
            + " in a descriptor set "
            + descriptorSetPath);
  }
  return ProtobufRouter.forAddressTemplate(
      (Descriptors.Descriptor) maybeDescriptor.get(), addressTemplate);
}
 
Example 5
Source File: DossierSoyModule.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private static void collectDescriptors(
    Set<Descriptors.GenericDescriptor> set, Descriptors.Descriptor descriptor) {
  if (!set.add(descriptor)) {
    return;
  }

  set.addAll(descriptor.getEnumTypes());
  for (Descriptors.FieldDescriptor field : descriptor.getFields()) {
    if (field.getJavaType() == Descriptors.FieldDescriptor.JavaType.MESSAGE) {
      collectDescriptors(set, field.getMessageType());
    }
  }
}
 
Example 6
Source File: ProtoDiff.java    From metastore with Apache License 2.0 5 votes vote down vote up
private <T extends Descriptors.GenericDescriptor> Map<String, T> toMap4Descriptor(List<T> in) {
  Map<String, T> out = new HashMap<>();
  in.forEach(
      descriptor -> {
        out.put(descriptor.getName(), descriptor);
      });
  return out;
}
 
Example 7
Source File: DossierSoyModule.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private static Set<Descriptors.GenericDescriptor> collectDescriptors(
    Descriptors.FileDescriptor... fileDescriptors) {
  Set<Descriptors.GenericDescriptor> set = new HashSet<>();
  for (Descriptors.FileDescriptor file : fileDescriptors) {
    set.addAll(file.getEnumTypes());
    for (Descriptors.Descriptor descriptor : file.getMessageTypes()) {
      collectDescriptors(set, descriptor);
    }
  }
  return set;
}
 
Example 8
Source File: DossierSoyModule.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
@Provides
@Singleton
ImmutableSet<Descriptors.GenericDescriptor> provideProtoDescriptors() {
  return ImmutableSet.copyOf(
      collectDescriptors(
          DescriptorProtos.getDescriptor(), Dossier.getDescriptor(), Expression.getDescriptor()));
}
 
Example 9
Source File: ProtobufDescriptorMap.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
private static void addEnums(
    Map<String, Descriptors.GenericDescriptor> descriptors,
    Descriptors.FileDescriptor descriptor,
    String packageName) {
  for (Descriptors.EnumDescriptor message : descriptor.getEnumTypes()) {
    String fullName = packageName + message.getName();
    descriptors.put(fullName, message);
  }
}
 
Example 10
Source File: ProtobufDescriptorMap.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
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 11
Source File: ProtobufDescriptorMap.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
public static ProtobufDescriptorMap from(DescriptorProtos.FileDescriptorSet fileDescriptorSet) {
  Map<String, Descriptors.FileDescriptor> resolvedSet =
      FileDescriptorResolver.resolve(fileDescriptorSet);

  Map<String, Descriptors.GenericDescriptor> messageOrEnumDescriptors = new HashMap<>();

  for (Descriptors.FileDescriptor fileDescriptor : resolvedSet.values()) {
    addMessages(messageOrEnumDescriptors, fileDescriptor, packageName(fileDescriptor));
    addEnums(messageOrEnumDescriptors, fileDescriptor, packageName(fileDescriptor));
  }
  return new ProtobufDescriptorMap(messageOrEnumDescriptors);
}
 
Example 12
Source File: ProtobufDescriptorMap.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
private static void addEnums(
    Map<String, Descriptors.GenericDescriptor> descriptors,
    Descriptors.FileDescriptor descriptor,
    String packageName) {
  for (Descriptors.EnumDescriptor message : descriptor.getEnumTypes()) {
    String fullName = packageName + message.getName();
    descriptors.put(fullName, message);
  }
}
 
Example 13
Source File: ProtobufDescriptorMap.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
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 14
Source File: ProtobufDescriptorMap.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
public static ProtobufDescriptorMap from(DescriptorProtos.FileDescriptorSet fileDescriptorSet) {
  Map<String, Descriptors.FileDescriptor> resolvedSet =
      FileDescriptorResolver.resolve(fileDescriptorSet);

  Map<String, Descriptors.GenericDescriptor> messageOrEnumDescriptors = new HashMap<>();

  for (Descriptors.FileDescriptor fileDescriptor : resolvedSet.values()) {
    addMessages(messageOrEnumDescriptors, fileDescriptor, packageName(fileDescriptor));
    addEnums(messageOrEnumDescriptors, fileDescriptor, packageName(fileDescriptor));
  }
  return new ProtobufDescriptorMap(messageOrEnumDescriptors);
}
 
Example 15
Source File: ProtobufDescriptorMap.java    From flink-statefun with Apache License 2.0 4 votes vote down vote up
public Optional<Descriptors.GenericDescriptor> getDescriptorByName(String messageFullName) {
  Descriptors.GenericDescriptor descriptor = descriptorByName.get(messageFullName);
  return Optional.ofNullable(descriptor);
}
 
Example 16
Source File: ProtobufDescriptorMap.java    From flink-statefun with Apache License 2.0 4 votes vote down vote up
private ProtobufDescriptorMap(Map<String, Descriptors.GenericDescriptor> descriptorByName) {
  this.descriptorByName = descriptorByName;
}
 
Example 17
Source File: Renderer.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {
  checkArgument(args.length > 0, "no output directory specified");

  Path outputDir = FileSystems.getDefault().getPath(args[0]);
  checkArgument(Files.isDirectory(outputDir), "not a directory: %s", outputDir);

  Injector injector = Guice.createInjector(new DossierSoyModule());

  ImmutableSet<Descriptors.GenericDescriptor> descriptors =
      injector.getInstance(
          Key.get(new TypeLiteral<ImmutableSet<Descriptors.GenericDescriptor>>() {}));
  SoyFileSet fileSet =
      injector
          .getInstance(SoyFileSet.Builder.class)
          .add(Renderer.class.getResource("resources/dossier.soy"))
          .add(Renderer.class.getResource("resources/nav.soy"))
          .add(Renderer.class.getResource("resources/types.soy"))
          .addProtoDescriptors(descriptors)
          .build();

  SoyJsSrcOptions options = new SoyJsSrcOptions();

  options.setShouldGenerateGoogModules(true);

  Pattern googModulePattern = Pattern.compile("(goog\\.module\\('.*'\\);)");
  String missingContent =
      "\n/** @suppress {extraRequire} */\n"
          + "goog.require('dossier.soyplugins');\n"
          + "/** @suppress {extraRequire} */\n"
          + "goog.require('goog.soy.data.SanitizedContent');\n";

  Iterator<Path> files =
      ImmutableList.of(
              outputDir.resolve("dossier.soy.js"),
              outputDir.resolve("nav.soy.js"),
              outputDir.resolve("types.soy.js"))
          .iterator();
  for (String string : fileSet.compileToJsSrc(options, null)) {
    Matcher matcher = googModulePattern.matcher(string);
    if (matcher.find()) {
      string = matcher.replaceFirst("$1\n" + missingContent);
    }

    Path file = files.next();
    Files.write(file, string.getBytes(StandardCharsets.UTF_8));
  }
}
 
Example 18
Source File: ProtobufDescriptorMap.java    From stateful-functions with Apache License 2.0 4 votes vote down vote up
public Optional<Descriptors.GenericDescriptor> getDescriptorByName(String messageFullName) {
  Descriptors.GenericDescriptor descriptor = descriptorByName.get(messageFullName);
  return Optional.ofNullable(descriptor);
}
 
Example 19
Source File: ProtobufDescriptorMap.java    From stateful-functions with Apache License 2.0 4 votes vote down vote up
private ProtobufDescriptorMap(Map<String, Descriptors.GenericDescriptor> descriptorByName) {
  this.descriptorByName = descriptorByName;
}