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 2020/05/16 01:10:46 UTC

[GitHub] [pulsar] cckellogg commented on a change in pull request #6972: Add Annotations for config validation checking

cckellogg commented on a change in pull request #6972:
URL: https://github.com/apache/pulsar/pull/6972#discussion_r426102269



##########
File path: pulsar-common/src/test/java/org/apache/pulsar/common/validator/ConfigValidationTest.java
##########
@@ -0,0 +1,148 @@
+/**
+ * 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.common.validator;
+
+import org.testng.annotations.Test;
+
+import java.util.*;
+import static org.testng.Assert.*;
+
+public class ConfigValidationTest {
+
+    private final List<String> testStringList = Arrays.asList(new String[]{"foo", "bar"});
+    private final List<Integer> testIntegerList = Arrays.asList(new Integer[]{0, 1});
+    private final Map<String, Integer> testStringIntegerMap = new HashMap<String, Integer>() {
+        {
+            put("one", 1);
+            put("two", 2);
+        }
+    };
+    private final Map<String, String> testStringStringMap = new HashMap<String, String>() {
+        {
+            put("one", "one");
+            put("two", "two");
+        }
+    };
+    private final String topic = "persistent://public/default/topic";
+
+    public static class TestValidator extends Validator {
+        @Override
+        public void validateField(String name, Object o) {
+            if (o instanceof String) {
+                String value = (String)o;
+                if (!value.startsWith("ABCDE")) {
+                    throw new IllegalArgumentException(String.format("Field %s does not start with ABCDE", name));
+                }
+            } else {
+                throw new IllegalArgumentException(String.format("Field %s is not a string", name));
+            }
+        }
+    }
+
+    class TestConfig {

Review comment:
       I like the idea of having type checking. The naming is a bit confusing to me something like makes more sense to me. 
   ```
   class TestConfig {
          @NotNull
           public String stringValue;
           
          @PositiveNumber or @Number(positive=true)
           public Integer positiveNumber;
           
          @List(itemType = Integer.class) or @ListType(itemType = Integer.class) or @IntegerList
           public List integerList;
           
          @Map(keyType = String.class, valueType = Integer.class) or @MapType(keyType = String.class, valueType = Integer.class)
           public Map stringIntegerMap;
           
          @List(itemType = String.class) or @StringList
           public List stringList;
           
          @TopicName
           public String topic;
           
          @CustomType(validatorClass = TestValidator.class)
           public String customString;
       }
   ```
   I think annotations should describe what the variable type is as opposed to describing how to test it. The testing is implied. 
   
   Another thing to think about is how to handle fields that should be required to have a value.
   
   Thoughts?
   
   Thoughts? Al




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