You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2021/03/26 16:46:12 UTC

[GitHub] [pulsar] vroyer commented on a change in pull request #10052: Add a JSON RecordBuilder to the GenericJsonSchema

vroyer commented on a change in pull request #10052:
URL: https://github.com/apache/pulsar/pull/10052#discussion_r602443381



##########
File path: pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/generic/JsonRecordBuilderImpl.java
##########
@@ -0,0 +1,105 @@
+/**
+ * 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.pulsar.client.impl.schema.generic;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.pulsar.client.api.schema.Field;
+import org.apache.pulsar.client.api.schema.GenericRecord;
+import org.apache.pulsar.client.api.schema.GenericRecordBuilder;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+public class JsonRecordBuilderImpl implements GenericRecordBuilder {
+
+    private static ObjectMapper objectMapper = new ObjectMapper();
+
+    private final GenericSchemaImpl genericSchema;
+    private Map<String, Object> map = new HashMap<>();
+    private Map<String, Field> fields = new HashMap<>();
+
+    public JsonRecordBuilderImpl(GenericSchemaImpl genericSchema) {
+        this.genericSchema = genericSchema;
+    }
+
+    /**
+     * Sets the value of a field.
+     *
+     * @param fieldName the name of the field to set.
+     * @param value     the value to set.
+     * @return a reference to the RecordBuilder.
+     */
+    @Override
+    public GenericRecordBuilder set(String fieldName, Object value) {
+        map.put(fieldName, value);
+        fields.put(fieldName, new Field(fieldName, map.size()));
+        return this;
+    }
+
+    /**
+     * Sets the value of a field.
+     *
+     * @param field the field to set.
+     * @param value the value to set.
+     * @return a reference to the RecordBuilder.
+     */
+    @Override
+    public GenericRecordBuilder set(Field field, Object value) {
+        set(field.getName(), value);
+        return this;
+    }
+
+    /**
+     * Clears the value of the given field.
+     *
+     * @param fieldName the name of the field to clear.
+     * @return a reference to the RecordBuilder.
+     */
+    @Override
+    public GenericRecordBuilder clear(String fieldName) {
+        map.remove(fieldName);
+        fields.remove(fieldName);
+        return this;
+    }
+
+    /**
+     * Clears the value of the given field.
+     *
+     * @param field the field to clear.
+     * @return a reference to the RecordBuilder.
+     */
+    @Override
+    public GenericRecordBuilder clear(Field field) {
+        clear(field.getName());
+        return this;
+    }
+
+    @Override
+    public GenericRecord build() {
+        JsonNode jn = objectMapper.valueToTree(map);
+        return new GenericJsonRecord(
+                null,
+                 fields.values().stream().collect(Collectors.toList()),

Review comment:
       Right.




-- 
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:
users@infra.apache.org