com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Type Java Examples

The following examples show how to use com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Type. 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: ProtobufDecompiler.java    From sql-layer with GNU Affero General Public License v3.0 6 votes vote down vote up
protected void decompileOptions(MessageOrBuilder options) throws IOException {
    for (Map.Entry<FieldDescriptor,Object> entry : options.getAllFields().entrySet()) {
        FieldDescriptor field = entry.getKey();
        Object value = entry.getValue();
        String fieldName = field.getName();
        if (field.isExtension()) {
            fieldName = "(" + fieldName + ")";
        }
        if (field.getType() == FieldDescriptor.Type.MESSAGE) {
            for (Map.Entry<FieldDescriptor,Object> subentry : ((MessageOrBuilder)value).getAllFields().entrySet()) {
                FieldDescriptor subfield = subentry.getKey();
                Object subvalue = subentry.getValue();
                indentedFormat("option %s.%s = %s;", fieldName, subfield.getName(), literal(subvalue, subfield.getType()));
            }
        }
        else {
            indentedFormat("option %s = %s;", fieldName, literal(value, field.getType()));
        }
    }
}
 
Example #2
Source File: Merger.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Resolve the additional type specified besides those that can be reached transitively from
 * service definition. It resolves the typeName into a {@link TypeRef} object. If typeName ends
 * with wildcard ".*", all the {@link TypeRef}s that is under typeName pattern path are added to
 * the root.
 */
private void addAdditionalType(
    Model model, Location location, final String typeName, final Type kind) {
  if (!SELECTOR_PATTERN.matcher(typeName).matches()) {
    model
        .getDiagReporter()
        .report(
            Diag.error(
                location,
                "Type selector '%s' specified in the config has bad syntax. "
                    + "Valid format is \"<segment>('.' <segment>)*('.' '*')?\"",
                typeName));
    return;
  }

  List<TypeRef> typeRefs = model.getSymbolTable().lookupMatchingTypes(typeName, kind);

  if (typeRefs == null || typeRefs.isEmpty()) {
    model
        .getDiagReporter()
        .report(
            Diag.error(
                location,
                "Cannot resolve additional %s type '%s' specified in the config.",
                kind,
                typeName));
  } else {
    for (TypeRef typeRef : typeRefs) {
      if (typeRef.isMessage()) {
        model.addRoot(typeRef.getMessageType());
      } else if (typeRef.isEnum()) {
        model.addRoot(typeRef.getEnumType());
      }
    }
  }
}
 
Example #3
Source File: AISToProtobuf.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void addChildTable(Table table) {
    String fieldName = uniqueIdent(ident(table.getName().getTableName(), false), fieldNames);
    fieldBuilder = messageBuilder.addFieldBuilder();
    fieldBuilder.setName(fieldName);
    fieldBuilder.setLabel(Label.LABEL_REPEATED);
    fieldBuilder.setType(Type.TYPE_MESSAGE);
    fieldBuilder.setTypeName(tableMessageNames.get(table));
    FieldOptions.Builder fieldBuilderOptions = FieldOptions.newBuilder();
    ColumnOptions.Builder columnOptions = ColumnOptions.newBuilder();
    columnOptions.setUuid(table.getUuid().toString());
    priorField = null;
    if (priorMessage != null) {
        for (FieldDescriptorProto field : priorMessage.getFieldList()) {
            FieldOptions options = field.getOptions();
            if ((options != null) &&
                (options.hasExtension(ColumnOptions.fdbsql))) {
                ColumnOptions coptions = options.getExtension(ColumnOptions.fdbsql);
                if (coptions.getUuid().equals(columnOptions.getUuid())) {
                    priorField = field;
                    break;
                }
            }
        }
    }
    setFieldNumber();
    fieldBuilderOptions.setExtension(ColumnOptions.fdbsql, columnOptions.build());
    fieldBuilder.setOptions(fieldBuilderOptions);
}
 
Example #4
Source File: AISToProtobuf.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void addNullForField(String columnName, int forField) {
    String fieldName = uniqueIdent("_" + ident(columnName, false) + "_is_null", fieldNames);
    fieldBuilder = messageBuilder.addFieldBuilder();
    fieldBuilder.setName(fieldName);
    fieldBuilder.setType(Type.TYPE_BOOL);
    fieldBuilder.setLabel(Label.LABEL_OPTIONAL);
    FieldOptions.Builder fieldBuilderOptions = FieldOptions.newBuilder();
    ColumnOptions.Builder columnOptions = ColumnOptions.newBuilder();
    columnOptions.setNullForField(forField);
    priorField = null;
    if (priorMessage != null) {
        for (FieldDescriptorProto field : priorMessage.getFieldList()) {
            FieldOptions options = field.getOptions();
            if ((options != null) &&
                (options.hasExtension(ColumnOptions.fdbsql))) {
                ColumnOptions coptions = options.getExtension(ColumnOptions.fdbsql);
                if (coptions.hasNullForField() &&
                    (coptions.getNullForField() == forField)) {
                    priorField = field;
                    break;
                }
            }
        }
    }
    setFieldNumber();
    fieldBuilderOptions.setExtension(ColumnOptions.fdbsql, columnOptions.build());
    fieldBuilder.setOptions(fieldBuilderOptions);
}
 
