Skip to content

Commit a2bab28

Browse files
committed
Format JsonResponseFile and JsonResponseFileTest.
1 parent 33b070b commit a2bab28

File tree

2 files changed

+239
-229
lines changed

2 files changed

+239
-229
lines changed

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/JsonResponseFile.java

Lines changed: 165 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -43,192 +43,192 @@
4343
/**
4444
* Converts JSON argument-files to command line arguments.
4545
*
46-
* The user can give a @file.json
47-
* which is converted to command line arguments
48-
* with nested maps becoming --a-b-c
49-
* and arrays getting converted to ':' seperated values.
50-
* The output can be given to joptsimple.
46+
* <p>The user can give a @file.json which is converted to command line arguments with nested maps
47+
* becoming --a-b-c and arrays getting converted to ':' seperated values. The output can be given to
48+
* joptsimple.
5149
*/
5250
public class JsonResponseFile {
5351

54-
private static final Logger LOG = LoggerFactory.getLogger(JsonResponseFile.class);
55-
56-
/** Adds arguments from JSON response files to the argument string(s). */
57-
@Nonnull
58-
public static String[] addResponseFiles(@Nonnull @NonNull String @NonNull [] args) throws IOException {
59-
boolean hasResponseFile = true;
60-
for (int j = 0; hasResponseFile && j < args.length; j++)
61-
if (args[j].charAt(0) == '@')
62-
hasResponseFile = true;
63-
if (!hasResponseFile)
64-
return args;
65-
66-
// Now convert...
67-
List<String> ret = new ArrayList<>();
68-
for (int j = 0; j < args.length; j++) {
69-
if (args[j].charAt(0) != '@')
70-
ret.add(args[j]);
71-
else
72-
ret.addAll(to_arguments(new File(args[j].substring(1))));
52+
private static final Logger LOG = LoggerFactory.getLogger(JsonResponseFile.class);
53+
54+
/** Adds arguments from JSON response files to the argument string(s). */
55+
@Nonnull
56+
public static String[] addResponseFiles(@Nonnull @NonNull String @NonNull [] args)
57+
throws IOException {
58+
boolean hasResponseFile = true;
59+
for (int j = 0; hasResponseFile && j < args.length; j++)
60+
if (args[j].charAt(0) == '@') hasResponseFile = true;
61+
if (!hasResponseFile) return args;
62+
63+
// Now convert...
64+
List<String> ret = new ArrayList<>();
65+
for (int j = 0; j < args.length; j++) {
66+
if (args[j].charAt(0) != '@') ret.add(args[j]);
67+
else ret.addAll(to_arguments(new File(args[j].substring(1))));
68+
}
69+
return ret.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
70+
}
71+
72+
@Nonnull
73+
private static ObjectMapper newObjectMapper() {
74+
return new ObjectMapper(new YAMLFactory());
75+
}
76+
77+
@Nonnull
78+
private static List<? extends String> to_arguments(@Nonnull File file) throws IOException {
79+
return convert(newObjectMapper().readTree(file));
80+
}
81+
82+
@VisibleForTesting
83+
/* pp */ static List<? extends String> to_arguments(@Nonnull String text)
84+
throws IOException, JsonProcessingException {
85+
return convert(newObjectMapper().readTree(text));
86+
}
87+
88+
@Nonnull
89+
private static List<? extends String> convert(@Nonnull JsonNode node) {
90+
List<String> out = new ArrayList<>();
91+
convert(node, out);
92+
return out;
93+
}
94+
95+
private static void convert(@Nonnull JsonNode node, @Nonnull List<? super String> ret) {
96+
Preconditions.checkArgument(node.isObject(), "Object expected as root node of JSON file.");
97+
node.fields()
98+
.forEachRemaining(
99+
entry -> {
100+
String name = entry.getKey();
101+
JsonNode childNode = entry.getValue();
102+
if (name.equals("definitions")) convertDefinitions("-D", "", ".", childNode, ret);
103+
else convertArgument("--" + name, childNode, ret);
104+
});
105+
}
106+
107+
private static void convertDefinitions(
108+
String s, String s1, String s2, JsonNode childnode, List<? super String> ret) {
109+
Preconditions.checkArgument(childnode.isObject(), "Definitions should be a JSON object");
110+
childnode
111+
.fields()
112+
.forEachRemaining(
113+
entry -> {
114+
Preconditions.checkState(
115+
entry.getValue().isTextual(),
116+
"Definitions should be a JSON object with all value nodes.");
117+
ret.add("-D" + entry.getKey() + "=" + entry.getValue().asText());
118+
});
119+
}
120+
121+
/** Converts the given JsonNode into a set of --argument strings, within the given prefix. */
122+
private static void convertArgument(
123+
@Nonnull String prefix, @Nonnull JsonNode node, @Nonnull List<? super String> ret) {
124+
if (node.isObject()) {
125+
node.fields()
126+
.forEachRemaining(
127+
(entry) -> {
128+
String name = entry.getKey();
129+
JsonNode childnode = entry.getValue();
130+
convertArgument(prefix + "-" + name, childnode, ret);
131+
});
132+
} else if (node.isArray()) {
133+
ArrayNode arrayNode = (ArrayNode) node;
134+
StringBuilder sb = new StringBuilder();
135+
boolean first = true;
136+
for (int j = 0; j < arrayNode.size(); j++) {
137+
JsonNode childNode = arrayNode.get(j);
138+
if (!childNode.isValueNode())
139+
throw new IllegalArgumentException(
140+
"Array in json response file can only contain strings");
141+
if (first) {
142+
sb.append(childNode.textValue());
143+
first = false;
144+
} else sb.append(":").append(childNode.textValue());
145+
}
146+
ret.add(prefix);
147+
ret.add(sb.toString());
148+
} else {
149+
ret.add(prefix);
150+
if (!node.textValue().isEmpty()) ret.add(node.textValue());
151+
}
152+
}
153+
154+
public static void save(ConnectorArguments arguments) throws IOException {
155+
File responseFile = FileUtils.getFile(arguments.getResponseFileName());
156+
newObjectMapper().writeValue(responseFile, from_arguments(arguments.getArgs()));
157+
LOG.info("Saved response file to {}", responseFile.getAbsolutePath());
158+
}
159+
160+
public static ResponseFileEntity from_arguments(String[] args) {
161+
ResponseFileEntity root = ResponseFileEntity.OBJECT.get();
162+
String prev = null;
163+
for (String arg : args) {
164+
if (arg.startsWith("-")) {
165+
if (prev != null) root.add(prev);
166+
prev = arg;
167+
} else {
168+
if (prev != null) {
169+
root.add(prev, arg);
170+
prev = null;
73171
}
74-
return ret.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
172+
}
75173
}
174+
if (prev != null) root.add(prev);
76175

77-
@Nonnull
78-
private static ObjectMapper newObjectMapper() {
79-
return new ObjectMapper(new YAMLFactory());
80-
}
176+
return root;
177+
}
81178

82-
@Nonnull
83-
private static List<? extends String> to_arguments(@Nonnull File file) throws IOException {
84-
return convert(newObjectMapper().readTree(file));
85-
}
179+
public static class ResponseFileEntity {
86180

87-
@VisibleForTesting
88-
/* pp */ static List<? extends String> to_arguments(@Nonnull String text) throws IOException, JsonProcessingException {
89-
return convert(newObjectMapper().readTree(text));
90-
}
181+
public static final Supplier<ResponseFileEntity> OBJECT =
182+
() -> new ResponseFileEntity(new LinkedHashMap<>(), null);
183+
public static final Function<String, ResponseFileEntity> VALUE =
184+
value -> new ResponseFileEntity(null, value);
91185

92-
@Nonnull
93-
private static List<? extends String> convert(@Nonnull JsonNode node) {
94-
List<String> out = new ArrayList<>();
95-
convert(node, out);
96-
return out;
97-
}
186+
private final Map<String, ResponseFileEntity> fields;
187+
private final String value;
98188

99-
private static void convert(@Nonnull JsonNode node, @Nonnull List<? super String> ret) {
100-
Preconditions.checkArgument(node.isObject(), "Object expected as root node of JSON file.");
101-
node.fields().forEachRemaining(
102-
entry -> {
103-
String name = entry.getKey();
104-
JsonNode childNode = entry.getValue();
105-
if (name.equals("definitions"))
106-
convertDefinitions("-D", "", ".", childNode, ret);
107-
else
108-
convertArgument("--" + name, childNode, ret);
109-
}
110-
);
189+
private ResponseFileEntity(Map<String, ResponseFileEntity> fields, String value) {
190+
this.fields = fields;
191+
this.value = value;
111192
}
112193

113-
private static void convertDefinitions(String s, String s1, String s2, JsonNode childnode, List<? super String> ret) {
114-
Preconditions.checkArgument(childnode.isObject(), "Definitions should be a JSON object");
115-
childnode.fields().forEachRemaining(entry -> {
116-
Preconditions.checkState(entry.getValue().isTextual(), "Definitions should be a JSON object with all value nodes.");
117-
ret.add("-D" + entry.getKey() + "=" + entry.getValue().asText());
118-
});
194+
@JsonValue
195+
public Object getValue() {
196+
if (fields != null) return fields;
197+
return value;
119198
}
120199

121-
/** Converts the given JsonNode into a set of --argument strings, within the given prefix. */
122-
private static void convertArgument(@Nonnull String prefix, @Nonnull JsonNode node, @Nonnull List<? super String> ret) {
123-
if (node.isObject()) {
124-
node.fields().forEachRemaining((entry) -> {
125-
String name = entry.getKey();
126-
JsonNode childnode = entry.getValue();
127-
convertArgument(prefix + "-" + name, childnode, ret);
128-
});
129-
} else if (node.isArray()) {
130-
ArrayNode arrayNode = (ArrayNode) node;
131-
StringBuilder sb = new StringBuilder();
132-
boolean first = true;
133-
for (int j = 0; j < arrayNode.size(); j++) {
134-
JsonNode childNode = arrayNode.get(j);
135-
if (!childNode.isValueNode())
136-
throw new IllegalArgumentException("Array in json response file can only contain strings");
137-
if (first) {
138-
sb.append(childNode.textValue());
139-
first = false;
140-
} else
141-
sb.append(":").append(childNode.textValue());
142-
}
143-
ret.add(prefix);
144-
ret.add(sb.toString());
145-
} else {
146-
ret.add(prefix);
147-
if (!node.textValue().isEmpty())
148-
ret.add(node.textValue());
149-
}
200+
public void put(String[] tokens, String value) {
201+
if (tokens.length == 1) {
202+
put(tokens[0], value);
203+
} else {
204+
fields
205+
.computeIfAbsent(tokens[0], j -> OBJECT.get())
206+
.put(ArrayUtils.subarray(tokens, 1, tokens.length), value);
207+
}
150208
}
151209

152-
public static void save(ConnectorArguments arguments) throws IOException {
153-
File responseFile = FileUtils.getFile(arguments.getResponseFileName());
154-
newObjectMapper().writeValue(responseFile, from_arguments(arguments.getArgs()));
155-
LOG.info("Saved response file to {}", responseFile.getAbsolutePath());
210+
private void put(String token, String value) {
211+
fields.put(token, new ResponseFileEntity(null, value));
156212
}
157213

158-
public static ResponseFileEntity from_arguments(String[] args) {
159-
ResponseFileEntity root = ResponseFileEntity.OBJECT.get();
160-
String prev = null;
161-
for (String arg : args) {
162-
if (arg.startsWith("-")) {
163-
if (prev != null)
164-
root.add(prev);
165-
prev = arg;
166-
} else {
167-
if (prev != null) {
168-
root.add(prev, arg);
169-
prev = null;
170-
}
171-
}
172-
}
173-
if (prev != null)
174-
root.add(prev);
175-
176-
return root;
214+
public void add(String flag) {
215+
add(flag, Boolean.TRUE.toString());
177216
}
178217

179-
public static class ResponseFileEntity {
180-
181-
public static final Supplier<ResponseFileEntity> OBJECT = () -> new ResponseFileEntity(new LinkedHashMap<>(), null);
182-
public static final Function<String, ResponseFileEntity> VALUE = value -> new ResponseFileEntity(null, value);
183-
184-
private final Map<String, ResponseFileEntity> fields;
185-
private final String value;
218+
public void add(String argument, String value) {
186219

187-
private ResponseFileEntity(Map<String, ResponseFileEntity> fields, String value) {
188-
this.fields = fields;
189-
this.value = value;
190-
}
191-
192-
@JsonValue
193-
public Object getValue() {
194-
if (fields != null)
195-
return fields;
196-
return value;
197-
}
220+
if (argument.equals("--save-response-file")) return; // don't save over and over again
198221

199-
public void put(String[] tokens, String value) {
200-
if (tokens.length == 1) {
201-
put(tokens[0], value);
202-
} else {
203-
fields.computeIfAbsent(tokens[0], j -> OBJECT.get())
204-
.put(ArrayUtils.subarray(tokens, 1, tokens.length), value);
205-
}
206-
}
207-
208-
private void put(String token, String value) {
209-
fields.put(token, new ResponseFileEntity(null, value));
210-
}
211-
212-
public void add(String flag) {
213-
add(flag, Boolean.TRUE.toString());
214-
}
215-
216-
public void add(String argument, String value) {
217-
218-
if (argument.equals("--save-response-file"))
219-
return; // don't save over and over again
220-
221-
if (argument.startsWith("-D")) {
222-
argument = argument.substring(2);
223-
int idx = argument.indexOf('=');
224-
ResponseFileEntity definitions = fields.computeIfAbsent("definitions", d -> OBJECT.get());
225-
definitions.put(argument.substring(0, idx), argument.substring(idx + 1));
226-
} else {
227-
argument = argument.substring(2);
228-
String[] tokens = StringUtils.split(argument, "-");
229-
put(tokens, value);
230-
}
231-
}
222+
if (argument.startsWith("-D")) {
223+
argument = argument.substring(2);
224+
int idx = argument.indexOf('=');
225+
ResponseFileEntity definitions = fields.computeIfAbsent("definitions", d -> OBJECT.get());
226+
definitions.put(argument.substring(0, idx), argument.substring(idx + 1));
227+
} else {
228+
argument = argument.substring(2);
229+
String[] tokens = StringUtils.split(argument, "-");
230+
put(tokens, value);
231+
}
232232
}
233-
233+
}
234234
}

0 commit comments

Comments
 (0)