htran1 commented on a change in pull request #2910: URL: https://github.com/apache/incubator-gobblin/pull/2910#discussion_r430757448
########## File path: gobblin-salesforce/src/main/java/org/apache/gobblin/typedconfig/ConstraintUtil.java ########## @@ -0,0 +1,128 @@ +/* + * 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.gobblin.typedconfig; + +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import lombok.extern.slf4j.Slf4j; +import org.apache.gobblin.typedconfig.compiletime.EnumOptions; +import org.apache.gobblin.typedconfig.compiletime.IntRange; +import org.apache.gobblin.typedconfig.compiletime.LongRange; +import org.apache.gobblin.typedconfig.compiletime.StringRegex; + + +/** + * Util class for handling constrain annotation + * supports: IntRange, LongRange, EnumOption, StringRegex + */ +@Slf4j +public class ConstraintUtil { + private ConstraintUtil() { + } + + public static Object constraint(Field field, Object value, Object defaultValue) { + if (value == null) { + return defaultValue; + } + Class type = field.getType(); + if (type.isEnum()) { + return getEnumValue(field, value, defaultValue); + } + switch (type.getName()) { + case "int": case "java.lang.Integer": + return getIntValue(field, value, defaultValue); + case "long": case "java.lang.Long": + return getLongValue(field, value, defaultValue); + case "java.lang.String": + return getStringValue(field, value, defaultValue); + case "boolean": case "java.lang.Boolean": + return getBooleanValue(field, value, defaultValue); + case "java.util.Date": + return getDateValue(field, value, defaultValue); + default: + throw new RuntimeException("not supported the return type: " + type.getName()); + } + } + + static private Object getIntValue(Field field, Object value, Object defaultValue) { + IntRange intRange = field.getAnnotation(IntRange.class); + if (intRange == null) { + return value; + } + int[] range = intRange.value(); + int intValue = Integer.parseInt(value.toString()); + if (range.length != 2 || (range.length == 2 && intValue >= range[0] && intValue <= range[1])) { + return value; + } else { + return defaultValue; + } + } + static private Object getLongValue(Field field, Object value, Object defaultValue) { + LongRange longRange = field.getAnnotation(LongRange.class); + long[] range = longRange.value(); + if (range == null) { + return value; + } + long longValue = Long.parseLong(value.toString()); + if (range.length != 2 || (range.length == 2 && longValue > range[0] && longValue < range[1])) { Review comment: We should raise an error instead of using the default if an out of range value is provided. This is to avoid confusion where the user thinks that the execution used the config they provide. ########## File path: gobblin-salesforce/src/main/java/org/apache/gobblin/typedconfig/ConstraintUtil.java ########## @@ -0,0 +1,128 @@ +/* + * 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.gobblin.typedconfig; + +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import lombok.extern.slf4j.Slf4j; +import org.apache.gobblin.typedconfig.compiletime.EnumOptions; +import org.apache.gobblin.typedconfig.compiletime.IntRange; +import org.apache.gobblin.typedconfig.compiletime.LongRange; +import org.apache.gobblin.typedconfig.compiletime.StringRegex; + + +/** + * Util class for handling constrain annotation Review comment: constrain -> constraint ---------------------------------------------------------------- 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: us...@infra.apache.org