summaryrefslogtreecommitdiff
path: root/groot-common/src/main
diff options
context:
space:
mode:
authordoufenghu <[email protected]>2024-10-30 20:15:51 +0800
committerdoufenghu <[email protected]>2024-10-30 20:15:51 +0800
commitac04f1d8735fb500c11aa87239c9c8c23e5af41a (patch)
tree4fc04287a1f6a5bb9ae8efbb2e50f0d4a1651d76 /groot-common/src/main
parentd2579028fb90bd60ca9e5f9fa36cbde8a6db8872 (diff)
[Improve][core] Enhance the drop, AsnLookup, and GeoIPLookup UDF context configuration checks by using a common validation utility.
Diffstat (limited to 'groot-common/src/main')
-rw-r--r--groot-common/src/main/java/com/geedgenetworks/common/config/CheckUDFContextUtil.java59
-rw-r--r--groot-common/src/main/java/com/geedgenetworks/common/config/UDFContextConfigOptions.java18
-rw-r--r--groot-common/src/main/java/com/geedgenetworks/common/exception/CommonErrorCode.java13
3 files changed, 66 insertions, 24 deletions
diff --git a/groot-common/src/main/java/com/geedgenetworks/common/config/CheckUDFContextUtil.java b/groot-common/src/main/java/com/geedgenetworks/common/config/CheckUDFContextUtil.java
index 80350f5..f1170be 100644
--- a/groot-common/src/main/java/com/geedgenetworks/common/config/CheckUDFContextUtil.java
+++ b/groot-common/src/main/java/com/geedgenetworks/common/config/CheckUDFContextUtil.java
@@ -11,12 +11,12 @@ public final class CheckUDFContextUtil {
// Check if all the params are present in the UDFContext
public static CheckResult checkAllExists(UDFContext context, String... params) {
- List<String> missingParams = Arrays.stream(params)
- .filter(param -> !isValidParam(context, param))
+ List<String> invalidParams = Arrays.stream(params)
+ .filter(param -> isInvalidParam(context, param))
.collect(Collectors.toList());
- if (!missingParams.isEmpty()) {
- String errorMsg = java.lang.String.format("Please specify [%s] as non-empty.", java.lang.String.join(",", missingParams));
+ if (!invalidParams.isEmpty()) {
+ String errorMsg = java.lang.String.format("Please specify [%s] as non-empty.", java.lang.String.join(",", invalidParams));
return CheckResult.error(errorMsg);
}
return CheckResult.success();
@@ -28,19 +28,21 @@ public final class CheckUDFContextUtil {
return CheckResult.success();
}
- List<String> missingParams = Arrays.stream(params)
- .filter(param -> !isValidParam(context, param))
+ List<String> invalidParams = Arrays.stream(params)
+ .filter(param -> isInvalidParam(context, param))
.collect(Collectors.toList());
- if (missingParams.size() == params.length) {
- String errorMsg = java.lang.String.format("Please specify at least one config of [%s] as non-empty.", java.lang.String.join(",", missingParams));
+ if (invalidParams.size() == params.length) {
+ String errorMsg = java.lang.String.format("Please specify at least one config of [%s] as non-empty.", java.lang.String.join(",", invalidParams));
return CheckResult.error(errorMsg);
}
return CheckResult.success();
}
+
+
// Check Array/Map Object has only one item
- public static CheckResult checkSingleItemExists (UDFContext context, String param) {
+ public static CheckResult checkCollectionSingleItemExists (UDFContext context, String param) {
if (context == null) {
return CheckResult.error("UDFContext is null");
}
@@ -57,25 +59,46 @@ public final class CheckUDFContextUtil {
}
- public static boolean isValidParam(UDFContext context, String param) {
+ // Check Parameters contains keys
+ public static CheckResult checkParametersContainsKeys(UDFContext context, String... keys) {
+ if (context == null) {
+ return CheckResult.error("UDFContext is null");
+ }
+
+ if (context.getParameters() == null) {
+ return CheckResult.error("Parameters is null");
+ }
+
+ List<String> missingKeys = Arrays.stream(keys)
+ .filter(key -> !context.getParameters().containsKey(key))
+ .collect(Collectors.toList());
+
+ if (!missingKeys.isEmpty()) {
+ String errorMsg = java.lang.String.format("Please specify [%s] as non-empty.", java.lang.String.join(",", missingKeys));
+ return CheckResult.error(errorMsg);
+ }
+ return CheckResult.success();
+ }
+
+ public static boolean isInvalidParam(UDFContext context, String param) {
if (context == null) {
- return false;
+ return true;
}
if (UDFContextConfigOptions.NAME.key().equals(param)) {
- return context.getName() != null;
+ return context.getName() == null;
} else if (UDFContextConfigOptions.LOOKUP_FIELDS.key().equals(param)) {
- return context.getLookupFields() != null && !context.getLookupFields().isEmpty();
+ return context.getLookupFields() == null || context.getLookupFields().isEmpty();
} else if (UDFContextConfigOptions.OUTPUT_FIELDS.key().equals(param)) {
- return context.getOutputFields() != null && !context.getOutputFields().isEmpty();
+ return context.getOutputFields() == null || context.getOutputFields().isEmpty();
} else if (UDFContextConfigOptions.FILTER.key().equals(param)) {
- return context.getFilter() != null;
+ return context.getFilter() == null;
} else if (UDFContextConfigOptions.PARAMETERS.key().equals(param)) {
- return context.getParameters() != null && !context.getParameters().isEmpty();
+ return context.getParameters() == null || context.getParameters().isEmpty();
} else if (UDFContextConfigOptions.FUNCTION.key().equals(param)) {
- return context.getFunction() != null;
+ return context.getFunction() == null;
} else {
- return false;
+ return true;
}
}
diff --git a/groot-common/src/main/java/com/geedgenetworks/common/config/UDFContextConfigOptions.java b/groot-common/src/main/java/com/geedgenetworks/common/config/UDFContextConfigOptions.java
index 85bab48..ac36b02 100644
--- a/groot-common/src/main/java/com/geedgenetworks/common/config/UDFContextConfigOptions.java
+++ b/groot-common/src/main/java/com/geedgenetworks/common/config/UDFContextConfigOptions.java
@@ -29,8 +29,26 @@ public interface UDFContextConfigOptions {
.noDefaultValue()
.withDescription("The parameters for the function.");
+ Option<String> PARAMETERS_KB_NAME = Options.key("kb_name")
+ .stringType()
+ .noDefaultValue()
+ .withDescription("The name of the knowledge base.");
+
+ Option<String> PARAMETERS_OPTION = Options.key("option")
+ .stringType()
+ .noDefaultValue()
+ .withDescription("The option for the function.");
+
+ Option<Map<String, String>> PARAMETERS_GEOLOCATION_FIELD_MAPPING = Options.key("geolocation_field_mapping")
+ .mapType()
+ .noDefaultValue()
+ .withDescription("The geolocation field mapping.");
+
+
Option<String> FUNCTION = Options.key("function")
.stringType()
.noDefaultValue()
.withDescription("The function to be executed.");
+
+
}
diff --git a/groot-common/src/main/java/com/geedgenetworks/common/exception/CommonErrorCode.java b/groot-common/src/main/java/com/geedgenetworks/common/exception/CommonErrorCode.java
index e4d9f59..5298810 100644
--- a/groot-common/src/main/java/com/geedgenetworks/common/exception/CommonErrorCode.java
+++ b/groot-common/src/main/java/com/geedgenetworks/common/exception/CommonErrorCode.java
@@ -2,12 +2,13 @@ package com.geedgenetworks.common.exception;
public enum CommonErrorCode implements GrootStreamErrorCodeSupplier {
- UNSUPPORTED_OPERATION("GROOT-STREAM-COMMON-0001", "Unsupported operation exception"),
- ILLEGAL_ARGUMENT("GROOT-STREAM-COMMON-0002", "Illegal argument exception"),
- SYNTAX_ERROR("GROOT-STREAM-COMMON-0003", "Syntax Error"),
- FILE_OPERATION_ERROR("GROOT-STREAM-COMMON-0004", "File operation failed, such as (read,list,write,move,copy,sync) etc..."),
-
- CONFIG_VALIDATION_FAILED("GROOT-STREAM-COMMON-0005", "Configuration item validate failed"),
+ UNSUPPORTED_OPERATION("GROOT-STREAM-COMMON-0001", "Unsupported operation."),
+ ILLEGAL_ARGUMENT("GROOT-STREAM-COMMON-0002", "Illegal argument."),
+ SYNTAX_ERROR("GROOT-STREAM-COMMON-0003", "Syntax error."),
+ FILE_OPERATION_ERROR("GROOT-STREAM-COMMON-0004", "File operation failed (e.g., read, list, write, move, copy, sync)."),
+ CONFIG_VALIDATION_FAILED("GROOT-STREAM-COMMON-0005", "Configuration item validation failed."),
+ JSON_OPERATION_FAILED(
+ "GROOT-STREAM-COMMON-0006", "JSON convert/parse operation failed."),
;
private final String code;