nastra commented on a change in pull request #4036:
URL: https://github.com/apache/iceberg/pull/4036#discussion_r799307987
##########
File path: core/src/main/java/org/apache/iceberg/util/JsonUtil.java
##########
@@ -65,6 +67,16 @@ public static Integer getIntOrNull(String property, JsonNode
node) {
return pNode.asInt();
}
+ public static Long getLongOrNull(String property, JsonNode node) {
+ if (!node.has(property)) {
+ return null;
+ }
+ JsonNode pNode = node.get(property);
+ Preconditions.checkArgument(pNode != null && !pNode.isNull() &&
pNode.isIntegralNumber() &&
+ pNode.canConvertToLong(), "Cannot parse %s from non-string value: %s",
property, pNode);
Review comment:
just wondering whether the error message should mention that it can't
parse it to a Long
##########
File path: core/src/test/java/org/apache/iceberg/TestSnapshotRefParser.java
##########
@@ -0,0 +1,113 @@
+/*
+ * 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 java.util.Objects;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestSnapshotRefParser {
+
+ @Test
+ public void testTagToJsonDefault() {
+ String json = "{\"snapshot-id\":1,\"type\":\"tag\"}";
+ SnapshotRef ref = SnapshotRef.tagBuilder(1L).build();
+ Assert.assertEquals("Should be able to serialize default tag",
+ json, SnapshotRefParser.toJson(ref));
+ }
+
+ @Test
+ public void testTagToJsonAllFields() {
+ String json = "{\"snapshot-id\":1,\"type\":\"tag\",\"max-ref-age-ms\":1}";
+ SnapshotRef ref = SnapshotRef.tagBuilder(1L)
+ .maxRefAgeMs(1L)
+ .build();
+ Assert.assertEquals("Should be able to serialize tag with all fields",
+ json, SnapshotRefParser.toJson(ref));
+ }
+
+ @Test
+ public void testBranchToJsonDefault() {
+ String json = "{\"snapshot-id\":1,\"type\":\"branch\"}";
+ SnapshotRef ref = SnapshotRef.branchBuilder(1L).build();
+ Assert.assertEquals("Should be able to serialize default branch",
+ json, SnapshotRefParser.toJson(ref));
+ }
+
+ @Test
+ public void testBranchToJsonAllFields() {
+ String json =
"{\"snapshot-id\":1,\"type\":\"branch\",\"min-snapshots-to-keep\":2," +
+ "\"max-snapshot-age-ms\":3,\"max-ref-age-ms\":4}";
+ SnapshotRef ref = SnapshotRef.branchBuilder(1L)
+ .minSnapshotsToKeep(2)
+ .maxSnapshotAgeMs(3L)
+ .maxRefAgeMs(4L)
+ .build();
+ Assert.assertEquals("Should be able to serialize branch with all fields",
+ json, SnapshotRefParser.toJson(ref));
+ }
+
+ @Test
+ public void testTagFromJsonDefault() {
+ String json = "{\"snapshot-id\":1,\"type\":\"tag\"}";
+ SnapshotRef ref = SnapshotRef.tagBuilder(1L).build();
+ Assert.assertTrue("Should be able to deserialize default tag",
Review comment:
TBH this check feels weird. When it fails it is unfortunately useless as
it doesn't tell you which fields didn't match.
It would be better if the `SnapshotRef` would implement `equals(..)` and
then you could just do something like
`Assertions.assertThat(ref).isEqualTo(SnapshotRefParser.fromJson(json))`. That
way you would immediately see which fields are different when the check fails.
##########
File path: core/src/test/java/org/apache/iceberg/TestSnapshotRefParser.java
##########
@@ -0,0 +1,113 @@
+/*
+ * 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 java.util.Objects;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestSnapshotRefParser {
Review comment:
would it make sense to add some tests where:
* the json string is null/empty
* the json string contains field names that it doesn't understand
* the json string omits required fields, such as the `snapshot-id` or the
`type`
* certain fields contain values that can't be parsed, such as `type` having
some garbage string or `snapshot-id` having a string that can't be parsed to a
long
So far we're only testing the happy path, but not the cases where things
could go wrong
##########
File path: core/src/test/java/org/apache/iceberg/TestSnapshotRefParser.java
##########
@@ -0,0 +1,113 @@
+/*
+ * 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 java.util.Objects;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestSnapshotRefParser {
+
+ @Test
+ public void testTagToJsonDefault() {
+ String json = "{\"snapshot-id\":1,\"type\":\"tag\"}";
Review comment:
would it make sense to combine the from/to json parts? That way we don't
have to duplicate that json string in multiple methods and the entire ser/de is
tested within a single method
--
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]