Java Code Examples for com.google.protobuf.TextFormat#merge()

The following examples show how to use com.google.protobuf.TextFormat#merge() . 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: BigQueryInputPartitionReaderTest.java    From spark-bigquery-connector with Apache License 2.0 6 votes vote down vote up
@Test
public void testReadAvro() throws Exception {
    TableInfo allTypesTableInfo = allTypesTableInfo();
    ReadRowsResponse.Builder readRowsResponse = ReadRowsResponse.newBuilder();
    TextFormat.merge(ALL_TYPES_TABLE_READ_ROWS_RESPONSE_STR, readRowsResponse);
    Iterator<ReadRowsResponse> readRowsResponses = ImmutableList.of(readRowsResponse.build()).iterator();

    ReadRowsResponseToInternalRowIteratorConverter converter =
            ReadRowsResponseToInternalRowIteratorConverter.avro(
                    ALL_TYPES_TABLE_BIGQUERY_SCHEMA,
                    ALL_TYPES_TABLE_FIELDS,
                    ALL_TYPES_TABLE_AVRO_RAW_SCHEMA
            );

    BigQueryInputPartitionReader reader = new BigQueryInputPartitionReader(readRowsResponses, converter, null);

    assertThat(reader.next()).isTrue();
    InternalRow row = reader.get();
    assertThat(reader.next()).isFalse();
    assertThat(row.numFields()).isEqualTo(15);
    assertThat(row.getString(0)).isEqualTo("hello");

}
 
Example 2
Source File: AndroidManifestTest.java    From bundletool with Apache License 2.0 6 votes vote down vote up
@Test
public void configSplit_noExtraElementsFromModuleSplit() throws Exception {
  XmlNode.Builder xmlNodeBuilder = XmlNode.newBuilder();
  TextFormat.merge(TestData.openReader("testdata/manifest/manifest1.textpb"), xmlNodeBuilder);
  AndroidManifest androidManifest = AndroidManifest.create(xmlNodeBuilder.build());

  XmlNode.Builder expectedXmlNodeBuilder = XmlNode.newBuilder();
  TextFormat.merge(
      TestData.openReader("testdata/manifest/config_split_manifest1.textpb"),
      expectedXmlNodeBuilder);

  AndroidManifest configManifest =
      AndroidManifest.createForConfigSplit(
          androidManifest.getPackageName(),
          androidManifest.getVersionCode(),
          "testModule.config.hdpi",
          "testModule",
          Optional.empty());

  XmlProtoNode generatedManifest = configManifest.getManifestRoot();
  assertThat(generatedManifest.getProto())
      .ignoringRepeatedFieldOrder()
      .isEqualTo(expectedXmlNodeBuilder.build());
}
 
Example 3
Source File: ProtobufHttpMessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected Message readInternal(Class<? extends Message> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	MediaType contentType = inputMessage.getHeaders().getContentType();
	if (contentType == null) {
		contentType = PROTOBUF;
	}
	Charset charset = contentType.getCharset();
	if (charset == null) {
		charset = DEFAULT_CHARSET;
	}

	Message.Builder builder = getMessageBuilder(clazz);
	if (PROTOBUF.isCompatibleWith(contentType)) {
		builder.mergeFrom(inputMessage.getBody(), this.extensionRegistry);
	}
	else if (TEXT_PLAIN.isCompatibleWith(contentType)) {
		InputStreamReader reader = new InputStreamReader(inputMessage.getBody(), charset);
		TextFormat.merge(reader, this.extensionRegistry, builder);
	}
	else if (this.protobufFormatSupport != null) {
		this.protobufFormatSupport.merge(
				inputMessage.getBody(), charset, contentType, this.extensionRegistry, builder);
	}
	return builder.build();
}
 
Example 4
Source File: TestConfig.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
/** Parses the config file, in proto text format, and returns it. */
public Service getApiProtoConfig(String configFileName) throws ParseException {
  String content = readTestData(configFileName);
  Service.Builder builder = Service.newBuilder();
  TextFormat.merge(content, builder);
  return builder.build();
}
 
