/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.beam.runners.core.construction;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.protobuf.Struct;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.util.common.ReflectHelpers;

/** Utilities for going to/from Runner API pipeline options. */
public class PipelineOptionsTranslation {
  private static final ObjectMapper MAPPER =
      new ObjectMapper()
          .registerModules(ObjectMapper.findModules(ReflectHelpers.findClassLoader()));

  /** Converts the provided {@link PipelineOptions} to a {@link Struct}. */
  public static Struct toProto(PipelineOptions options) {
    Struct.Builder builder = Struct.newBuilder();
    try {
      // The JSON format of a Protobuf Struct is the JSON object that is equivalent to that struct
      // (with values encoded in a standard json-codeable manner). See Beam PR 3719 for more.
      JsonFormat.parser().merge(MAPPER.writeValueAsString(options), builder);
      return builder.build();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  /** Converts the provided {@link Struct} into {@link PipelineOptions}. */
  public static PipelineOptions fromProto(Struct protoOptions) throws IOException {
    return MAPPER.readValue(JsonFormat.printer().print(protoOptions), PipelineOptions.class);
  }
}

@@ -885,29 +885,296 @@
 son.
-databind.ObjectMapper
+core.JsonToken;%0Aimport com.fasterxml.jackson.core.TreeNode;%0Aimport com.fasterxml.jackson.core.type.TypeReference;%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0Aimport com.google.common.base.CaseFormat;%0Aimport com.google.common.collect.ImmutableMap;%0Aimport com.google.gson.JsonObject
 ;%0Aim
@@ -1278,16 +1278,91 @@
 eption;%0A
+import java.util.HashMap;%0Aimport java.util.Iterator;%0Aimport java.util.Map;%0A
 import o
@@ -1464,16 +1464,19 @@
 rs;%0A%0A/**
+%0A *
  Utiliti
@@ -1524,16 +1524,95 @@
 options.
+%0A *%0A * %3Cp%3ETODO: Make this the default to/from translation for PipelineOptions.%0A
  */%0Apubl
@@ -1988,24 +1988,25 @@
 wBuilder();%0A
+%0A
     try %7B%0A  
@@ -2003,16 +2003,649 @@
   try %7B%0A
+      // TODO: Officially define URNs for options and their scheme.%0A      TreeNode treeNode = MAPPER.valueToTree(options);%0A      TreeNode rootOptions = treeNode.get(%22options%22);%0A      Iterator%3CString%3E optionsKeys = rootOptions.fieldNames();%0A      Map%3CString, TreeNode%3E optionsUsingUrns = new HashMap%3CString, TreeNode%3E();%0A      while (optionsKeys.hasNext()) %7B%0A        String optionKey = optionsKeys.next();%0A        TreeNode optionValue = rootOptions.get(optionKey);%0A        optionsUsingUrns.put(%0A            %22beam:option:%22 + CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, optionKey) + %22:v1%22,%0A            optionValue);%0A      %7D%0A%0A
       //
@@ -2894,16 +2894,25 @@
 (options
+UsingUrns
 ), build
@@ -3192,73 +3192,723 @@
     
-return MAPPER.readValue(JsonFormat.printer().print(protoOptions),
+Map%3CString, TreeNode%3E mapWithoutUrns = new HashMap%3CString, TreeNode%3E();%0A    TreeNode rootOptions = MAPPER.readTree(%0A        JsonFormat.printer().print(protoOptions));%0A    Iterator%3CString%3E optionsKeys = rootOptions.fieldNames();%0A    while (optionsKeys.hasNext()) %7B%0A      String optionKey = optionsKeys.next();%0A      TreeNode optionValue = rootOptions.get(optionKey);%0A      mapWithoutUrns.put(%0A          CaseFormat.LOWER_UNDERSCORE.to(%0A              CaseFormat.LOWER_CAMEL,%0A              optionKey.substring(%22beam:option:%22.length(), optionKey.length() - %22:v1%22.length())),%0A          optionValue);%0A    %7D%0A    return MAPPER.readValue(%0A        MAPPER.writeValueAsString(ImmutableMap.of(%22options%22, mapWithoutUrns)),%0A       
  Pip

Use the beam:option:value:v1 as the portable pipeline options representation