nastra commented on code in PR #6934:
URL: https://github.com/apache/iceberg/pull/6934#discussion_r1176228147


##########
core/src/test/java/org/apache/iceberg/TestContentFileParser.java:
##########
@@ -0,0 +1,244 @@
+/*
+ * 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.iceberg;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Stream;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.types.Comparators;
+import org.apache.iceberg.types.Conversions;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.util.JsonUtil;
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+public class TestContentFileParser {

Review Comment:
   I think it would be good to also add a test with a plain JSON string to see 
how the full JSON looks like. And then maybe also another test with a plain 
JSON string where all optional fields (metrics, equality field ids, sort order 
id, split offsets, ...) are missing



##########
core/src/test/java/org/apache/iceberg/util/TestJsonUtil.java:
##########
@@ -167,6 +169,26 @@ public void getStringOrNull() throws 
JsonProcessingException {
         .hasMessage("Cannot parse to a string value: x: 23");
   }
 
+  @Test
+  public void getByteBufferOrNull() throws JsonProcessingException {
+    Assertions.assertThat(JsonUtil.getByteBufferOrNull("x", 
JsonUtil.mapper().readTree("{}")))
+        .isNull();
+    Assertions.assertThat(
+            JsonUtil.getByteBufferOrNull("x", 
JsonUtil.mapper().readTree("{\"x\": null}")))
+        .isNull();
+
+    byte[] bytes = new byte[] {1, 2, 3, 4};
+    String base16Str = BaseEncoding.base16().encode(bytes);
+    String json = String.format("{\"x\": \"%s\"}", base16Str);
+    ByteBuffer byteBuffer = JsonUtil.getByteBufferOrNull("x", 
JsonUtil.mapper().readTree(json));
+    Assertions.assertThat(byteBuffer.array()).isEqualTo(bytes);
+
+    Assertions.assertThatThrownBy(
+            () -> JsonUtil.getByteBufferOrNull("x", 
JsonUtil.mapper().readTree("{\"x\": 23}")))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot parse default as a binary value: 23");

Review Comment:
   as I mentioned above this error msg is slightly misleading because it's not 
clear what `default` means here. Once we move away from `SingleValueParser` for 
parsing bytes, we can have a better error msg in this case



##########
core/src/main/java/org/apache/iceberg/util/JsonUtil.java:
##########
@@ -173,6 +176,14 @@ public static String getStringOrNull(String property, 
JsonNode node) {
     return getString(property, node);
   }
 
+  public static ByteBuffer getByteBufferOrNull(String property, JsonNode node) 
{
+    if (!node.has(property) || node.get(property).isNull()) {
+      return null;
+    }
+
+    return (ByteBuffer) SingleValueParser.fromJson(Types.BinaryType.get(), 
node.get(property));

Review Comment:
   I would rather prefer to define `return ByteBuffer.wrap( 
BaseEncoding.base16().decode(node.get(property).textValue().toUpperCase(Locale.ROOT)));`
 independently from `SingleValueParser` here because the error msg from 
`SingleValueParser` (`Cannot parse default as a ...`) is rather misleading.
   Then `getByteBufferOrNull()` could define its own error msg. 



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to