Java Code Examples for com.google.protobuf.DescriptorProtos.FieldDescriptorProto#getTypeName()

The following examples show how to use com.google.protobuf.DescriptorProtos.FieldDescriptorProto#getTypeName() . 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: 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 2
Source File: ProtobufDecompiler.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void decompileFields(List<FieldDescriptorProto> fieldDescriptors,
                               Map<String,DescriptorProto> groups)
        throws IOException {
    for (FieldDescriptorProto fieldDescriptor : fieldDescriptors) {
        String label = LABELS.get(fieldDescriptor.getLabel());
        String type = TYPES.get(fieldDescriptor.getType());
        String name = fieldDescriptor.getName();
        if (fieldDescriptor.hasTypeName()) {
            type = fieldDescriptor.getTypeName();
            if ((absolutePackage != null) && type.startsWith(absolutePackage)) {
                type = type.substring(absolutePackage.length());
            }
        }
        DescriptorProto groupDescriptor = null;
        if (fieldDescriptor.getType() == Type.TYPE_GROUP) {
            groupDescriptor = groups.get(type);
            if (groupDescriptor != null) {
                name = type;
                type = "group";
            }
        }
        indentedFormat("%s %s %s = %d",
                       label, type, name, fieldDescriptor.getNumber());
        if (fieldDescriptor.hasOptions() || fieldDescriptor.hasDefaultValue()) {
            write(defaultAndOptions(fieldDescriptor.hasOptions() ? fieldDescriptor.getOptions() : null,
                                    fieldDescriptor.hasDefaultValue() ? fieldDescriptor.getDefaultValue() : null));
        }
        if (groupDescriptor == null) {
            write(";");
        }
        else {
            decompileMessageBody(groupDescriptor);
        }
    }
}