Example 5
Source File: ProtobufHttpMessageConverter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected Message readInternal(Class<? extends Message> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	MediaType contentType = inputMessage.getHeaders().getContentType();
	contentType = (contentType != null ? contentType : PROTOBUF);

	Charset charset = getCharset(inputMessage.getHeaders());
	InputStreamReader reader = new InputStreamReader(inputMessage.getBody(), charset);

	try {
		Message.Builder builder = getMessageBuilder(clazz);

		if (MediaType.APPLICATION_JSON.isCompatibleWith(contentType)) {
			JsonFormat.merge(reader, this.extensionRegistry, builder);
		}
		else if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
			TextFormat.merge(reader, this.extensionRegistry, builder);
		}
		else if (MediaType.APPLICATION_XML.isCompatibleWith(contentType)) {
			XmlFormat.merge(reader, this.extensionRegistry, builder);
		}
		else {
			builder.mergeFrom(inputMessage.getBody(), this.extensionRegistry);
		}
		return builder.build();
	}
	catch (Exception e) {
		throw new HttpMessageNotReadableException("Could not read Protobuf message: " + e.getMessage(), e);
	}
}
 
Example 6
Source File: Worker.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
private static WorkerConfig toWorkerConfig(Readable input, WorkerOptions options)
    throws IOException {
  WorkerConfig.Builder builder = WorkerConfig.newBuilder();
  TextFormat.merge(input, builder);
  if (!Strings.isNullOrEmpty(options.root)) {
    builder.setRoot(options.root);
  }

  if (!Strings.isNullOrEmpty(options.casCacheDirectory)) {
    builder.setCasCacheDirectory(options.casCacheDirectory);
  }
  return builder.build();
}
 
Example 7
Source File: BuildFarmServer.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
private static BuildFarmServerConfig toBuildFarmServerConfig(
    Readable input, BuildFarmServerOptions options) throws IOException {
  BuildFarmServerConfig.Builder builder = BuildFarmServerConfig.newBuilder();
  TextFormat.merge(input, builder);
  if (options.port > 0) {
    builder.setPort(options.port);
  }
  return builder.build();
}
 
Example 8
Source File: EditLogLedgerMetadata.java    From hadoop with Apache License 2.0 5 votes vote down vote up
static EditLogLedgerMetadata read(ZooKeeper zkc, String path)
    throws IOException, KeeperException.NoNodeException  {
  try {
    byte[] data = zkc.getData(path, false, null);

    EditLogLedgerProto.Builder builder = EditLogLedgerProto.newBuilder();
    if (LOG.isDebugEnabled()) {
      LOG.debug("Reading " + path + " data: " + new String(data, UTF_8));
    }
    TextFormat.merge(new String(data, UTF_8), builder);
    if (!builder.isInitialized()) {
      throw new IOException("Invalid/Incomplete data in znode");
    }
    EditLogLedgerProto ledger = builder.build();

    int dataLayoutVersion = ledger.getDataLayoutVersion();
    long ledgerId = ledger.getLedgerId();
    long firstTxId = ledger.getFirstTxId();
    if (ledger.hasLastTxId()) {
      long lastTxId = ledger.getLastTxId();
      return new EditLogLedgerMetadata(path, dataLayoutVersion,
                                       ledgerId, firstTxId, lastTxId);
    } else {
      return new EditLogLedgerMetadata(path, dataLayoutVersion,
                                       ledgerId, firstTxId);
    }
  } catch(KeeperException.NoNodeException nne) {
    throw nne;
  } catch(KeeperException ke) {
    throw new IOException("Error reading from zookeeper", ke);
  } catch (InterruptedException ie) {
    throw new IOException("Interrupted reading from zookeeper", ie);
  }
}
 
Example 9
Source File: ObjectDetectionTensorflowOutputConverter.java    From tensorflow with Apache License 2.0 5 votes vote down vote up
/**
 * Loads object labels in the string_int_label_map.proto
 * @param labelsResource
 * @return
 * @throws Exception
 */
