From b4f62f93123a0782964361b6e41c2bb656933852 Mon Sep 17 00:00:00 2001 From: Kat Shen Date: Fri, 25 Aug 2023 19:33:30 +0000 Subject: [PATCH] fix nits Signed-off-by: Kat Shen --- .../processor/keyvalue/KeyValueProcessor.java | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/data-prepper-plugins/key-value-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/keyvalue/KeyValueProcessor.java b/data-prepper-plugins/key-value-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/keyvalue/KeyValueProcessor.java index 5f27e92512..fb11a3386d 100644 --- a/data-prepper-plugins/key-value-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/keyvalue/KeyValueProcessor.java +++ b/data-prepper-plugins/key-value-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/keyvalue/KeyValueProcessor.java @@ -62,12 +62,12 @@ public KeyValueProcessor(final PluginMetrics pluginMetrics, final KeyValueProces super(pluginMetrics); this.keyValueProcessorConfig = keyValueProcessorConfig; - if(keyValueProcessorConfig.getFieldDelimiterRegex() != null + if (keyValueProcessorConfig.getFieldDelimiterRegex() != null && !keyValueProcessorConfig.getFieldDelimiterRegex().isEmpty()) { - if(keyValueProcessorConfig.getFieldSplitCharacters() != null + if (keyValueProcessorConfig.getFieldSplitCharacters() != null && !keyValueProcessorConfig.getFieldSplitCharacters().isEmpty()) { throw new IllegalArgumentException("field_delimiter_regex and field_split_characters cannot both be defined."); - } else if(!validateRegex(keyValueProcessorConfig.getFieldDelimiterRegex())) { + } else if (!validateRegex(keyValueProcessorConfig.getFieldDelimiterRegex())) { throw new PatternSyntaxException("field_delimiter_regex is not a valid regex string", keyValueProcessorConfig.getFieldDelimiterRegex(), -1); } @@ -92,16 +92,15 @@ public KeyValueProcessor(final PluginMetrics pluginMetrics, final KeyValueProces fieldDelimiterPattern = Pattern.compile(regex); - if (keyValueProcessorConfig.getRecursive()) { - if (fieldDelimiterPattern.matcher(delimiterBracketCheck).matches()) { - throw new IllegalArgumentException("While recursive is true, the set field split characters cannot contain brackets while you are trying to recurse."); - } + if (keyValueProcessorConfig.getRecursive() + && fieldDelimiterPattern.matcher(delimiterBracketCheck).matches()) { + throw new IllegalArgumentException("While recursive is true, the set field split characters cannot contain brackets while you are trying to recurse."); } } - if(keyValueProcessorConfig.getKeyValueDelimiterRegex() != null + if (keyValueProcessorConfig.getKeyValueDelimiterRegex() != null && !keyValueProcessorConfig.getKeyValueDelimiterRegex().isEmpty()) { - if(keyValueProcessorConfig.getValueSplitCharacters() != null + if (keyValueProcessorConfig.getValueSplitCharacters() != null && !keyValueProcessorConfig.getValueSplitCharacters().isEmpty()) { throw new IllegalArgumentException("key_value_delimiter_regex and value_split_characters cannot both be defined."); } else if (!validateRegex(keyValueProcessorConfig.getKeyValueDelimiterRegex())) { @@ -110,10 +109,9 @@ public KeyValueProcessor(final PluginMetrics pluginMetrics, final KeyValueProces keyValueDelimiterPattern = Pattern.compile(keyValueProcessorConfig.getKeyValueDelimiterRegex()); - if (keyValueProcessorConfig.getRecursive()) { - if (keyValueDelimiterPattern.matcher(delimiterBracketCheck).matches()) { - throw new IllegalArgumentException("While recursive is true, the set key value delimiter regex cannot contain brackets while you are trying to recurse."); - } + if (keyValueProcessorConfig.getRecursive() + && keyValueDelimiterPattern.matcher(delimiterBracketCheck).matches()) { + throw new IllegalArgumentException("While recursive is true, the set key value delimiter regex cannot contain brackets while you are trying to recurse."); } } else { String regex; @@ -130,10 +128,9 @@ public KeyValueProcessor(final PluginMetrics pluginMetrics, final KeyValueProces keyValueDelimiterPattern = Pattern.compile(regex); - if (keyValueProcessorConfig.getRecursive()) { - if (keyValueDelimiterPattern.matcher(delimiterBracketCheck).matches()) { - throw new IllegalArgumentException("While recursive is true, the set value split characters cannot contain brackets while you are trying to recurse."); - } + if (keyValueProcessorConfig.getRecursive() + && keyValueDelimiterPattern.matcher(delimiterBracketCheck).matches()) { + throw new IllegalArgumentException("While recursive is true, the set value split characters cannot contain brackets while you are trying to recurse."); } } @@ -188,7 +185,7 @@ private String buildRegexFromCharacters(String s) { char[] splitters = s.toCharArray(); StringBuilder regexedFieldSplitCharacters = new StringBuilder(); for(char c : splitters) { - if(Objects.equals(c, '\\')) { + if (Objects.equals(c, '\\')) { regexedFieldSplitCharacters.append(c); } else { regexedFieldSplitCharacters.append(c).append('|'); @@ -202,7 +199,7 @@ private String buildRegexFromCharacters(String s) { private boolean validateRegex(final String pattern) { - if(pattern != null && !Objects.equals(pattern, "")) { + if (pattern != null && !Objects.equals(pattern, "")) { try { Pattern.compile(pattern); } catch (PatternSyntaxException e) { @@ -257,7 +254,7 @@ public Collection> doExecute(final Collection> recor return records; } - private ObjectNode recurse(String input, ObjectMapper mapper) { + private ObjectNode recurse(final String input, final ObjectMapper mapper) { Stack bracketStack = new Stack(); Map bracketMap = initBracketMap(); int pairStart = 0; @@ -400,12 +397,12 @@ private Map executeConfigs(Map map) { continue; } - if(keyValueProcessorConfig.getDeleteKeyRegex() != null && !Objects.equals(keyValueProcessorConfig.getDeleteKeyRegex(), "")) { + if (keyValueProcessorConfig.getDeleteKeyRegex() != null && !Objects.equals(keyValueProcessorConfig.getDeleteKeyRegex(), "")) { key = key.replaceAll(keyValueProcessorConfig.getDeleteKeyRegex(), ""); } key = keyValueProcessorConfig.getPrefix() + key; - if(value != null + if (value != null && value instanceof String && keyValueProcessorConfig.getDeleteValueRegex() != null && !Objects.equals(keyValueProcessorConfig.getDeleteValueRegex(), "")) { @@ -470,7 +467,7 @@ private void addKeyValueToMap(final Map parsedMap, final String } } - if(!parsedMap.containsKey(key)) { + if (!parsedMap.containsKey(key)) { parsedMap.put(key, processedValue); return; }