rdblue commented on a change in pull request #2465:
URL: https://github.com/apache/iceberg/pull/2465#discussion_r615319289
##########
File path: core/src/main/java/org/apache/iceberg/util/JsonUtil.java
##########
@@ -117,18 +119,67 @@ public static String getStringOrNull(String property,
JsonNode node) {
public static List<String> getStringList(String property, JsonNode node) {
Preconditions.checkArgument(node.has(property), "Cannot parse missing list
%s", property);
- JsonNode pNode = node.get(property);
- Preconditions.checkArgument(pNode != null && !pNode.isNull() &&
pNode.isArray(),
- "Cannot parse %s from non-array value: %s", property, pNode);
-
- ImmutableList.Builder<String> builder = ImmutableList.builder();
- Iterator<JsonNode> elements = pNode.elements();
- while (elements.hasNext()) {
- JsonNode element = elements.next();
- Preconditions.checkArgument(element.isTextual(),
- "Cannot parse string from non-text value: %s", element);
- builder.add(element.asText());
+ return ImmutableList.<String>builder()
+ .addAll(new JsonStringArrayVisitor(property, node))
+ .build();
+ }
+
+ public static Set<Integer> getIntegerSetOrNull(String property, JsonNode
node) {
+ if (!node.has(property)) {
+ return null;
+ }
+
+ return ImmutableSet.<Integer>builder()
+ .addAll(new JsonIntegerArrayVisitor(property, node))
+ .build();
+ }
+
+ abstract static class JsonArrayIterator<T> implements Iterator<T> {
+
+ private final Iterator<JsonNode> elements;
+
+ JsonArrayIterator(String property, JsonNode node) {
+ JsonNode pNode = node.get(property);
+ Preconditions.checkArgument(pNode != null && !pNode.isNull() &&
pNode.isArray(),
+ "Cannot parse %s from non-array value: %s", property, pNode);
+ this.elements = pNode.elements();
+ }
+
+ @Override
+ public boolean hasNext() {
+ return elements().hasNext();
+ }
+
+ Iterator<JsonNode> elements() {
Review comment:
Rather than exposing elements, I think it is a bit cleaner to implement
`next` here and add an abstract `T convert(JsonNode element)` method that the
sub-classes implement. That way they don't need to know about `elements` or
call `next` manually.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]