Skip to content

Commit

Permalink
Add config param to split multiline text
Browse files Browse the repository at this point in the history
  • Loading branch information
rroupski-splunk committed Sep 13, 2023
1 parent bdee7bf commit 960ae8a
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 110 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Global properties
artifactGroup=io.ocsf
artifactVersion=1.2.2-dev
artifactVersion=1.3.0-dev
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public final class TranslatorBuilder
private static final String ValueType = "type";
private static final String Values = "values";
private static final String Separator = "separator";
private static final String Splitter = "splitter";

private static final String Predicate = "when";
private static final String Parser = "parser";
Expand Down Expand Up @@ -157,8 +158,11 @@ public static Translator fromString(final String json) throws IOException
public static Translator build(
final Path home, final JsonReader reader, final Map<String, Object> map) throws IOException
{
final Translator translator = createTranslator(
(String) map.get(Predicate), readParsers(home, reader, map), readRules(home, reader, map));
final Translator translator =
createTranslator(
(String) map.get(Predicate),
readParsers(home, reader, map),
readRules(home, reader, map));

final Collection<Map<String, Object>> ruleset = Maps.typecast(map.get(RuleSet));
if (ruleset == null || ruleset.isEmpty())
Expand All @@ -170,9 +174,11 @@ public static Translator build(
final Collection<Translator> list = new ArrayList<>(ruleset.size());
for (final Map<String, Object> rule : ruleset)
{
list.add(createSubTranslator(
(String) rule.get(Predicate),
readParsers(home, reader, rule), readRules(home, reader, rule)));
list.add(
createSubTranslator(
(String) rule.get(Predicate),
readParsers(home, reader, rule),
readRules(home, reader, rule)));
}

return new Translator()
Expand Down Expand Up @@ -315,12 +321,18 @@ private static DataTranslator createParser(final Map<String, Object> map)
{
final Map<String, Object> dstMap = Maps.typecast(Maps.getIn(data, dstKey));
if (dstMap == null)
{
Maps.putIn(data, dstKey, parsed);
}
else
{
dstMap.putAll(parsed);
}
}
else
{
data.putAll(parsed);
}
}
}
catch (final Exception ex)
Expand Down Expand Up @@ -398,8 +410,7 @@ public Map<String, Object> apply(final Map<String, Object> data)

@Override
public Map<String, Object> apply(
final Map<String, Object> data, final Map<String,
Object> translated)
final Map<String, Object> data, final Map<String, Object> translated)
{
return TranslatorBuilder.apply(compiled, translator.parse(data), translated);
}
Expand All @@ -419,8 +430,7 @@ public Map<String, Object> apply(final Map<String, Object> data)

@Override
public Map<String, Object> apply(
final Map<String, Object> data,
final Map<String, Object> translated)
final Map<String, Object> data, final Map<String, Object> translated)
{
return p.test(data) ?
TranslatorBuilder.apply(compiled, translator.parse(data), translated) :
Expand All @@ -438,11 +448,8 @@ private static Collection<Map<String, Object>> readRules(
}

private static Collection<Map<String, Object>> readRules(
final Path home,
final JsonReader reader,
final Collection<Map<String, Object>> list,
final Collection<Map<String, Object>> rules)
throws IOException
final Path home, final JsonReader reader, final Collection<Map<String, Object>> list,
final Collection<Map<String, Object>> rules) throws IOException
{
for (final Map<String, Object> rule : list)
{
Expand Down Expand Up @@ -474,11 +481,8 @@ else if (include instanceof List<?>)
}

private static void includeRule(
final Path home,
final String filename,
final JsonReader reader,
final Collection<Map<String, Object>> rules)
throws IOException
final Path home, final String filename, final JsonReader reader,
final Collection<Map<String, Object>> rules) throws IOException
{
final Object included = reader.read(home.resolve(filename));
if (included instanceof Map<?, ?>)
Expand All @@ -499,15 +503,14 @@ else if (included instanceof Collection<?>)
}

static Map<String, Object> apply(
final List<Tuple<String, Rule>> rules, final Map<String,
Object> data)
final List<Tuple<String, Rule>> rules, final Map<String, Object> data)
{
return apply(rules, data, new HashMap<>());
}

static Map<String, Object> apply(
final List<Tuple<String, Rule>> rules, final Map<String, Object> data, final Map<String,
Object> translated)
final List<Tuple<String, Rule>> rules, final Map<String, Object> data,
final Map<String, Object> translated)
{
rules.forEach(rule -> rule.value.apply(data, translated));
Maps.cleanup(data);
Expand Down Expand Up @@ -540,12 +543,10 @@ private static Tuple<String, Rule> createRule(final Map<String, Object> rule) th
final String name = r.getKey().intern();
final Object obj = r.getValue();

if (obj instanceof Map<?, ?>)
return newRule(name, Maps.typecast(obj));
if (obj instanceof Map<?, ?>) return newRule(name, Maps.typecast(obj));

// handle embedded objects
if (obj instanceof Collection<?>)
return embedded(name, Maps.typecast(obj));
if (obj instanceof Collection<?>) return embedded(name, Maps.typecast(obj));
}