private static String[] loadLabels(Resource labelsResource) throws Exception {
	try (InputStream is = labelsResource.getInputStream()) {
		String text = StreamUtils.copyToString(is, Charset.forName("UTF-8"));
		StringIntLabelMapOuterClass.StringIntLabelMap.Builder builder =
				StringIntLabelMapOuterClass.StringIntLabelMap.newBuilder();
		TextFormat.merge(text, builder);
		StringIntLabelMapOuterClass.StringIntLabelMap proto = builder.build();

		int maxLabelId = proto.getItemList().stream()
				.map(StringIntLabelMapOuterClass.StringIntLabelMapItem::getId)
				.max(Comparator.comparing(i -> i))
				.orElse(-1);

		String[] labelIdToNameMap = new String[maxLabelId + 1];
		for (StringIntLabelMapOuterClass.StringIntLabelMapItem item : proto.getItemList()) {
			if (!StringUtils.isEmpty(item.getDisplayName())) {
				labelIdToNameMap[item.getId()] = item.getDisplayName();
			}
			else {
				// Common practice is to set the name to a MID or Synsets Id. Synset is a set of synonyms that
				// share a common meaning: https://en.wikipedia.org/wiki/WordNet
				labelIdToNameMap[item.getId()] = item.getName();
			}
		}
		return labelIdToNameMap;
	}
}
 
Example 10
Source File: TFTrainer.java    From OpenLabeler with Apache License 2.0 5 votes vote down vote up
public static List<LabelMapItem> getLabelMapItems(Path path) {
    try {
        if (path.toFile().exists()) {
            String text = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
            StringIntLabelMap.Builder builder = StringIntLabelMap.newBuilder();
            TextFormat.merge(text, builder);
            StringIntLabelMap proto = builder.build();
            return proto.getItemList().stream().map(i -> new LabelMapItem(i)).collect(Collectors.toList());
        }
    }
    catch (Exception ex) {
        LOG.log(Level.SEVERE, "Unable to parse label map", ex);
    }
    return Collections.emptyList();
}
 
Example 11
Source File: XmlProtoNodeBuilderTest.java    From bundletool with Apache License 2.0 5 votes vote down vote up
@Test
public void modifyingDeepElementReflectsChangeInParentsToo() throws Exception {
  XmlNode.Builder xmlNode = XmlNode.newBuilder();
  TextFormat.merge(TestData.openReader("testdata/manifest/manifest1.textpb"), xmlNode);

  XmlProtoNodeBuilder node = new XmlProtoNode(xmlNode.build()).toBuilder();
  node.getElement()
      .getChildElement("uses-sdk")
      .getOrCreateAndroidAttribute("minSdkVersion", 0x0101020c)
      .setValueAsDecimalInteger(999);

  assertThat(
          node.build()
              .getProto()
              .getElement()
              .getChildList()
              .stream()
              .filter(childNode -> childNode.getElement().getName().equals("uses-sdk"))
              .collect(onlyElement())
              .getElement()
              .getAttributeList()
              .stream()
              .filter(attribute -> attribute.getName().equals("minSdkVersion"))
              .collect(onlyElement())
              .getCompiledItem()
              .getPrim()
              .getIntDecimalValue())
      .isEqualTo(999);
}
 
Example 12
Source File: FieldMasksTest.java    From google-ads-java with Apache License 2.0 5 votes vote down vote up
@Parameters(name = "{index}: {0}")
public static Collection<Object[]> data() throws IOException {
  InputStream stream = FieldMasksTest.class.getResourceAsStream("/testdata/test_cases.textproto");
  Readable readable = new InputStreamReader(stream, Charsets.UTF_8);

  TestSuite.Builder builder = TestSuite.newBuilder();
  TextFormat.merge(readable, builder);
  TestSuite testSuite = builder.build();

  List<Object[]> result = new ArrayList<>();
  for (TestCase testCase : testSuite.getTestCasesList()) {
    result.add(new Object[] {testCase.getDescription(), testCase});
  }
  return result;
}
 
