amogh-jahagirdar commented on code in PR #6861: URL: https://github.com/apache/iceberg/pull/6861#discussion_r1129660249
########## core/src/main/java/org/apache/iceberg/view/ViewVersionParser.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.view; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonNode; +import java.io.IOException; +import java.util.Locale; +import java.util.Map; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.util.JsonUtil; + +class ViewVersionParser { + + private static final String VERSION_ID = "version-id"; + private static final String TIMESTAMP_MS = "timestamp-ms"; + private static final String SUMMARY = "summary"; + private static final String OPERATION = "operation"; + + private static final String REPRESENTATIONS = "representations"; + + private ViewVersionParser() {} + + static void toJson(ViewVersion version, JsonGenerator generator) throws IOException { + Preconditions.checkArgument(version != null, "Invalid view version: null"); + generator.writeStartObject(); + generator.writeNumberField(VERSION_ID, version.versionId()); + generator.writeNumberField(TIMESTAMP_MS, version.timestampMillis()); + generator.writeObjectFieldStart(SUMMARY); + generator.writeStringField(OPERATION, version.operation().name().toLowerCase(Locale.ENGLISH)); + for (Map.Entry<String, String> summaryEntry : version.summary().entrySet()) { + if (!summaryEntry.getKey().equals(OPERATION)) { + generator.writeStringField(summaryEntry.getKey(), summaryEntry.getValue()); + } + } + + generator.writeEndObject(); + + generator.writeArrayFieldStart(REPRESENTATIONS); + for (ViewRepresentation representation : version.representations()) { + ViewRepresentationParser.toJson(representation, generator); + } + generator.writeEndArray(); + + generator.writeEndObject(); + } + + static String toJson(ViewVersion version) { + return JsonUtil.generate(gen -> toJson(version, gen), false); + } + + static ViewVersion fromJson(String json) { + Preconditions.checkArgument(json != null, "Cannot parse view version from null string"); Review Comment: @nastra I added this explicit check back because failing in the `JsonUtil` itself yields an awkward error message. Here it's more explicit, I think this is better. -- 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]
