com.google.protobuf.SourceContext Java Examples

The following examples show how to use com.google.protobuf.SourceContext. 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: DescriptorNormalizer.java    From api-compiler with Apache License 2.0 7 votes vote down vote up
@VisitsBefore
void normalize(EnumType enumType) {
  Enum.Builder coreEnumBuilder = Enum.newBuilder().setName(enumType.getFullName());
  coreEnumBuilder.setSourceContext(
      SourceContext.newBuilder()
          .setFileName(enumType.getFile().getLocation().getDisplayString()));
  coreEnumBuilder.setSyntax(enumType.getSyntax());

  for (EnumValue value : enumType.getReachableValues()) {
    com.google.protobuf.EnumValue.Builder coreEnumValueBuilder =
        com.google.protobuf.EnumValue.newBuilder();

    // Use simple name for enum value, as otherwise client has to use
    // fully qualified name in the request.
    coreEnumValueBuilder.setName(value.getSimpleName()).setNumber(value.getNumber());

    coreEnumValueBuilder.addAllOptions(
        DescriptorNormalization.getOptions(value.getProto(), includeDefaults));
    coreEnumBuilder.addEnumvalue(coreEnumValueBuilder.build());
  }

  coreEnumBuilder.addAllOptions(
      DescriptorNormalization.getOptions(enumType.getProto(), includeDefaults));

  enums.add(coreEnumBuilder.build());
}
 
Example #2
Source File: TypesBuilderFromDescriptor.java    From api-compiler with Apache License 2.0 6 votes vote down vote up
/**
 * TODO (guptasu): Only needed to create NullValue enum.  Check if this can be removed.
 * Create the Protobuf.Enum instance from enumDescriptorProto.
 */
private static Enum createEnum(String enumName,
    EnumDescriptorProto enumDescriptorProto, String fileName) {

  com.google.protobuf.Enum.Builder coreEnumBuilder =
      com.google.protobuf.Enum.newBuilder().setName(enumName);

  coreEnumBuilder.setSyntax(Syntax.SYNTAX_PROTO3);

  for (EnumValueDescriptorProto value : enumDescriptorProto.getValueList()) {
    com.google.protobuf.EnumValue.Builder coreEnumValueBuilder =
        com.google.protobuf.EnumValue.newBuilder();
    coreEnumValueBuilder.setName(value.getName()).setNumber(value.getNumber());
    coreEnumBuilder.addEnumvalue(coreEnumValueBuilder.build());
  }

  coreEnumBuilder.setSourceContext(SourceContext.newBuilder().setFileName(fileName));
  return coreEnumBuilder.build();
}
 
Example #3
Source File: DescriptorGenerator.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
private FileContents getFileContents(SourceContext sourceContext) {
  String fileName = sourceContext.getFileName();
  if (!contentsByFile.containsKey(fileName)) {
    contentsByFile.put(fileName, new FileContents());
  }
  return contentsByFile.get(fileName);
}
 
Example #4
Source File: DescriptorNormalizer.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
@VisitsBefore
void normalize(Interface iface) {
  Api.Builder coreApiBuilder = Api.newBuilder().setName(iface.getFullName());
  coreApiBuilder.setSourceContext(
      SourceContext.newBuilder().setFileName(iface.getFile().getLocation().getDisplayString()));
  coreApiBuilder.setSyntax(iface.getSyntax());

  for (Method method : iface.getReachableMethods()) {
    com.google.protobuf.Method.Builder coreMethodBuilder =
        com.google.protobuf.Method.newBuilder()
            .setName(method.getSimpleName())
            .setRequestTypeUrl(generateTypeUrl(method.getInputType()))
            .setResponseTypeUrl(generateTypeUrl(method.getOutputType()));

    coreMethodBuilder.setRequestStreaming(method.getRequestStreaming());
    coreMethodBuilder.setResponseStreaming(method.getResponseStreaming());
    coreMethodBuilder.addAllOptions(
        DescriptorNormalization.getMethodOptions(
            method.getOptionFields(),
            false,
            includeDefaults));
    coreApiBuilder.addMethods(coreMethodBuilder);
  }

  coreApiBuilder.addAllOptions(
      DescriptorNormalization.getOptions(iface.getProto(), includeDefaults));
  coreApiBuilder.setVersion(iface.getAttribute(VersionAttribute.KEY).majorVersion());
  apis.add(coreApiBuilder.build());
}
 
Example #5
Source File: TypesBuilderFromDescriptor.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * TODO (guptasu): only needed to create hard coded Types (Struct, ListValue, and Value). Check
 * if this can be removed. Create the Protobuf.Type instance from descriptorProto.
 */
private static Type createType(String typeName, DescriptorProto descriptorProto,
    String fileName) {
  Type.Builder coreTypeBuilder = Type.newBuilder().setName(typeName);

  int count = 1;
  for (FieldDescriptorProto fieldProto : descriptorProto.getFieldList()) {
    Field.Kind fieldKind = Field.Kind.valueOf(fieldProto.getType().getNumber());
    Cardinality cardinality = Cardinality.CARDINALITY_OPTIONAL;
    if (fieldProto.getLabel() == Label.LABEL_REPEATED) {
      cardinality = Cardinality.CARDINALITY_REPEATED;
    }
    Field.Builder coreFieldBuilder = Field
        .newBuilder()
        .setName(fieldProto.getName())
        .setNumber(count++)
        .setKind(fieldKind)
        .setCardinality(cardinality);
    if (fieldKind == Kind.TYPE_MESSAGE || fieldKind == Kind.TYPE_ENUM) {
      String typeFullName =
          fieldProto.getTypeName().startsWith(".") ? fieldProto.getTypeName().substring(1)
              : fieldProto.getTypeName();
      coreFieldBuilder.setTypeUrl(TYPE_SERVICE_BASE_URL + typeFullName);
    }
    coreTypeBuilder.addFields(coreFieldBuilder.build());
  }
  coreTypeBuilder.setSourceContext(SourceContext.newBuilder().setFileName(fileName));
  coreTypeBuilder.setSyntax(Syntax.SYNTAX_PROTO3);
  return coreTypeBuilder.build();
}
 