Example 13
Source File: EditLogLedgerMetadata.java    From big-c with Apache License 2.0 5 votes vote down vote up
static EditLogLedgerMetadata read(ZooKeeper zkc, String path)
    throws IOException, KeeperException.NoNodeException  {
  try {
    byte[] data = zkc.getData(path, false, null);

    EditLogLedgerProto.Builder builder = EditLogLedgerProto.newBuilder();
    if (LOG.isDebugEnabled()) {
      LOG.debug("Reading " + path + " data: " + new String(data, UTF_8));
    }
    TextFormat.merge(new String(data, UTF_8), builder);
    if (!builder.isInitialized()) {
      throw new IOException("Invalid/Incomplete data in znode");
    }
    EditLogLedgerProto ledger = builder.build();

    int dataLayoutVersion = ledger.getDataLayoutVersion();
    long ledgerId = ledger.getLedgerId();
    long firstTxId = ledger.getFirstTxId();
    if (ledger.hasLastTxId()) {
      long lastTxId = ledger.getLastTxId();
      return new EditLogLedgerMetadata(path, dataLayoutVersion,
                                       ledgerId, firstTxId, lastTxId);
    } else {
      return new EditLogLedgerMetadata(path, dataLayoutVersion,
                                       ledgerId, firstTxId);
    }
  } catch(KeeperException.NoNodeException nne) {
    throw nne;
  } catch(KeeperException ke) {
    throw new IOException("Error reading from zookeeper", ke);
  } catch (InterruptedException ie) {
    throw new IOException("Interrupted reading from zookeeper", ie);
  }
}
 
Example 14
Source File: FileUtils.java    From startup-os with Apache License 2.0 5 votes vote down vote up
/** Reads a prototxt file into a proto from zip archive. */
public Message readPrototxtFromZip(
    String zipFilePath, String pathInsideZip, Message.Builder builder) throws IOException {
  ZipFile zipFile =
      new ZipFile(expandHomeDirectory(joinPaths(getCurrentWorkingDirectory(), zipFilePath)));
  String content = streamToString(zipFile.getInputStream(zipFile.getEntry(pathInsideZip)));
  TextFormat.merge(content, builder);
  return builder.build();
}
 
Example 15
Source File: ProtobufHttpMessageConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected Message readInternal(Class<? extends Message> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	MediaType contentType = inputMessage.getHeaders().getContentType();
	if (contentType == null) {
		contentType = PROTOBUF;
	}
	Charset charset = contentType.getCharset();
	if (charset == null) {
		charset = DEFAULT_CHARSET;
	}

	Message.Builder builder = getMessageBuilder(clazz);
	if (PROTOBUF.isCompatibleWith(contentType)) {
		builder.mergeFrom(inputMessage.getBody(), this.extensionRegistry);
	}
	else if (TEXT_PLAIN.isCompatibleWith(contentType)) {
		InputStreamReader reader = new InputStreamReader(inputMessage.getBody(), charset);
		TextFormat.merge(reader, this.extensionRegistry, builder);
	}
	else if (this.protobufFormatSupport != null) {
		this.protobufFormatSupport.merge(
				inputMessage.getBody(), charset, contentType, this.extensionRegistry, builder);
	}
	return builder.build();
}
 