Example #5
Source File: ProtobufDecompiler.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
protected String literal(Object value, FieldDescriptor.Type type) {
    switch (type) {
    case STRING:
        return quotedString((String)value);
    default:
        return value.toString();
    }
}
 
Example #6
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);
        }
    }
}
 
Example #7
Source File: ProtobufDecompiler.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
protected void findGroups(List<FieldDescriptorProto> fieldDescriptors,
                          Map<String,DescriptorProto> groups) {
    for (FieldDescriptorProto fieldDescriptor : fieldDescriptors) {
        if (fieldDescriptor.getType() == Type.TYPE_GROUP) {
            groups.put(fieldDescriptor.getTypeName(), null);
        }
    }
}
 
Example #8
Source File: SymbolTableTest.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
@Test public void testResolveType2() {
  Mockito.when(message1.getFullName()).thenReturn(m1Name);
  Mockito.when(message2.getFullName()).thenReturn(m2Name);
  Mockito.when(message3.getFullName()).thenReturn(m3Name);
  Mockito.when(message4.getFullName()).thenReturn(m4Name);

  Assert.assertSame(TypeRef.of(Type.TYPE_INT32), table.resolveType("a.b",  "int32"));
  Assert.assertSame(m1, table.resolveType2("a.b",  "m"));
  Assert.assertSame(m2, table.resolveType2("a.b",  "m.m"));
  Assert.assertSame(m2, table.resolveType2("a.b.m",  "m"));
  Assert.assertSame(m1, table.resolveType2("a.b.m",  ".a.b.m"));
  Assert.assertSame(m4, table.resolveType2("a.b.a",  "a.n"));
  Assert.assertNull(table.resolveType2("a.b.a",  "a.m")); // Note: different from resolveType()
}
 
Example #9
Source File: SymbolTableTest.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
@Test public void testResolveType() {
  Assert.assertSame(TypeRef.of(Type.TYPE_INT32), table.resolveType("a.b",  "int32"));
  Assert.assertSame(m1, table.resolveType("a.b",  "m"));
  Assert.assertSame(m2, table.resolveType("a.b",  "m.m"));
  Assert.assertSame(m2, table.resolveType("a.b.m",  "m"));
  Assert.assertSame(m1, table.resolveType("a.b.m",  ".a.b.m"));
  Assert.assertSame(m3, table.resolveType("a.b.a",  "a.m")); // Note: different from resolveType()
}
 
Example #10
Source File: TypeRef.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
private TypeRef(Type protoType, Cardinality card,
    @Nullable MessageType messageType, @Nullable EnumType enumType) {
  this.kind = Preconditions.checkNotNull(protoType);
  this.card = Preconditions.checkNotNull(card);
  this.messageType = messageType;
  this.enumType = enumType;
}
 
Example #11
Source File: ProtoDescriptorSerializer.java    From milkman with MIT License 4 votes vote down vote up
private String getType(Type type) {
	switch (type) {
	case TYPE_BOOL:
		return "bool";
	case TYPE_BYTES:
		return "bytes";
	case TYPE_DOUBLE:
		return "double";
	case TYPE_ENUM:
		return "enum";
	case TYPE_FIXED32:
		return "fixed32";
	case TYPE_FIXED64:
		return "fixed64";
	case TYPE_FLOAT:
		return "float";
	case TYPE_GROUP:
		return "group";
	case TYPE_INT32:
		return "int32";
	case TYPE_INT64:
		return "int64";
	case TYPE_MESSAGE:
		return "message";
	case TYPE_SFIXED32:
		return "sfixed32";
	case TYPE_SFIXED64:
		return "sfixed64";
	case TYPE_SINT32:
		return "sint32";
	case TYPE_SINT64:
		return "sint64";
	case TYPE_STRING:
		return "string";
	case TYPE_UINT32:
		return "uint32";
	case TYPE_UINT64:
		return "uint64";
	default:
		break;
	
	
	}
	throw new IllegalArgumentException("Unknown type: " + type);
}
 
