private String getMessageJavaType(String packageName, DescriptorProto sourceMessageDesc, FieldDescriptorProto field) { String fieldType = CommonUtils.findNotIncludePackageType(field.getTypeName()); Map<String, Pair<DescriptorProto, List<FieldDescriptorProto>>> nestedFieldType = transform(sourceMessageDesc); // isMap if (nestedFieldType.containsKey(fieldType)) { Pair<DescriptorProto, List<FieldDescriptorProto>> nestedFieldPair = nestedFieldType.get(fieldType); if (nestedFieldPair.getRight().size() == 2) { DescriptorProto mapSourceMessageDesc = nestedFieldPair.getLeft(); List<FieldDescriptorProto> mapFieldList = nestedFieldPair.getRight(); String nestedJavaType = "java.util.Map<" + findJavaType(packageName, mapSourceMessageDesc, mapFieldList.get(0)) + "," + findJavaType(packageName, mapSourceMessageDesc, mapFieldList.get(1)) + ">"; return nestedJavaType; } else { return null; } } else { return CommonUtils.findPojoTypeFromCache(field.getTypeName(), pojoTypeCache); } }
/** * Resolves a type based on the given partial name. This does not assume that the name, as * obtained from the descriptor, is in absolute form. */ private TypeRef resolveType(Location location, FieldDescriptorProto.Type kind, String name) { TypeRef type; switch (kind) { case TYPE_MESSAGE: case TYPE_ENUM: case TYPE_GROUP: type = symbolTable.resolveType(namespaces.peek(), name); break; default: type = TypeRef.of(kind); } if (type == null) { model.getDiagReporter().report(Diag.error(location, "Unresolved type '%s'", name)); } return type; }
private boolean checkCyclic(Set<MessageType> visited, MessageType message) { if (!visited.add(this)) { return false; } for (Field field : fields) { TypeRef type = field.getType(); if (type.getKind() == FieldDescriptorProto.Type.TYPE_MESSAGE) { if (message == type.getMessageType()) { return true; } if (type.getMessageType().checkCyclic(visited, message)) { // If there is a cycle to message via this, then this is also cyclic. That's the nature // of a cycle. return true; } } } return false; }
private void add(List<FieldDescriptorProto> extensions) { for (int i = 0; i < extensions.size(); i++) { pathSegments.push(i); FieldDescriptorProto extensionProto = extensions.get(i); String extendee = resolve(extensionProto.getExtendee()); Multimap<String, Extension> messageExtensions = builder.get(extendee); if (messageExtensions == null) { messageExtensions = ArrayListMultimap.create(); builder.put(extendee, messageExtensions); } String path = DOT_JOINER.join(pathSegments.descendingIterator()); DescriptorProtos.SourceCodeInfo.Location location = locationMap.get(path).get(0); // Since paths are only unique within a file, we need a synthetic path to make them unique, // given that paths are used to uniquely identify elements in a ProtoFile, and we're // stuffing elements from another file into it. path = currentFile.getName() + ":" + path; Location fileLocation = new SimpleLocation(String.format( "%s:%d:%d", currentFile.getName(), location.getSpan(0) + 1, location.getSpan(1) + 1)); Extension extension = new Extension(extensionProto, location, path, fileLocation); messageExtensions.put(getExtensionFieldName(extensionProto.getName()), extension); pathSegments.pop(); } }
public void decompile(FileDescriptorProto fileDescriptor) throws IOException { if (fileDescriptor.hasPackage()) { indentedFormat("package %s;", fileDescriptor.getPackage()); absolutePackage = "." + fileDescriptor.getPackage() + "."; } for (String dependency : fileDescriptor.getDependencyList()) { indentedFormat("import \"%s\";", dependency); } if (fileDescriptor.hasOptions()) { decompileOptions(fileDescriptor.getOptions()); } decompileMembers(fileDescriptor.getEnumTypeList(), fileDescriptor.getMessageTypeList(), Collections.<FieldDescriptorProto>emptyList(), Collections.<DescriptorProto.ExtensionRange>emptyList(), fileDescriptor.getExtensionList()); for (ServiceDescriptorProto serviceDescriptor : fileDescriptor.getServiceList()) { decompile(serviceDescriptor); } newline(); flush(); }
public void testPackedEnumField() throws Exception { FileDescriptorProto fileDescriptorProto = FileDescriptorProto.newBuilder() .setName("foo.proto") .addEnumType(EnumDescriptorProto.newBuilder() .setName("Enum") .addValue(EnumValueDescriptorProto.newBuilder() .setName("FOO") .setNumber(1) .build()) .build()) .addMessageType(DescriptorProto.newBuilder() .setName("Message") .addField(FieldDescriptorProto.newBuilder() .setName("foo") .setTypeName("Enum") .setNumber(1) .setLabel(FieldDescriptorProto.Label.LABEL_REPEATED) .setOptions(DescriptorProtos.FieldOptions.newBuilder() .setPacked(true) .build()) .build()) .build()) .build(); Descriptors.FileDescriptor.buildFrom( fileDescriptorProto, new FileDescriptor[0]); }
private static Optional<String> appendToFullName( DescriptorProto messageDescriptor, List<Integer> path, String fullNameSoFar) { switch (path.get(0)) { case DescriptorProto.NESTED_TYPE_FIELD_NUMBER: DescriptorProto nestedMessage = messageDescriptor.getNestedType(path.get(1)); return appendMessageToFullName(nestedMessage, path, fullNameSoFar); case DescriptorProto.ENUM_TYPE_FIELD_NUMBER: EnumDescriptorProto enumDescriptor = messageDescriptor.getEnumType(path.get(1)); return Optional.of(appendEnumToFullName(enumDescriptor, path, fullNameSoFar)); case DescriptorProto.FIELD_FIELD_NUMBER: FieldDescriptorProto fieldDescriptor = messageDescriptor.getField(path.get(1)); return Optional.of(appendFieldComponent(fullNameSoFar, fieldDescriptor.getName())); default: return Optional.empty(); } }
private FileDescriptorProto makeCanonicalProto(final FileDescriptor fileDescriptor) { final FileDescriptorProto.Builder protoBuilder = FileDescriptorProto.newBuilder(fileDescriptor.toProto()); for (final FieldDescriptorProto.Builder field : protoBuilder.getExtensionBuilderList()) { makeCanonicalField(field, fileDescriptor.findExtensionByName(field.getName())); } for (final DescriptorProto.Builder message : protoBuilder.getMessageTypeBuilderList()) { makeCanonicalMessage(message, fileDescriptor.findMessageTypeByName(message.getName())); } // for (EnumDescriptorProto.Builder enumProto : // protoBuilder.getEnumTypeBuilderList()) { // makeCanonicalEnum(enumProto, // fileDescriptor.findEnumTypeByName(enumProto.getName())); // } for (final ServiceDescriptorProto.Builder serviceProto : protoBuilder.getServiceBuilderList()) { makeCanonicalService(serviceProto, fileDescriptor.findServiceByName(serviceProto.getName())); } // TODO: incorporate options' tree walking into canonicalization to eliminate double walking return OptionResolver.newBuilder().setCustomOptionsAsExtensions(false) .resolveAllOptionsFor(fileDescriptor, protoBuilder).build(); }
@Override public void exitBytesFieldOption(final BytesFieldOptionContext ctx) { if (ctx.getChildCount() == 3) { verifyOptionNameUnique("default", ctx.getStart()); final FieldDescriptorProto.Builder fieldBuilder = FieldDescriptorProto.Builder.class.cast(scopes.getProtoBuilder()); final String input = ctx.getChild(2).getText(); try { final String result = protoEscapeBytes(unescapeBytes(input.substring(1, input.length() - 1))); fieldBuilder.setDefaultValue(result); } catch (final InvalidEscapeSequenceException e) { contextLookup.reportInvalidDefaultValue((ParserRuleContext) ctx.getChild(2), e); } } }
@Override public void exitStringFieldOption(final StringFieldOptionContext ctx) { // TODO: what to do about '\?' if (ctx.getChildCount() == 3) { verifyOptionNameUnique("default", ctx.getStart()); final FieldDescriptorProto.Builder fieldBuilder = FieldDescriptorProto.Builder.class.cast(scopes.getProtoBuilder()); final String input = ctx.getChild(2).getText(); try { final String result = unescapeText(input.substring(1, input.length() - 1).replaceAll("\\\\[?]", "?")); fieldBuilder.setDefaultValue(result); } catch (final InvalidEscapeSequenceException e) { contextLookup.reportInvalidDefaultValue((ParserRuleContext) ctx.getChild(2), e); } } }
private ParsedContext(final FileDescriptorProto.Builder proto, final List<FieldDescriptorProto.Builder> unresolved, final ContextLookup lookup) { this.proto = proto; if ((unresolved == null) || unresolved.isEmpty()) { this.unresolved = Collections.emptyList(); } else { if (lookup == null) { throw new NullPointerException(); } this.unresolved = new ArrayList<Map.Entry<FieldDescriptorProto.Builder, FieldContext>>(); for (final FieldDescriptorProto.Builder field : unresolved) { final FieldContext context = (FieldContext) lookup.getContext(field, false); if (context == null) { throw new IllegalStateException("field context must not be null"); } this.unresolved.add(new SimpleEntry<FieldDescriptorProto.Builder, FieldContext>(field, context)); } } }
public boolean resolveAllRefs(final Collection<FileDescriptor> dependencies, final IProtoErrorListener errorListener) { if (isBuilt()) { throw new IllegalStateException("not supported when proto is already built"); } final Map<String, NameContext> cache = new HashMap<String, NameContext>(); if (resolveAllRefs(dependencies, errorListener, cache)) { return true; } for (final Entry<FieldDescriptorProto.Builder, FieldContext> entry : unresolved) { reportUnresolvedTypeNameError(entry.getKey(), entry.getValue(), errorListener); } return false; }
private void buildAllOptions(final FileDescriptorProto.Builder proto) { if (!buildOptions(proto.getOptionsBuilder())) { proto.clearOptions(); } for (final FieldDescriptorProto.Builder extensionProto : proto.getExtensionBuilderList()) { if (!buildOptions(extensionProto.getOptionsBuilder())) { extensionProto.clearOptions(); } } for (final EnumDescriptorProto.Builder enumProto : proto.getEnumTypeBuilderList()) { buildAllOptions(enumProto); } for (final ServiceDescriptorProto.Builder serviceProto : proto.getServiceBuilderList()) { buildAllOptions(serviceProto); } for (final DescriptorProto.Builder messageProto : proto.getMessageTypeBuilderList()) { buildAllOptions(messageProto); } }
private void buildAllOptions(final DescriptorProto.Builder proto) { if (!buildOptions(proto.getOptionsBuilder())) { proto.clearOptions(); } for (final FieldDescriptorProto.Builder fieldProto : proto.getFieldBuilderList()) { if (!buildOptions(fieldProto.getOptionsBuilder())) { fieldProto.clearOptions(); } } for (final FieldDescriptorProto.Builder extensionProto : proto.getExtensionBuilderList()) { if (!buildOptions(extensionProto.getOptionsBuilder())) { extensionProto.clearOptions(); } } for (final EnumDescriptorProto.Builder enumProto : proto.getEnumTypeBuilderList()) { buildAllOptions(enumProto); } for (final DescriptorProto.Builder messageProto : proto.getNestedTypeBuilderList()) { buildAllOptions(messageProto); } }
private FileDescriptorProto makeCanonicalProto(final FileDescriptor file) { final FileDescriptorProto.Builder protoBuilder = FileDescriptorProto.newBuilder(file.toProto()); for (final FieldDescriptorProto.Builder field : protoBuilder.getExtensionBuilderList()) { makeCanonicalField(field, file.findExtensionByName(field.getName())); } for (final DescriptorProto.Builder message : protoBuilder.getMessageTypeBuilderList()) { makeCanonicalMessage(message, file.findMessageTypeByName(message.getName())); } // for (EnumDescriptorProto.Builder enumProto : // protoBuilder.getEnumTypeBuilderList()) { // makeCanonicalEnum(enumProto, // file.findEnumTypeByName(enumProto.getName())); // } for (final ServiceDescriptorProto.Builder serviceProto : protoBuilder.getServiceBuilderList()) { makeCanonicalService(serviceProto, file.findServiceByName(serviceProto.getName())); } return OptionResolver.newBuilder().setCustomOptionsAsExtensions(reparseCustomOptions) .resolveAllOptionsFor(file, protoBuilder).build(); }
@SuppressWarnings("null") public static FileDescriptorProto.Builder makeProtoRefsRelative(final FileDescriptorProto proto, final FieldTypeRefsMode fieldTypeRefsMode) { if (fieldTypeRefsMode == FieldTypeRefsMode.AS_IS) { return FileDescriptorProto.newBuilder(proto); } final FileDescriptorProto.Builder protoBuilder = FileDescriptorProto.newBuilder(proto); final String packagePath = "." + proto.getPackage(); for (final FieldDescriptorProto.Builder field : protoBuilder.getExtensionBuilderList()) { makeFieldRefsRelative(packagePath, field, fieldTypeRefsMode, packagePath); } for (final DescriptorProto.Builder message : protoBuilder.getMessageTypeBuilderList()) { makeMessageRefsRelative(packagePath, message, fieldTypeRefsMode, packagePath); } return protoBuilder; }
@SuppressWarnings("null") private static void makeFieldRefsRelative(final String packagePath, final FieldDescriptorProto.Builder field, final FieldTypeRefsMode fieldTypeRefsMode, final String parentFullName) { if (field.hasExtendee() && field.getExtendee().startsWith(".")) { field.setExtendee(getRelativeName(packagePath, field.getExtendee(), fieldTypeRefsMode, parentFullName)); } if (field.hasTypeName() && field.getTypeName().startsWith(".")) { field.setTypeName(getRelativeName(packagePath, field.getTypeName(), fieldTypeRefsMode, parentFullName)); if (field.hasType()) { field.clearType(); } } }
private void assertEqualDescriptorProtoFields(final int messageIndex, final boolean isProtocCompatible) throws URISyntaxException, IOException { // given final List<FieldDescriptorProto> expected = protocProto.getMessageType(messageIndex).getFieldList(); // when final Builder protoBuilder = filesBuilder.setProtocCompatible(isProtocCompatible) .addFiles(baseDir, DEFAULT_VALUES_PROTO); final List<FieldDescriptorProto> actual = protoBuilder.buildProtos().get(0).getMessageType(messageIndex).getFieldList(); // then // no errors logged! verify(mockErrorListener, never()).validationError(anyInt(), anyInt(), anyString(), any(RuntimeException.class)); verify(mockErrorListener, never()).syntaxError(any(Recognizer.class), any(), anyInt(), anyInt(), anyString(), any(RecognitionException.class)); assertThat(actual).as("check nullness, duplicates, size").isNotNull().doesNotContainNull() .doesNotHaveDuplicates().hasSameSizeAs(expected); assertThat(actual).as("check fields equality").containsOnlyElementsOf(expected); }
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(); }
public void testUnknownFieldsDenied() throws Exception { FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() .setName("foo.proto") .addMessageType(DescriptorProto.newBuilder() .setName("Foo") .addField(FieldDescriptorProto.newBuilder() .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) .setTypeName("Bar") .setName("bar") .setNumber(1))) .build(); try { Descriptors.FileDescriptor.buildFrom(fooProto, new FileDescriptor[0]); fail("DescriptorValidationException expected"); } catch (DescriptorValidationException e) { assertTrue(e.getMessage().indexOf("Bar") != -1); assertTrue(e.getMessage().indexOf("is not defined") != -1); } }
public void testFieldTypeEnumMapping() throws Exception { assertEquals(FieldDescriptor.Type.values().length, FieldDescriptorProto.Type.values().length); for (FieldDescriptor.Type type : FieldDescriptor.Type.values()) { FieldDescriptorProto.Type protoType = type.toProto(); assertEquals("TYPE_" + type.name(), protoType.name()); assertEquals(type, FieldDescriptor.Type.valueOf(protoType)); } }
/** * Tests that the DescriptorValidationException works as intended. */ public void testDescriptorValidatorException() throws Exception { FileDescriptorProto fileDescriptorProto = FileDescriptorProto.newBuilder() .setName("foo.proto") .addMessageType(DescriptorProto.newBuilder() .setName("Foo") .addField(FieldDescriptorProto.newBuilder() .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) .setType(FieldDescriptorProto.Type.TYPE_INT32) .setName("foo") .setNumber(1) .setDefaultValue("invalid") .build()) .build()) .build(); try { Descriptors.FileDescriptor.buildFrom(fileDescriptorProto, new FileDescriptor[0]); fail("DescriptorValidationException expected"); } catch (DescriptorValidationException e) { // Expected; check that the error message contains some useful hints assertTrue(e.getMessage().indexOf("foo") != -1); assertTrue(e.getMessage().indexOf("Foo") != -1); assertTrue(e.getMessage().indexOf("invalid") != -1); assertTrue(e.getCause() instanceof NumberFormatException); assertTrue(e.getCause().getMessage().indexOf("invalid") != -1); } }
private Map<String, Pair<DescriptorProto, List<FieldDescriptorProto>>> transform( DescriptorProto sourceMessageDesc) { Map<String, Pair<DescriptorProto, List<FieldDescriptorProto>>> nestedFieldMap = Maps.newHashMap(); sourceMessageDesc.getNestedTypeList().forEach(new Consumer<DescriptorProto>() { @Override public void accept(DescriptorProto t) { nestedFieldMap.put(t.getName(), new ImmutablePair<DescriptorProto, List<FieldDescriptorProto>>(t, t.getFieldList())); } }); return nestedFieldMap; }
public CausalOrderProtobufSerializer(@NotNull final Serializer<T> objectSerializer) { this.objectSerializer = objectSerializer; try { final FileDescriptorProto timestampedMessageFile = FileDescriptorProto.newBuilder() .addMessageType( DescriptorProto.newBuilder() .setName(MESSAGE_NAME) .addField( FieldDescriptorProto.newBuilder() .setLabel(FieldDescriptorProto.Label.LABEL_REPEATED) .setName(IDS_FIELD_NAME) .setNumber(IDS_FIELD_NUMBER) .setType(FieldDescriptorProto.Type.TYPE_BYTES)) .addField( FieldDescriptorProto.newBuilder() .setLabel(FieldDescriptorProto.Label.LABEL_REPEATED) .setName(TIMESTAMPS_FIELD_NAME) .setNumber(TIMESTAMPS_FIELD_NUMBER) .setOptions(FieldOptions.newBuilder() .setPacked(true)) .setType(FieldDescriptorProto.Type.TYPE_UINT64)) .addField( FieldDescriptorProto.newBuilder() .setName(VALUE_FIELD_NAME) .setNumber(VALUE_FIELD_NUMBER) .setType(FieldDescriptorProto.Type.TYPE_BYTES))) .build(); this.messageDescriptor = FileDescriptor .buildFrom(timestampedMessageFile, new FileDescriptor[0]) .findMessageTypeByName(MESSAGE_NAME); this.ids = messageDescriptor.findFieldByName(IDS_FIELD_NAME); this.timestamps = messageDescriptor.findFieldByName(TIMESTAMPS_FIELD_NAME); this.value = messageDescriptor.findFieldByName(VALUE_FIELD_NAME); this.messageParser = DynamicMessage.newBuilder(messageDescriptor) .buildPartial() .getParserForType(); } catch (final DescriptorValidationException e) { throw new RuntimeException(e); } }
public SingleSourceFifoOrderProtobufSerializer(@NotNull final Serializer<T> objectSerializer) { this.objectSerializer = objectSerializer; try { final FileDescriptorProto timestampedMessageFile = FileDescriptorProto.newBuilder() .addMessageType( DescriptorProto.newBuilder() .setName(MESSAGE_NAME) .addField( FieldDescriptorProto.newBuilder() .setName(TIMESTAMP_FIELD_NAME) .setNumber(TIMESTAMP_FIELD_NUMBER) .setType(FieldDescriptorProto.Type.TYPE_INT64)) .addField( FieldDescriptorProto.newBuilder() .setName(VALUE_FIELD_NAME) .setNumber(VALUE_FIELD_NUMBER) .setType(FieldDescriptorProto.Type.TYPE_BYTES))) .build(); final Descriptor message = FileDescriptor .buildFrom(timestampedMessageFile, new FileDescriptor[0]) .findMessageTypeByName(MESSAGE_NAME); this.timestampedMessageField = message.findFieldByName(TIMESTAMP_FIELD_NAME); this.valueMessageField = message.findFieldByName(VALUE_FIELD_NAME); this.messageBuilder = DynamicMessage.newBuilder(message); this.messageParser = messageBuilder.buildPartial().getParserForType(); } catch (final DescriptorValidationException e) { throw new RuntimeException(e); } }
private FieldDescriptorProto generateField(Field field) { FieldDescriptorProto.Builder builder = FieldDescriptorProto.newBuilder(); builder.setName(getFieldName(field)); builder.setNumber(field.getNumber()); builder.setLabel(toLabel(field.getCardinality())); builder.setType(toType(field.getKind())); if (field.getKind() == Kind.TYPE_ENUM || field.getKind() == Kind.TYPE_MESSAGE || field.getKind() == Kind.TYPE_GROUP) { builder.setTypeName(getTypeName(field.getTypeUrl())); } // NOTE: extendee not supported // NOTE: default_value not supported if (field.getOneofIndex() != 0) { // Index in the containing type's oneof_decl is zero-based. // Index in google.protobuf.type.Field.oneof_index is one-based. builder.setOneofIndex(field.getOneofIndex() - 1); } if (!Strings.isNullOrEmpty(field.getDefaultValue())) { builder.setDefaultValue(field.getDefaultValue()); } FieldOptions options = getFieldOptions(field); if (!options.equals(FieldOptions.getDefaultInstance())) { builder.setOptions(options); } return builder.build(); }
/** In proto3, repeated fields of scalar numeric types use packed encoding by default */ private boolean isDefaultPackedEncoding(Field field) { if (field.getSyntax() == Syntax.SYNTAX_PROTO3 && field.isRepeated()) { FieldDescriptorProto.Type fieldType = field.getProto().getType(); if (fieldType != FieldDescriptorProto.Type.TYPE_GROUP && fieldType != FieldDescriptorProto.Type.TYPE_BYTES && fieldType != FieldDescriptorProto.Type.TYPE_STRING && fieldType != FieldDescriptorProto.Type.TYPE_MESSAGE) { return true; } } return false; }
@VisitsBefore void visit(Method method) { // Resolve input and output type of this method. TypeRef inputType = resolveType(method.getLocation(), FieldDescriptorProto.Type.TYPE_MESSAGE, method.getDescriptor().getInputTypeName()); if (inputType != null) { method.setInputType(inputType); } TypeRef outputType = resolveType(method.getLocation(), FieldDescriptorProto.Type.TYPE_MESSAGE, method.getDescriptor().getOutputTypeName()); if (outputType != null) { method.setOutputType(outputType); } findOptionTypes(method.getOptionFields()); }
private Extension(FieldDescriptorProto proto, DescriptorProtos.SourceCodeInfo.Location location, String path, Location fileLocation) { this.proto = proto; this.location = location; this.path = path; this.fileLocation = fileLocation; }
/** * 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(); }
protected void addFieldToMessageAncestor( int generationsToSkip, FieldDescriptorProto.Builder fieldDesc, SourceCodeInfo.Location location) { BuilderVisitorNodeInfo ancestorInfo = getAncestorInfo(generationsToSkip); if (ancestorInfo instanceof MessageNodeInfo) { ((MessageNodeInfo) ancestorInfo).addNewField(fieldDesc, location); setModified(true); } else { throw new RuntimeException( String.format( "Tried to add a field to a %s, but can only add to %s", ancestorInfo.node().getClass(), DescriptorProto.Builder.class)); } }
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 void addColumn(Column column) { String fieldName = uniqueIdent(ident(column.getName(), false), fieldNames); fieldBuilder = messageBuilder.addFieldBuilder(); fieldBuilder.setName(fieldName); fieldBuilder.setLabel(Label.LABEL_OPTIONAL); FieldOptions.Builder fieldBuilderOptions = FieldOptions.newBuilder(); ColumnOptions.Builder columnOptions = ColumnOptions.newBuilder(); if (!fieldName.equals(column.getName())) { columnOptions.setName(column.getName()); } columnOptions.setSqlType(column.getTypeDescription().toUpperCase()); columnOptions.setUuid(column.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; } } } } setColumnType(column, columnOptions); setFieldNumber(); fieldBuilderOptions.setExtension(ColumnOptions.fdbsql, columnOptions.build()); fieldBuilder.setOptions(fieldBuilderOptions); if (column.getNullable() && ((column.getDefaultValue() != null) || (column.getDefaultFunction() != null))) { addNullForField(column.getName(), fieldBuilder.getNumber()); } }
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); }