Example 16
Source File: MergerTest.java    From api-compiler with Apache License 2.0 4 votes vote down vote up
private void createApiWithHttpConfig(String methodKind, String path, String body,
    boolean isCustom)
    throws Exception {
  String source =
      "syntax = \"proto2\"; "
          + "package protiary.test;"
          + "message M {"
          + "  optional string a = 1;"
          + "  required N b = 2;"
          + "  optional string c = 3;"
          + "  repeated string d = 4;"
          + "}"
          + "message N {"
          + "  optional string a = 1;"
          + "}"
          + "service S {"
          + "  rpc Rpc(M) returns (N);"
          + "}";

  Service.Builder configBuilder = Service.newBuilder();
  TextFormat.merge(
      String.format(
          "name: \"blob.googleapis.com\" "
          + "config_version {"
          +  "value: 3"
          + "}"
          + "http { rules { selector: \"protiary.test.S.Rpc\" "
          + (isCustom ? " custom{ kind: \"%s\" path: \"%s\" }" : " %s: \"%s\" ")
          + " %s  } }",
          methodKind, path, body == null ? "" : "body: \"" + body + "\""),
      configBuilder);

  TestDataLocator locator = TestDataLocator.create(MergerTest.class);
  locator.injectVirtualTestData("source.proto", source);
  testConfig = new TestConfig(locator, tempDir.getRoot().getPath(),
      ImmutableList.of("source.proto"));
  model = testConfig.createModel(ImmutableList.<String>of());
  StandardSetup.registerStandardProcessors(model);
  model.setServiceConfig(configBuilder.build());
  StandardSetup.registerStandardConfigAspects(model);
}
 
Example 17
Source File: InstrumentFunctions.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates an instrument functions compiler pass.
 *
 * @param compiler          The JSCompiler
 * @param functionNames     Assigned function identifiers.
 * @param templateFilename  Template filename; for use during error
 *                          reporting only.
 * @param appNameStr        String to pass to appNameSetter.
 * @param readable          Instrumentation template protobuf text.
 */
InstrumentFunctions(AbstractCompiler compiler,
                    FunctionNames functionNames,
                    String templateFilename,
                    String appNameStr,
                    Readable readable) {
  this.compiler = compiler;
  this.functionNames = functionNames;
  this.templateFilename = templateFilename;
  this.appNameStr = appNameStr;

  Instrumentation.Builder builder = Instrumentation.newBuilder();
  try {
    TextFormat.merge(readable, builder);
  } catch (IOException e) {
    compiler.report(JSError.make(RhinoErrorReporter.PARSE_ERROR,
        "Error reading instrumentation template protobuf at " +
        templateFilename));
    this.initCodeSource = "";
    this.definedFunctionName = "";
    this.reportFunctionName = "";
    this.reportFunctionExitName = "";
    this.appNameSetter = "";
    this.declarationsToRemove = Lists.newArrayList();
    return;
  }

  Instrumentation template = builder.build();

  StringBuilder initCodeSourceBuilder = new StringBuilder();
  for (String line : template.getInitList()) {
    initCodeSourceBuilder.append(line).append("\n");
  }
  this.initCodeSource = initCodeSourceBuilder.toString();

  this.definedFunctionName = template.getReportDefined();
  this.reportFunctionName = template.getReportCall();
  this.reportFunctionExitName = template.getReportExit();
  this.appNameSetter = template.getAppNameSetter();

  this.declarationsToRemove = ImmutableList.copyOf(
      template.getDeclarationToRemoveList());
}
 
Example 18
Source File: CppActionConfigs.java    From bazel with Apache License 2.0 4 votes vote down vote up
private static CToolchain.ActionConfig getActionConfig(String protoText) throws ParseException {
  CToolchain.ActionConfig.Builder actionConfig = CToolchain.ActionConfig.newBuilder();
  TextFormat.merge(protoText, actionConfig);
  return actionConfig.build();
}
 
Example 19
Source File: FileUtils.java    From startup-os with Apache License 2.0 4 votes vote down vote up
/** Reads a prototxt from a string into a proto. */
public Message readPrototxtFromString(String prototxt, Message.Builder builder)
    throws IOException {
  TextFormat.merge(prototxt, builder);
  return builder.build();
}
 
Example 20
Source File: FileUtils.java    From startup-os with Apache License 2.0 4 votes vote down vote up
/** Reads a prototxt file into a proto. */
public Message readPrototxt(String path, Message.Builder builder) throws IOException {
  String protoText = readFile(path);
  TextFormat.merge(protoText, builder);
  return builder.build();
}