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())); } } }
private FieldDescriptorProto createFieldDescriptorProto(String name, int index, Type type, String typeName, Label label, String defaultValue, String extendee, UnknownFieldSet unknownFields, FieldOptions options) { FieldDescriptorProto.Builder fieldBuilder = FieldDescriptorProto.newBuilder(); return fieldBuilder .setName(name) .setNumber(index) .setType(type) .setTypeName(typeName) .setLabel(label) .setDefaultValue(defaultValue) .setExtendee(extendee) .setUnknownFields(unknownFields) .setOptions(options) .build(); }
/** * 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()); } } } }
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; }
@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() }
@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() }
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); } } }
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); } } }
protected String literal(Object value, FieldDescriptor.Type type) { switch (type) { case STRING: return quotedString((String)value); default: return value.toString(); } }
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); }
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); }
static public String getTypeName(FieldDescriptorProto proto) { Type t = proto.getType(); if (t==Type.TYPE_MESSAGE || t==Type.TYPE_ENUM) { String name = proto.getTypeName(); if (name.startsWith(".")) { return name.replaceFirst("\\.", ""); } else { return name; } } return typeMap.get(t); }
@Override public void exitGroup(final GroupContext ctx) { scopes.popScope(); final String groupName = ctx.groupIdentifier().getText(); // add group's field final FieldDescriptorProto.Builder fieldBuilder = scopes.addField(); setFieldBuilder(fieldBuilder, groupName.toLowerCase(), ctx.fieldNumber().getText(), ctx.label().getText(), FieldDescriptorProto.Type.TYPE_GROUP).setTypeName(groupName); scopes.popScope(); contextLookup.addGroup(fieldBuilder, ctx); scopes.addIfUnresolved(fieldBuilder); scopes.verifyField(fieldBuilder); }
@Override public void exitOneofGroup(final OneofGroupContext ctx) { scopes.popScope(); final String groupName = ctx.groupIdentifier().getText(); // add group's field final FieldDescriptorProto.Builder fieldBuilder = scopes.addField(); setFieldBuilder(fieldBuilder, groupName.toLowerCase(), ctx.fieldNumber().getText(), "optional", FieldDescriptorProto.Type.TYPE_GROUP).setTypeName(groupName); scopes.popScope(); contextLookup.addGroup(fieldBuilder, ctx); scopes.addIfUnresolved(fieldBuilder); scopes.verifyField(fieldBuilder); }
private FieldDescriptorProto.Builder setFieldBuilder( final FieldDescriptorProto.Builder fieldBuilder, final String name, final String number, final String label, final FieldDescriptorProto.Type type) { return fieldBuilder.setName(name) .setLabel(FieldDescriptorProto.Label.valueOf("LABEL_" + label.toUpperCase())) .setNumber(Integer.decode(number)).setType(type); }
/** * 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)); }
/** * 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)); }
/** * Creates a reference to an enum type, with default cardinality optional. */ public static TypeRef of(EnumType enumType) { return interner.intern(new TypeRef(Type.TYPE_ENUM, Cardinality.OPTIONAL, null, enumType)); }
/** * Creates a reference to a primitive type based on its name in the protocol buffer language, * with default cardinality optional. */ @Nullable public static TypeRef fromPrimitiveName(String name) { Type kind = PRIMITIVE_TYPE_MAP.get(name); return kind == null ? null : of(kind); }
/** * Returns the type kind. */ public Type getKind() { return kind; }
/** * Return trues if this type represents a map. */ public boolean isMap() { return card == Cardinality.REPEATED && kind == Type.TYPE_MESSAGE && messageType.getProto().getOptions().getMapEntry(); }
/** * Return true of this is a cyclic message type. */ public boolean isCyclic() { return getKind() == Type.TYPE_MESSAGE && getMessageType().isCyclic(); }
/** * Returns true of this is a message type. */ public boolean isMessage() { return kind == Type.TYPE_MESSAGE; }
/** * Returns true of this is an enum type. */ public boolean isEnum() { return kind == Type.TYPE_ENUM; }
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(); }
public AISToProtobuf(ProtobufRowFormat.Type formatType) { this(formatType, null); }
public AISToProtobuf(ProtobufRowFormat.Type formatType, FileDescriptorSet priorSet) { this.formatType = formatType; this.priorSet = priorSet; setBuilder = FileDescriptorSet.newBuilder(); }
/** Get the Protobuf field type. */ public abstract Type getType();
public CompatibleConversion(Type type, UnderlyingType underlying) { this.type = type; this.underlying = underlying; }
@Override public Type getType() { return type; }
public IntegerConversion(Type type, UnderlyingType underlying) { this.type = type; this.underlying = underlying; }
@Override public Type getType() { return Type.TYPE_BYTES; }
@Override public Type getType() { return Type.TYPE_STRING; }
@Override public Type getType() { return Type.TYPE_SINT32; }
@Override public Type getType() { return Type.TYPE_SINT64; }