/** Converts the provided {@link Struct} into {@link PipelineOptions}. */ public static PipelineOptions fromProto(Struct protoOptions) { try { Map<String, TreeNode> mapWithoutUrns = new HashMap<>(); TreeNode rootOptions = MAPPER.readTree(JsonFormat.printer().print(protoOptions)); Iterator<String> optionsKeys = rootOptions.fieldNames(); while (optionsKeys.hasNext()) { String optionKey = optionsKeys.next(); TreeNode optionValue = rootOptions.get(optionKey); mapWithoutUrns.put( CaseFormat.LOWER_UNDERSCORE.to( CaseFormat.LOWER_CAMEL, optionKey.substring("beam:option:".length(), optionKey.length() - ":v1".length())), optionValue); } return MAPPER.readValue( MAPPER.writeValueAsString(ImmutableMap.of("options", mapWithoutUrns)), PipelineOptions.class); } catch (IOException e) { throw new RuntimeException("Failed to read PipelineOptions from Protocol", e); } }
/** Converts the provided {@link PipelineOptions} to a {@link Struct}. */ public static Struct toProto(PipelineOptions options) { Struct.Builder builder = Struct.newBuilder(); try { // TODO: Officially define URNs for options and their scheme. TreeNode treeNode = MAPPER.valueToTree(options); TreeNode rootOptions = treeNode.get("options"); Iterator<String> optionsKeys = rootOptions.fieldNames(); Map<String, TreeNode> optionsUsingUrns = new HashMap<>(); while (optionsKeys.hasNext()) { String optionKey = optionsKeys.next(); TreeNode optionValue = rootOptions.get(optionKey); optionsUsingUrns.put( "beam:option:" + CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, optionKey) + ":v1", optionValue); } // 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(optionsUsingUrns), builder); return builder.build(); } catch (IOException e) { throw new RuntimeException("Failed to convert PipelineOptions to Protocol", e); } }
private void hash(TreeNode node) { if (node.isArray()) { md.update((byte)'l'); for (int i = 0; i < node.size(); i++) { hash(node.get(i)); } md.update((byte)'e'); } else if (node.isObject()) { String[] fieldNames = new String[node.size()]; Iterator<String> it = node.fieldNames(); for (int i=0; i< fieldNames.length; i++) { fieldNames[i] = it.next(); } Arrays.sort(fieldNames); md.update((byte)'d'); for (String fName : fieldNames) { hash(fName); hash(node.get(fName)); } md.update((byte)'e'); } else if (node.isValueNode()) { String val = ((JsonNode)node).textValue(); if (val != null) { hash(val); } } else { throw new IllegalArgumentException(node.toString()); } }
protected void compareObjects(TreeNode a, TreeNode b) { assertThat(a.isObject()).isEqualTo(b.isObject()); assertThat(a.size()).isEqualTo(b.size()); Iterator<String> fnames = a.fieldNames(); while (fnames.hasNext()) { String key = fnames.next(); assertThat(a.get(key)).isEqualTo(b.get(key)); } }
final TreeNode typesNodes = readTree.get(typesNodeName); if (typesNodes != null) { final Iterator<String> typeFields = typesNodes.fieldNames(); while (typeFields.hasNext()) { final String key = typeFields.next(); final Iterator<String> fieldNames = readTree.fieldNames(); while (fieldNames.hasNext()) { final String key = fieldNames.next();
Iterator<String> fieldNames = node.fieldNames(); while (fieldNames.hasNext()) { String fieldName = fieldNames.next();