Search in sources :

Example 1 with UnknownFieldSet

use of com.google.protobuf.UnknownFieldSet in project compiler by boalang.

the class JsonFormat method printUnknownFields.

protected static void printUnknownFields(UnknownFieldSet unknownFields, JsonGenerator generator) throws IOException {
    boolean firstField = true;
    for (Map.Entry<Integer, UnknownFieldSet.Field> entry : unknownFields.asMap().entrySet()) {
        UnknownFieldSet.Field field = entry.getValue();
        if (firstField) {
            firstField = false;
        } else {
            generator.print(", ");
        }
        generator.print("\"");
        generator.print(entry.getKey().toString());
        generator.print("\"");
        generator.print(": [");
        boolean firstValue = true;
        for (long value : field.getVarintList()) {
            if (firstValue) {
                firstValue = false;
            } else {
                generator.print(", ");
            }
            generator.print(unsignedToString(value));
        }
        for (int value : field.getFixed32List()) {
            if (firstValue) {
                firstValue = false;
            } else {
                generator.print(", ");
            }
            generator.print(String.format((Locale) null, "0x%08x", value));
        }
        for (long value : field.getFixed64List()) {
            if (firstValue) {
                firstValue = false;
            } else {
                generator.print(", ");
            }
            generator.print(String.format((Locale) null, "0x%016x", value));
        }
        for (ByteString value : field.getLengthDelimitedList()) {
            if (firstValue) {
                firstValue = false;
            } else {
                generator.print(", ");
            }
            generator.print("\"");
            generator.print(escapeBytes(value));
            generator.print("\"");
        }
        for (UnknownFieldSet value : field.getGroupList()) {
            if (firstValue) {
                firstValue = false;
            } else {
                generator.print(", ");
            }
            generator.print("{\n");
            printUnknownFields(value, generator);
            generator.print("}\n");
        }
        generator.print("]");
    }
}
Also used : BigInteger(java.math.BigInteger) Locale(java.util.Locale) ByteString(com.google.protobuf.ByteString) Map(java.util.Map) UnknownFieldSet(com.google.protobuf.UnknownFieldSet)

Example 2 with UnknownFieldSet

use of com.google.protobuf.UnknownFieldSet in project jvm-serializers by eishay.

the class JsonFormat method printUnknownFields.

private static void printUnknownFields(UnknownFieldSet unknownFields, JsonGenerator generator) throws IOException {
    for (Map.Entry<Integer, UnknownFieldSet.Field> entry : unknownFields.asMap().entrySet()) {
        //            String prefix = entry.getKey().toString() + ": ";
        UnknownFieldSet.Field field = entry.getValue();
        for (long value : field.getVarintList()) {
            generator.print("\"");
            generator.print(entry.getKey().toString());
            generator.print("\"");
            generator.print(": ");
            generator.print(unsignedToString(value));
            generator.print("\n");
        }
        for (int value : field.getFixed32List()) {
            generator.print("\"");
            generator.print(entry.getKey().toString());
            generator.print("\"");
            generator.print(": ");
            generator.print(String.format((Locale) null, "0x%08x", value));
            generator.print("\n");
        }
        for (long value : field.getFixed64List()) {
            generator.print(entry.getKey().toString());
            generator.print(": ");
            generator.print(String.format((Locale) null, "0x%016x", value));
            generator.print("\n");
        }
        for (ByteString value : field.getLengthDelimitedList()) {
            generator.print(entry.getKey().toString());
            generator.print(": \"");
            generator.print(escapeBytes(value));
            generator.print("\"\n");
        }
        for (UnknownFieldSet value : field.getGroupList()) {
            generator.print(entry.getKey().toString());
            generator.print(" {\n");
            generator.indent();
            printUnknownFields(value, generator);
            generator.outdent();
            generator.print("}\n");
        }
    }
}
Also used : BigInteger(java.math.BigInteger) Locale(java.util.Locale) ByteString(com.google.protobuf.ByteString) Map(java.util.Map) UnknownFieldSet(com.google.protobuf.UnknownFieldSet)

Example 3 with UnknownFieldSet

use of com.google.protobuf.UnknownFieldSet in project gerrit by GerritCodeReview.

the class ProtobufImport method run.

@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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ByteString(com.google.protobuf.ByteString) TextProgressMonitor(org.eclipse.jgit.lib.TextProgressMonitor) ProgressMonitor(org.eclipse.jgit.lib.ProgressMonitor) TextProgressMonitor(org.eclipse.jgit.lib.TextProgressMonitor) BufferedInputStream(java.io.BufferedInputStream) Injector(com.google.inject.Injector) RelationModel(com.google.gwtorm.schema.RelationModel) UnknownFieldSet(com.google.protobuf.UnknownFieldSet) HashMap(java.util.HashMap) Map(java.util.Map) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) JavaSchemaModel(com.google.gwtorm.schema.java.JavaSchemaModel)

Aggregations

ByteString (com.google.protobuf.ByteString)3 UnknownFieldSet (com.google.protobuf.UnknownFieldSet)3 Map (java.util.Map)3 BigInteger (java.math.BigInteger)2 Locale (java.util.Locale)2 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)1 RelationModel (com.google.gwtorm.schema.RelationModel)1 JavaSchemaModel (com.google.gwtorm.schema.java.JavaSchemaModel)1 Injector (com.google.inject.Injector)1 BufferedInputStream (java.io.BufferedInputStream)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 ProgressMonitor (org.eclipse.jgit.lib.ProgressMonitor)1 TextProgressMonitor (org.eclipse.jgit.lib.TextProgressMonitor)1