sashapolo commented on a change in pull request #67:
URL: https://github.com/apache/ignite-3/pull/67#discussion_r596024512
##########
File path:
modules/configuration/src/main/java/org/apache/ignite/configuration/internal/validation/MaxValidator.java
##########
@@ -17,28 +17,21 @@
package org.apache.ignite.configuration.internal.validation;
-import org.apache.ignite.configuration.internal.DynamicConfiguration;
-import
org.apache.ignite.configuration.validation.ConfigurationValidationException;
-import org.apache.ignite.configuration.validation.FieldValidator;
+import javax.validation.constraints.Max;
+import org.apache.ignite.configuration.validation.ValidationContext;
+import org.apache.ignite.configuration.validation.ValidationIssue;
+import org.apache.ignite.configuration.validation.Validator;
/**
* Validate that field value is not greater than some maximum value.
- *
- * @param <C> Root configuration type.
*/
-public class MaxValidator<C extends DynamicConfiguration<?, ?, ?>> extends
FieldValidator<Number, C> {
- /** Maximum value. */
- private final long maxValue;
-
- /** Constructor. */
- public MaxValidator(long maxValue, String message) {
- super(message);
- this.maxValue = maxValue;
- }
-
+public class MaxValidator implements Validator<Max, Number> {
/** {@inheritDoc} */
- @Override public void validate(Number value, C newRoot, C oldRoot) throws
ConfigurationValidationException {
- if (value.longValue() > maxValue)
- throw new ConfigurationValidationException(message);
+ @Override public void validate(Max annotation, ValidationContext<Number>
ctx) {
+ if (ctx.getNewValue().longValue() > annotation.value()) {
+ ctx.addIssue(new ValidationIssue(
+ "Configuration value '" + ctx.currentKey() + "' cannot be more
then " + annotation.value()
Review comment:
```suggestion
"Configuration value '" + ctx.currentKey() + "' must not be
greater than " + annotation.value()
```
##########
File path:
modules/configuration/src/main/java/org/apache/ignite/configuration/internal/validation/MinValidator.java
##########
@@ -17,28 +17,21 @@
package org.apache.ignite.configuration.internal.validation;
-import org.apache.ignite.configuration.internal.DynamicConfiguration;
-import
org.apache.ignite.configuration.validation.ConfigurationValidationException;
-import org.apache.ignite.configuration.validation.FieldValidator;
+import javax.validation.constraints.Min;
+import org.apache.ignite.configuration.validation.ValidationContext;
+import org.apache.ignite.configuration.validation.ValidationIssue;
+import org.apache.ignite.configuration.validation.Validator;
/**
* Validate that field value is not less than some minimal value.
- *
- * @param <C> Root configuration type.
*/
-public class MinValidator<C extends DynamicConfiguration<?, ?, ?>> extends
FieldValidator<Number, C> {
- /** Minimal value. */
- private final long minValue;
-
- /** Constructor. */
- public MinValidator(long minValue, String message) {
- super(message);
- this.minValue = minValue;
- }
-
+public class MinValidator implements Validator<Min, Number> {
/** {@inheritDoc} */
- @Override public void validate(Number value, C newRoot, C oldRoot) throws
ConfigurationValidationException {
- if (value.longValue() < minValue)
- throw new ConfigurationValidationException(message);
+ @Override public void validate(Min annotation, ValidationContext<Number>
ctx) {
+ if (ctx.getNewValue().longValue() < annotation.value()) {
+ ctx.addIssue(new ValidationIssue(
+ "Configuration value '" + ctx.currentKey() + "' cannot be less
then " + annotation.value()
Review comment:
```suggestion
"Configuration value '" + ctx.currentKey() + "' must not be
less than " + annotation.value()
```
##########
File path:
modules/configuration-annotation-processor/src/test/java/org/apache/ignite/configuration/internal/validation/ValidationUtilTest.java
##########
@@ -0,0 +1,183 @@
+/*
+ * 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.ignite.configuration.internal.validation;
+
+import java.lang.annotation.Annotation;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.ignite.configuration.annotation.Config;
+import org.apache.ignite.configuration.annotation.ConfigValue;
+import org.apache.ignite.configuration.annotation.ConfigurationRoot;
+import org.apache.ignite.configuration.annotation.NamedConfigValue;
+import org.apache.ignite.configuration.annotation.Value;
+import org.apache.ignite.configuration.internal.RootsNode;
+import org.apache.ignite.configuration.internal.util.ConfigurationUtil;
+import org.apache.ignite.configuration.sample.storage.TestConfigurationStorage;
+import org.apache.ignite.configuration.tree.NamedListView;
+import org.apache.ignite.configuration.validation.ValidationContext;
+import org.apache.ignite.configuration.validation.ValidationIssue;
+import org.apache.ignite.configuration.validation.Validator;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import static java.util.Collections.emptyMap;
+import static java.util.Collections.emptySet;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/** */
+public class ValidationUtilTest {
+ /** */
+ @Target(FIELD)
+ @Retention(RUNTIME)
+ @interface LeafValidation {
+ }
+
+ /** */
+ @Target(FIELD)
+ @Retention(RUNTIME)
+ @interface InnerValidation {
+ }
+
+ /** */
+ @Target(FIELD)
+ @Retention(RUNTIME)
+ @interface NamedListValidation {
+ }
+
+ /** */
+ @ConfigurationRoot(rootName = "root", storage =
TestConfigurationStorage.class)
+ public static class ValidatedRootConfigurationSchema {
+ /** */
+ @InnerValidation
+ @ConfigValue
+ public ValidatedChildConfigurationSchema child;
+
+ /** */
+ @NamedListValidation
+ @NamedConfigValue
+ public ValidatedChildConfigurationSchema elements;
+ }
+
+ /** */
+ @Config
+ public static class ValidatedChildConfigurationSchema {
+ /** */
+ @LeafValidation
+ @Value(hasDefault = true)
+ public String str = "foo";
+ }
+
+ /** */
+ private ValidatedRootNode root;
+
+ /** */
+ @BeforeEach
+ public void before() {
+ root = new ValidatedRootNode();
+
+ ConfigurationUtil.addDefaults(root, root);
+ }
+
+ /** */
+ @AfterEach
+ public void after() {
Review comment:
Why is this method needed? Doesn't junit create a new class per test? If
yes, I would suggest removing this method and initializing the `root` variable,
leaving only the `addDefaults` call in `before`
##########
File path:
modules/configuration/src/main/java/org/apache/ignite/configuration/tree/InnerNode.java
##########
@@ -110,7 +110,18 @@
*/
@Override public abstract void construct(String key, ConfigurationSource
src) throws NoSuchElementException;
- /** */
+ /**
+ * Assigns default value to the corresponding leaf. Defaults are gathered
from configuration schema class.
+ *
+ * @param key Field name to be initialized.
Review comment:
why not call this parameter `fieldName` then?
##########
File path:
modules/configuration/src/main/java/org/apache/ignite/configuration/ConfigurationRegistry.java
##########
@@ -35,22 +41,33 @@
/** */
private final ConfigurationChanger changer = new ConfigurationChanger();
+ {
+ // Default vaildators implemented in current module.
+ changer.addValidator(Min.class, new MinValidator());
Review comment:
Maybe it would be a good idea to treat the `ConfiguraitonChanger` class
as a builder, i.e. make all setter-like methods return `ConfiguraitonChanger`.
It would make some of its API usages cleaner
----------------------------------------------------------------
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:
[email protected]