throw new IllegalArgumentException("Illegal rule");
Expand All @@ -559,8 +560,7 @@ private static Tuple<String, Rule> newRule(final String name, final Map<String,
{
final Object value = map.get("@value");

if (value instanceof Map<?, ?>)
return merge(name, Maps.typecast(value));
if (value instanceof Map<?, ?>) return merge(name, Maps.typecast(value));

return merge(name, map);
}
Expand Down Expand Up @@ -599,8 +599,7 @@ private static Tuple<String, Rule> newRule(final String name, final Map<String,
}

private static Tuple<String, Rule> embedded(
final String name, final Collection<Map<String,
Object>> ruleData)
final String name, final Collection<Map<String, Object>> ruleData)
{
final List<Tuple<String, Rule>> rules = compile(ruleData);

Expand Down Expand Up @@ -649,8 +648,7 @@ private static Tuple<String, Rule> value(final String name, final Object ruleDat
}

return new Tuple<>(name, (data, translated) -> {
if (predicate == null || predicate.test(data))
Maps.putIn(translated, name, value, overwrite);
if (predicate == null || predicate.test(data)) Maps.putIn(translated, name, value, overwrite);
});
}

Expand Down Expand Up @@ -678,7 +676,9 @@ private static Tuple<String, Rule> clone(final String name, final Object ruleDat

return new Tuple<>(name, (data, translated) -> {
if (predicate == null || predicate.test(data))
{
Maps.putIn(translated, dest, Maps.getIn(translated, name), overwrite);
}
});
}

Expand All @@ -698,8 +698,7 @@ private static Tuple<String, Rule> remove(final String name, final Object ruleDa
}

return new Tuple<>(name, (data, translated) -> {
if (predicate == null || predicate.test(data))
Maps.removeIn(data, name);
if (predicate == null || predicate.test(data)) Maps.removeIn(data, name);
});
}

Expand All @@ -719,6 +718,7 @@ private static Tuple<String, Rule> rename(
final String key;
final String type;
final String separator;
final String splitter;
final Object defValue;
final boolean overwrite;
final boolean is_array;
Expand All @@ -732,6 +732,7 @@ private static Tuple<String, Rule> rename(
key = Maps.get(map, NameField, name).intern();
type = Maps.get(map, ValueType);
separator = (String) map.get(Separator);
splitter = (String) map.getOrDefault(Splitter, Strings.LineSplitter);

defValue = map.get(DefaultValue);
overwrite = Maps.get(map, Overwrite, Boolean.FALSE);
Expand All @@ -745,6 +746,7 @@ else if (ruleData instanceof String)
key = ((String) ruleData).intern();
type = null;
separator = null;
splitter = Strings.LineSplitter;
defValue = null;
overwrite = false;
is_array = false;
Expand Down Expand Up @@ -778,7 +780,6 @@ public Object get(final Map<String, Object> map, final String _name)
{
if (separator != null && sb.length() > 0)
sb.append(separator);

sb.append(v);
}
}
Expand All @@ -792,8 +793,7 @@ public Object get(final Map<String, Object> map, final String _name)
src = source;
}

return new Tuple<>(name, (data, translated) ->
{
return new Tuple<>(name, (data, translated) -> {
if (predicate == null || predicate.test(data))
{
final Object value = src.get(data, name);
Expand All @@ -807,7 +807,7 @@ public Object get(final Map<String, Object> map, final String _name)
if (is_array)
{
final List<Object> list = new ArrayList<>();
for (final Object o : toArray(value))
for (final Object o : Strings.toArray(value, splitter))
{
final Object parsed = o != null ? typecast(o, type) : null;
list.add(parsed);
Expand All @@ -825,9 +825,13 @@ public Object get(final Map<String, Object> map, final String _name)
else
{
if (is_array)
Maps.putIn(translated, key, toArray(value), overwrite);
{
Maps.putIn(translated, key, Strings.toArray(value, splitter), overwrite);
}
else
{
Maps.putIn(translated, key, value, overwrite);
}
}
}
else if (defValue != null)
Expand All @@ -853,8 +857,7 @@ private static Tuple<String, Rule> lookup(

final Map<String, Object> values = Maps.downcase(Maps.get(rule, Values));

return new Tuple<>(name, (data, translated) ->
{
return new Tuple<>(name, (data, translated) -> {
if (predicate == null || predicate.test(data))
{
final Object value = source.get(data, name);
Expand Down Expand Up @@ -986,23 +989,6 @@ private static Object url(final Object o)
}
}

static List<Object> toArray(final Object value)
{
if (value instanceof String)
{
// Split the string and convert to list
final String[] split = ((String) value).trim().split("\\s+");
return Maps.typecast(Arrays.asList(split));
}
else if (value instanceof List<?>)
{
return Maps.typecast(value);
}

// Everything else, including null, gets wrapped in a single-item list
return Collections.singletonList(value);
}

private static Object toDouble(final Object o)
{
if (o instanceof Number) return ((Number) o).doubleValue();
Expand Down Expand Up @@ -1043,8 +1029,7 @@ private static Object toLong(final Object o)
{
final String n = ((String) o).trim();

if (!n.isEmpty())
return Long.decode(n);
if (!n.isEmpty()) return Long.decode(n);
}
catch (final NumberFormatException ignore)
{
Expand All @@ -1066,8 +1051,7 @@ private static Object toInt(final Object o)
{
final String n = ((String) o).trim();

if (!n.isEmpty())
return Integer.decode(n);
if (!n.isEmpty()) return Integer.decode(n);
}
catch (final NumberFormatException ignore)
{
Expand Down
Loading

0 comments on commit 960ae8a

Please sign in to comment.