Example #6
Source File: Convert.java    From metastore with Apache License 2.0 4 votes vote down vote up
private static Descriptors.FileDescriptor convertToFileDescriptorMap(
    String name,
    String parent,
    Map<String, DescriptorProtos.FileDescriptorProto> inMap,
    Map<String, Descriptors.FileDescriptor> outMap,
    ExtensionRegistry extensionRegistry) {
  if (outMap.containsKey(name)) {
    return outMap.get(name);
  }
  Descriptors.FileDescriptor fd;
  switch (name) {
    case "google/protobuf/descriptor.proto":
      fd = DescriptorProtos.FieldOptions.getDescriptor().getFile();
      break;
    case "google/protobuf/wrappers.proto":
      fd = Int32Value.getDescriptor().getFile();
      break;
    case "google/protobuf/timestamp.proto":
      fd = Timestamp.getDescriptor().getFile();
      break;
    case "google/protobuf/duration.proto":
      fd = Duration.getDescriptor().getFile();
      break;
    case "google/protobuf/any.proto":
      fd = Any.getDescriptor().getFile();
      break;
    case "google/protobuf/api.proto":
      fd = Api.getDescriptor().getFile();
      break;
    case "google/protobuf/empty.proto":
      fd = Empty.getDescriptor().getFile();
      break;
    case "google/protobuf/field_mask.proto":
      fd = FieldMask.getDescriptor().getFile();
      break;
    case "google/protobuf/source_context.proto":
      fd = SourceContext.getDescriptor().getFile();
      break;
    case "google/protobuf/struct.proto":
      fd = Struct.getDescriptor().getFile();
      break;
    case "google/protobuf/type.proto":
      fd = Type.getDescriptor().getFile();
      break;
    default:
      DescriptorProtos.FileDescriptorProto fileDescriptorProto = inMap.get(name);
      if (fileDescriptorProto == null) {
        if (parent == null) {
          throw new IllegalArgumentException(
              String.format("Couldn't find file \"%1s\" in file descriptor set", name));
        }
        throw new IllegalArgumentException(
            String.format("Couldn't find file \"%1s\", imported by \"%2s\"", name, parent));
      }
      List<Descriptors.FileDescriptor> dependencies = new ArrayList<>();
      if (fileDescriptorProto.getDependencyCount() > 0) {
        fileDescriptorProto
            .getDependencyList()
            .forEach(
                dependencyName ->
                    dependencies.add(
                        convertToFileDescriptorMap(
                            dependencyName, name, inMap, outMap, extensionRegistry)));
      }
      try {
        fd =
            Descriptors.FileDescriptor.buildFrom(
                fileDescriptorProto, dependencies.toArray(new Descriptors.FileDescriptor[0]));

      } catch (Descriptors.DescriptorValidationException e) {
        throw new RuntimeException(e);
      }
  }
  outMap.put(name, fd);
  return fd;
}
 
Example #7
Source File: DescriptorNormalizer.java    From api-compiler with Apache License 2.0 4 votes vote down vote up
@VisitsBefore
void normalize(MessageType message) {
  Type.Builder coreTypeBuilder = Type.newBuilder().setName(message.getFullName());
  coreTypeBuilder.setSourceContext(
      SourceContext.newBuilder().setFileName(message.getFile().getLocation().getDisplayString()));
  coreTypeBuilder.setSyntax(message.getSyntax());

  for (Field field : message.getReachableFields()) {
    com.google.protobuf.Field.Builder coreFieldBuilder =
        com.google.protobuf.Field.newBuilder()
            .setName(field.getSimpleName())
            .setNumber(field.getNumber())
            .setKind(toCoreFieldKind(field.getProto()))
            .setCardinality(toCoreFieldCardinality(field.getProto()))
            .setJsonName(field.getJsonName());

    if (field.getType().isEnum() || field.getType().isMessage()) {
      coreFieldBuilder.setTypeUrl(generateTypeUrl(field.getType()));
    }

    FieldDescriptorProto proto = field.getProto();

    if (proto.hasOneofIndex()) {
      // Index in the containing type's oneof_decl is zero-based.
      // Index in google.protobuf.type.Field.oneof_index is one-based.
      coreFieldBuilder.setOneofIndex(field.getProto().getOneofIndex() + 1);
    }
    if (proto.getOptions().hasPacked()) {
      coreFieldBuilder.setPacked(proto.getOptions().getPacked());
    } else if (isDefaultPackedEncoding(field)) {
      coreFieldBuilder.setPacked(true);
    }
    if (proto.hasDefaultValue()) {
      coreFieldBuilder.setDefaultValue(proto.getDefaultValue());
    }
    coreFieldBuilder.addAllOptions(
        DescriptorNormalization.getOptions(field.getProto(), includeDefaults));
    coreTypeBuilder.addFields(coreFieldBuilder.build());
  }

  coreTypeBuilder.addAllOptions(
      DescriptorNormalization.getOptions(message.getProto(), includeDefaults));
  coreTypeBuilder.addAllOneofs(DescriptorNormalization.getOneofs(message.getProto()));
  types.add(coreTypeBuilder.build());
}