private void handleMissingField(String fieldName, JsonParser parser, ExtensionRegistry extensionRegistry, UnknownFieldSet.Builder builder) throws IOException { JsonToken token = parser.nextToken(); if (token.equals(JsonToken.START_OBJECT)) { // Message structure token = parser.nextToken(); // skip name while (token != null && !token.equals(JsonToken.END_OBJECT)) { handleMissingField(fieldName, parser, extensionRegistry, builder); token = parser.nextToken(); // get } or field name } } else if (token.equals(JsonToken.START_ARRAY)) { // Collection do { handleMissingField(fieldName, parser, extensionRegistry, builder); token = parser.getCurrentToken(); // got value or ] } while (token != null && !token.equals(JsonToken.END_ARRAY)); } else { // Primitive value // NULL, INT, BOOL, STRING // nothing to do.. } }
/** * * @author liushiming * @param args * @since JDK 1.8 */ public static void main(String[] args) { CommandProtoc commondProtoc = CommandProtoc.configProtoPath( "/Users/liushiming/project/java/saluki/saluki-plugin/saluki-plugin-common/src/test/java/com/quancheng/saluki", new File( "/Users/liushiming/project/java/saluki/saluki-example/saluki-example-api/target/protoc-dependencies")); FileDescriptorSet fileDescriptorSet = commondProtoc.invoke( "/Users/liushiming/project/java/saluki/saluki-plugin/saluki-plugin-common/src/test/java/com/quancheng/saluki/saluki_service.proto"); Map<Integer, UnknownFieldSet.Field> lengthDelimitedList = fileDescriptorSet.getFile(0) .getMessageType(0).getField(0).getOptions().getUnknownFields().asMap(); for (Map.Entry<Integer, UnknownFieldSet.Field> integerFieldEntry : lengthDelimitedList .entrySet()) { for (ByteString byteString : integerFieldEntry.getValue().getLengthDelimitedList()) { System.out.println(integerFieldEntry.getKey() + "--" + byteString.toStringUtf8()); } } System.out.println(fileDescriptorSet); }
public Builder mergeFrom(com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { com.google.protobuf.UnknownFieldSet.Builder unknownFields = com.google.protobuf.UnknownFieldSet.newBuilder(this.getUnknownFields()); while (true) { int tag = input.readTag(); switch (tag) { case 0: this.setUnknownFields(unknownFields.build()); onChanged(); return this; default: { if (!parseUnknownField(input, unknownFields, extensionRegistry, tag)) { this.setUnknownFields(unknownFields.build()); onChanged(); return this; } break; } case 10: { bitField0_ |= 0x00000001; bytesObject_ = input.readBytes(); break; } } } }
private static void handleUnknownFields( Record record, String fieldPath, DynamicMessage.Builder builder ) throws IOException { String path = fieldPath.isEmpty() ? FORWARD_SLASH : fieldPath; String attribute = record.getHeader().getAttribute(ProtobufTypeUtil.PROTOBUF_UNKNOWN_FIELDS_PREFIX + path); if (attribute != null) { UnknownFieldSet.Builder unknownFieldBuilder = UnknownFieldSet.newBuilder(); unknownFieldBuilder.mergeDelimitedFrom( new ByteArrayInputStream( org.apache.commons.codec.binary.Base64.decodeBase64(attribute.getBytes(StandardCharsets.UTF_8)) ) ); UnknownFieldSet unknownFieldSet = unknownFieldBuilder.build(); builder.setUnknownFields(unknownFieldSet); } }
public static void checkRecordForUnknownFields(Record record, int i) throws IOException { // unknown fields are expected in paths for person and employee String attribute = record.getHeader().getAttribute(ProtobufTypeUtil.PROTOBUF_UNKNOWN_FIELDS_PREFIX + "/"); UnknownFieldSet.Builder builder = UnknownFieldSet.newBuilder(); builder.mergeDelimitedFrom(new ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64(attribute.getBytes()))); UnknownFieldSet unknownFieldSet = builder.build(); UnknownFieldsUtil.checkEmployeeUnknownFields(unknownFieldSet); if(i%2 == 0) { attribute = record.getHeader().getAttribute(ProtobufTypeUtil.PROTOBUF_UNKNOWN_FIELDS_PREFIX + "/engineer/person"); } else { attribute = record.getHeader().getAttribute(ProtobufTypeUtil.PROTOBUF_UNKNOWN_FIELDS_PREFIX + "/exec/person"); } builder = UnknownFieldSet.newBuilder(); builder.mergeDelimitedFrom(new ByteArrayInputStream(org.apache.commons.codec.binary.Base64.decodeBase64(attribute.getBytes()))); unknownFieldSet = builder.build(); UnknownFieldsUtil.checkPersonUnknownFields(unknownFieldSet); }
public static UnknownFieldSet getEmployeeUnknownFields() { // add unknown fields UnknownFieldSet.Field unknownStringField = UnknownFieldSet.Field.newBuilder() .addLengthDelimited(ByteString.copyFromUtf8("Hello San FRancisco!")) .build(); UnknownFieldSet.Field unknownVarIntField = UnknownFieldSet.Field.newBuilder() .addVarint(123456789) .build(); UnknownFieldSet employeeUnknownFields = UnknownFieldSet.newBuilder() .addField(345, unknownStringField) .addField(456, unknownVarIntField) .build(); return employeeUnknownFields; }
@Test public void testToBuilder() throws Exception { final IBuilder2 builder = messageProvider.newBuilder(TestAllTypes.getDescriptor()); reflectionTester.setAllFieldsViaReflection(builder); final int unknownFieldNum = 9; final long unknownFieldVal = 90; builder.setUnknownFields(UnknownFieldSet .newBuilder() .addField(unknownFieldNum, UnknownFieldSet.Field.newBuilder().addVarint(unknownFieldVal).build()).build()); final Message message = builder.build(); final Message derived = message.toBuilder().build(); reflectionTester.assertAllFieldsSetViaReflection(derived); assertEquals(Arrays.asList(unknownFieldVal), derived.getUnknownFields().getField(unknownFieldNum).getVarintList()); }
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(); }
@Override public void print(UnknownFieldSet fields, OutputStream output, Charset cs) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(output, cs); print(fields, writer); writer.flush(); }
/** * Outputs a Smile representation of {@code fields} to {@code output}. */ public void print(final UnknownFieldSet fields, OutputStream output, Charset cs) throws IOException { try { XMLStreamWriter generator = createGenerator(output); generator.writeStartElement(MESSAGE_ELEMENT); printUnknownFields(fields, generator); generator.writeEndElement(); generator.close(); } catch (XMLStreamException e) { throw new IOException(e); } }
private void handleMissingField(String fieldName, XMLEventReader parser, XMLEvent event, ExtensionRegistry extensionRegistry, UnknownFieldSet.Builder builder) throws XMLStreamException { // skip over the unknown fields, since we can't map them by id, then this message must not know about them. // We 'could' map them into the UnknownFieldSet, however none of the other formatters support this.. // but in the future it would probably be useful for the case: Message A (v2) -> Message B (v1) -> Xml -> Message A (v2) // this would require extra meta data in the xml to know the type of the unknown-field. if (event.isStartElement()) { /** * This loop will eat up everything inside "6" * So when this method is called, fieldName = 6, and event is set at index="11" * <unknown-field index="6"> * <unknown-field index="11">566667</unknown-field> * <unknown-field index="15"> * <unknown-field index="16">566667</unknown-field> * </unknown-field> * </unknown-field> */ int depth = 1; // we start 1 level down, the value of "6" while (parser.hasNext()) { XMLEvent nextEvent = parser.nextEvent(); if (nextEvent.isEndElement()) { depth--; if (depth <= 0 && parser.peek().isEndElement()) { break; } } else if (nextEvent.isStartElement()) { depth++; } } } else if (event.isCharacters()) { // done, let it slide. } }
/** * Outputs a textual representation of {@code fields} to {@code output}. */ public void print(final UnknownFieldSet fields, Appendable output) throws IOException { CouchDBGenerator generator = new CouchDBGenerator(output); generator.print("{"); printUnknownFields(fields, generator); generator.print("}"); }
/** * Like {@code print()}, but writes directly to a {@code String} and returns it. */ public String printToString(final UnknownFieldSet fields) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); print(fields, out, defaultCharset); out.flush(); return out.toString(); } catch (IOException e) { throw new RuntimeException("Writing to a StringBuilder threw an IOException (should never happen).", e); } }
/** * Outputs a textual representation of {@code fields} to {@code output}. */ public void print(final UnknownFieldSet fields, Appendable output) throws IOException { XmlGenerator generator = new XmlGenerator(output); generator.print("<message>"); printUnknownFields(fields, generator); generator.print("</message>"); }
/** * Outputs a Smile representation of {@code fields} to {@code output}. */ public void print(final UnknownFieldSet fields, OutputStream output, Charset cs) throws IOException { JsonGenerator generator = createGenerator(output); generator.writeStartObject(); printUnknownFields(fields, generator); generator.writeEndObject(); generator.close(); }
public void print(final UnknownFieldSet fields, Appendable output) throws IOException { HtmlGenerator generator = new HtmlGenerator(output); generator.print("<html>"); generator.print(META_CONTENT); generator.print("</head><body>"); printUnknownFields(fields, generator); generator.print("</body></html>"); }
/** * Outputs a textual representation of {@code fields} to {@code output}. */ public void print(final UnknownFieldSet fields, Appendable output) throws IOException { JsonGenerator generator = new JsonGenerator(output); generator.print("{"); printUnknownFields(fields, generator); generator.print("}"); }
private SyncMessageContext(CodedInputStream input, ExtensionRegistryLite extensionRegistry) throws InvalidProtocolBufferException { this.memoizedIsInitialized = -1; this.memoizedSerializedSize = -1; this.initFields(); boolean mutable_bitField0_ = false; com.google.protobuf.UnknownFieldSet.Builder unknownFields = UnknownFieldSet.newBuilder(); try { boolean e = false; while(!e) { int tag = input.readTag(); switch(tag) { case 0: e = true; break; case 10: this.bitField0_ |= 1; this.destination_ = input.readBytes(); break; case 16: this.bitField0_ |= 2; this.timestamp_ = input.readUInt64(); break; default: if(!this.parseUnknownField(input, unknownFields, extensionRegistry, tag)) { e = true; } } } } catch (InvalidProtocolBufferException var11) { throw var11.setUnfinishedMessage(this); } catch (IOException var12) { throw (new InvalidProtocolBufferException(var12.getMessage())).setUnfinishedMessage(this); } finally { this.unknownFields = unknownFields.build(); this.makeExtensionsImmutable(); } }
@Override public int run() throws Exception { mustHaveValidSite(); Injector dbInjector = createDbInjector(SINGLE_USER); manager.add(dbInjector); manager.start(); RuntimeShutdown.add(manager::stop); dbInjector.injectMembers(this); ProgressMonitor progress = new TextProgressMonitor(); progress.beginTask("Importing entities", ProgressMonitor.UNKNOWN); try (ReviewDb db = schemaFactory.open()) { for (RelationModel model : new JavaSchemaModel(ReviewDb.class).getRelations()) { relations.put(model.getRelationID(), Relation.create(model, db)); } Parser<UnknownFieldSet> parser = UnknownFieldSet.getDefaultInstance().getParserForType(); try (InputStream in = new BufferedInputStream(Files.newInputStream(file.toPath()))) { UnknownFieldSet msg; while ((msg = parser.parseDelimitedFrom(in)) != null) { Map.Entry<Integer, UnknownFieldSet.Field> e = Iterables.getOnlyElement(msg.asMap().entrySet()); Relation rel = checkNotNull( relations.get(e.getKey()), "unknown relation ID %s in message: %s", e.getKey(), msg); List<ByteString> values = e.getValue().getLengthDelimitedList(); checkState(values.size() == 1, "expected one string field in message: %s", msg); upsert(rel, values.get(0)); progress.update(1); } } progress.endTask(); } return 0; }
public static void checkPersonUnknownFields(UnknownFieldSet unknownFieldSet) { Map<Integer, UnknownFieldSet.Field> integerFieldMap = unknownFieldSet.asMap(); Assert.assertEquals(2, integerFieldMap.size()); Assert.assertTrue(integerFieldMap.containsKey(123)); Assert.assertEquals(1, integerFieldMap.get(123).getFixed32List().size()); Assert.assertEquals(1234, (int)integerFieldMap.get(123).getFixed32List().get(0)); Assert.assertTrue(integerFieldMap.containsKey(234)); Assert.assertEquals(1, integerFieldMap.get(234).getFixed64List().size()); Assert.assertEquals(12345678, (long)integerFieldMap.get(234).getFixed64List().get(0)); }
public static void checkEmployeeUnknownFields(UnknownFieldSet unknownFieldSet) { Map<Integer, UnknownFieldSet.Field> integerFieldMap = unknownFieldSet.asMap(); Assert.assertEquals(2, integerFieldMap.size()); Assert.assertTrue(integerFieldMap.containsKey(345)); Assert.assertEquals(1, integerFieldMap.get(345).getLengthDelimitedList().size()); Assert.assertEquals( integerFieldMap.get(345).getLengthDelimitedList().get(0), ByteString.copyFromUtf8("Hello San FRancisco!") ); Assert.assertTrue(integerFieldMap.containsKey(456)); Assert.assertEquals(1, integerFieldMap.get(456).getVarintList().size()); Assert.assertEquals(123456789, (long)integerFieldMap.get(456).getVarintList().get(0)); }
public static UnknownFieldSet getPersonUnknownFields() throws IOException { // add unknown fields UnknownFieldSet.Field unknownIntField = UnknownFieldSet.Field.newBuilder() .addFixed32(1234) .build(); UnknownFieldSet.Field unknownLongField = UnknownFieldSet.Field.newBuilder() .addFixed64(12345678) .build(); UnknownFieldSet unknownFieldSet = UnknownFieldSet.newBuilder() .addField(123, unknownIntField) .addField(234, unknownLongField) .build(); return unknownFieldSet; }
public static byte[] getBytes(UnknownFieldSet unknownFieldSet) throws IOException { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); unknownFieldSet.writeDelimitedTo(bOut); bOut.flush(); bOut.close(); return org.apache.commons.codec.binary.Base64.encodeBase64(bOut.toByteArray()); }
/** * Outputs a textual representation of {@code fields} to {@code output}. */ public static void print(UnknownFieldSet fields, Appendable output) throws IOException { JsonGenerator generator = new JsonGenerator(output); generator.print("{\n"); generator.indent(); printUnknownFields(fields, generator); generator.outdent(); generator.print("}"); }
/** * Like {@code print()}, but writes directly to a {@code String} and returns it. */ public static String printToString(UnknownFieldSet fields) { try { StringBuilder text = new StringBuilder(); print(fields, text); return text.toString(); } catch (IOException e) { throw new RuntimeException("Writing to a StringBuilder threw an IOException (should never happen).", e); } }
@SuppressWarnings("null") @Override public Result isEqual(final Descriptor type, final UnknownFieldSet value1, final UnknownFieldSet value2) { if (value1 == value2) { return Result.EQUAL; } if (!ALL_OPTION_DESCRIPTORS.contains(type)) { return Result.NOT_FILTERED; } final Map<Integer, Field> map1 = value1.asMap(); final Map<Integer, Field> map2 = value2.asMap(); if (map1.size() != map2.size()) { return Result.NOT_EQUAL; } for (final Entry<Integer, Field> entry : map1.entrySet()) { final Field field1 = entry.getValue(); final Field field2 = map2.get(entry.getKey()); if (field2 == null || !Objects.equals(getUnknownFieldValue(field1), getUnknownFieldValue(field2))) { return Result.NOT_EQUAL; } } return Result.EQUAL; }
private FileDescriptorProto createFileDescriptorProto(String fileName, String packageName, UnknownFieldSet unknownFields) { FileDescriptorProto.Builder fileBuilder = FileDescriptorProto.newBuilder(); return fileBuilder .setName(fileName) .setPackage(packageName) .setUnknownFields(unknownFields) .addAllDependency(Collections.<String>emptyList()) .addAllEnumType(Collections.<EnumDescriptorProto>emptyList()) .addAllExtension(Collections.<FieldDescriptorProto>emptyList()) .addAllMessageType(Collections.<DescriptorProto>emptyList()) .addAllPublicDependency(Collections.<Integer>emptyList()) .addAllService(Collections.<ServiceDescriptorProto>emptyList()) .build(); }
private DescriptorProto createMessageDescriptorProto(String messageName, UnknownFieldSet unknownFields) { DescriptorProto.Builder messageBuilder = DescriptorProto.newBuilder(); return messageBuilder .setName(messageName) .setUnknownFields(unknownFields) .addAllEnumType(Collections.<EnumDescriptorProto>emptyList()) .addAllExtension(Collections.<FieldDescriptorProto>emptyList()) .addAllField(Collections.<FieldDescriptorProto>emptyList()) .addAllNestedType(Collections.<DescriptorProto>emptyList()) .build(); }
/** Outputs a textual representation of {@code fields} to {@code output}. */ public void print(final UnknownFieldSet fields, Appendable output) throws IOException { final JavaPropsGenerator generator = new JavaPropsGenerator(output); printUnknownFields(fields, generator); }
@Override protected List<String> collectFileData() { String sourePackageName = super.getSourcePackageName(); String className = super.getClassName(); String packageName = sourePackageName.toLowerCase(); List<String> packageData = Lists.newArrayList(); packageData.add("package " + packageName + ";"); packageData.add(""); List<String> importData = Lists.newArrayList(); importData.add("import com.quancheng.saluki.serializer.ProtobufAttribute;"); importData.add("import com.quancheng.saluki.serializer.ProtobufEntity;"); List<String> classAnnotationData = Lists.newArrayList(); classAnnotationData.add(""); classAnnotationData.add("@ProtobufEntity(" + sourePackageName + "." + className + ".class)"); boolean validator = false; List<String> fileData = Lists.newArrayList(); fileData.add("public class " + className + "{"); for (int i = 0; i < messageFields.size(); i++) { FieldDescriptorProto messageField = messageFields.get(i); String javaType = findJavaType(packageName, sourceMessageDesc, messageField); if (messageField.getLabel() == Label.LABEL_REPEATED && javaType != null) { if (!javaType.contains("java.util.Map")) { javaType = "java.util.ArrayList<" + javaType + ">"; } } fileData.add(""); String fieldName = messageField.getName(); UnknownFieldSet unknownFields = messageField.getOptions().getUnknownFields(); if (unknownFields != null) { for (Map.Entry<Integer, UnknownFieldSet.Field> integerFieldEntry : unknownFields.asMap() .entrySet()) { for (ByteString byteString : integerFieldEntry.getValue().getLengthDelimitedList()) { validator = true; String validateMsg = byteString.toStringUtf8(); fileData.add(" " + validateMsg); } } } fileData.add(" @ProtobufAttribute"); fileData.add(" private " + javaType + " " + fieldName + ";"); fileData.add(""); fileData.add(" public " + javaType + " get" + captureName(fieldName) + "() {"); fileData.add(" return this." + fieldName + ";"); fileData.add(" }"); fileData.add(""); fileData.add(" public void set" + captureName(fieldName) + "(" + javaType + " " + fieldName + ") {"); fileData.add(" this." + fieldName + "=" + fieldName + ";"); fileData.add(" }"); fileData.add(""); } fileData.add("}"); if (validator) { importData.add("import com.quancheng.saluki.core.grpc.annotation.ArgValidator;"); classAnnotationData.add("@ArgValidator"); } packageData.addAll(importData); packageData.addAll(classAnnotationData); packageData.addAll(fileData); return packageData; }
@Override public UnknownFieldSet getUnknownFields() { // TODO Auto-generated method stub return UnknownFieldSet.getDefaultInstance(); }
@Override public UnknownFieldSet getUnknownFields() { return UnknownFieldSet.getDefaultInstance(); }
@Test(expected = IllegalArgumentException.class) public void testSerializedUnsupportedMessage() { final GeneratedMessage mockMessage = Mockito.mock(GeneratedMessage.class); Mockito.doReturn(UnknownFieldSet.getDefaultInstance()).when(mockMessage).getUnknownFields(); AggregationMessage.create(mockMessage).serialize(); }