nastra commented on code in PR #5407: URL: https://github.com/apache/iceberg/pull/5407#discussion_r984392308
########## core/src/main/java/org/apache/iceberg/rest/requests/ReportMetricsRequestParser.java: ########## @@ -0,0 +1,95 @@ +/* + * 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.rest.requests; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonNode; +import java.io.IOException; +import java.util.Locale; +import org.apache.iceberg.metrics.ScanReport; +import org.apache.iceberg.metrics.ScanReportParser; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.rest.requests.ReportMetricsRequest.ReportType; +import org.apache.iceberg.util.JsonUtil; + +public class ReportMetricsRequestParser { + + private static final String REPORT_TYPE = "report-type"; + private static final String REPORT = "report"; + + private ReportMetricsRequestParser() {} + + public static String toJson(ReportMetricsRequest request) { + return toJson(request, false); + } + + public static String toJson(ReportMetricsRequest request, boolean pretty) { + return JsonUtil.generate(gen -> toJson(request, gen), pretty); + } + + public static void toJson(ReportMetricsRequest request, JsonGenerator gen) throws IOException { + Preconditions.checkArgument(null != request, "Invalid metrics request: null"); + + gen.writeStartObject(); + metricsToJson(request, gen); + gen.writeEndObject(); + } + + private static void metricsToJson(ReportMetricsRequest request, JsonGenerator gen) + throws IOException { + if (null != request.report()) { + gen.writeStringField(REPORT_TYPE, fromReportType(request.reportType())); + + switch (request.reportType()) { + case SCAN_REPORT: + gen.writeFieldName(REPORT); + ScanReportParser.toJson((ScanReport) request.report(), gen); Review Comment: this was mainly because our json parsers write a start object at the beginning and so writing the scan report without a field name didn't work: ``` com.fasterxml.jackson.core.JsonGenerationException: Can not start an object, expecting field name (context: Object) java.io.UncheckedIOException: com.fasterxml.jackson.core.JsonGenerationException: Can not start an object, expecting field name (context: Object) at org.apache.iceberg.util.JsonUtil.generate(JsonUtil.java:76) at org.apache.iceberg.rest.requests.ReportMetricsRequestParser.toJson(ReportMetricsRequestParser.java:43) at org.apache.iceberg.rest.requests.TestReportMetricsRequestParser.roundTripSerde(TestReportMetricsRequestParser.java:124) ``` I've added a method to `ScanReportParser` that omits writing start/end objects so that we don't have to write a `REPORT` field here -- 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]