Example #12
Source File: ProtobufRowConversion.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Type getType() {
    return Type.TYPE_BYTES;
}
 
Example #13
Source File: ProtobufRowConversion.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Type getType() {
    return Type.TYPE_SINT64;
}
 
Example #14
Source File: ProtobufRowConversion.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Type getType() {
    return Type.TYPE_SINT32;
}
 
Example #15
Source File: ProtobufRowConversion.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Type getType() {
    return Type.TYPE_SINT32;
}
 
Example #16
Source File: ProtobufRowConversion.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Type getType() {
    return Type.TYPE_STRING;
}
 
Example #17
Source File: ProtobufRowConversion.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Type getType() {
    return Type.TYPE_STRING;
}
 
Example #18
Source File: ProtobufRowConversion.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Type getType() {
    return Type.TYPE_BYTES;
}
 
Example #19
Source File: ProtobufRowConversion.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Type getType() {
    return Type.TYPE_BYTES;
}
 
Example #20
Source File: ProtobufRowConversion.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Type getType() {
    return type;
}
 
Example #21
Source File: ProtobufRowConversion.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
public IntegerConversion(Type type, UnderlyingType underlying) {
    this.type = type;
    this.underlying = underlying;
}
 
Example #22
Source File: ProtobufRowConversion.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Type getType() {
    return type;
}
 
Example #23
Source File: ProtobufRowConversion.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
public CompatibleConversion(Type type, UnderlyingType underlying) {
    this.type = type;
    this.underlying = underlying;
}
 
Example #24
Source File: ProtobufRowConversion.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
/** Get the Protobuf field type. */
public abstract Type getType();
 
Example #25
Source File: TypeRef.java    From api-compiler with Apache License 2.0 4 votes vote down vote up
/**
 * Return true of this is a cyclic message type.
 */
public boolean isCyclic() {
  return getKind() == Type.TYPE_MESSAGE && getMessageType().isCyclic();
}
 
Example #26
Source File: TypeRef.java    From api-compiler with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a reference to a primitive type, with default cardinality optional.
 */
public static TypeRef of(Type primitiveType) {
  Preconditions.checkArgument(
      primitiveType != Type.TYPE_MESSAGE && primitiveType != Type.TYPE_ENUM);
  return interner.intern(new TypeRef(primitiveType, Cardinality.OPTIONAL, null, null));
}
 
Example #27
Source File: AISToProtobuf.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
public AISToProtobuf(ProtobufRowFormat.Type formatType, FileDescriptorSet priorSet) {
    this.formatType = formatType;
    this.priorSet = priorSet;
    setBuilder = FileDescriptorSet.newBuilder();
}
 
Example #28
Source File: AISToProtobuf.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
public AISToProtobuf(ProtobufRowFormat.Type formatType) {
    this(formatType, null);
}
 
Example #29
Source File: TypeRef.java    From api-compiler with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a reference to a message type, with default cardinality optional.
 */
public static TypeRef of(MessageType messageType) {
  return interner.intern(new TypeRef(Type.TYPE_MESSAGE, Cardinality.OPTIONAL, messageType, null));
}
 
Example #30
Source File: ProtobufDecompiler.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
protected String defaultAndOptions(MessageOrBuilder options, String defaultValue) {
    StringBuilder str = new StringBuilder();
    boolean first = true;
    if (defaultValue != null) {
        str.append(" [default = ");
        str.append(defaultValue); // TODO: quote
        first = false;
    }
    if (options != null) {
        for (Map.Entry<FieldDescriptor,Object> entry : options.getAllFields().entrySet()) {
            FieldDescriptor field = entry.getKey();
            Object value = entry.getValue();
            String fieldName = field.getName();
            if (field.isExtension()) {
                fieldName = "(" + fieldName + ")";
            }
            if (field.getType() == FieldDescriptor.Type.MESSAGE) {
                for (Map.Entry<FieldDescriptor,Object> subentry : ((MessageOrBuilder)value).getAllFields().entrySet()) {
                    FieldDescriptor subfield = subentry.getKey();
                    Object subvalue = subentry.getValue();
                    if (first) {
                        str.append(" [");
                        first = false;
                    }
                    else {
                        str.append(", ");
                    }
                    str.append(fieldName).append(".").append(subfield.getName()).append(" = ").append(literal(subvalue, subfield.getType()));
                }
            }
            else {
                if (first) {
                    str.append(" [");
                    first = false;
                }
                else {
                    str.append(", ");
                }
                str.append(fieldName).append(" = ").append(literal(value, field.getType()));
            }
        }
    }
    if (!first) {
        str.append("]");
    }
    return str.toString();
}