Skip to content

Commit

Permalink
0.2.3 - treat values of objects with keys $$ to be present on parent …
Browse files Browse the repository at this point in the history
…paths
  • Loading branch information
elisherer committed Jun 17, 2024
1 parent e047c70 commit 96808a2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion java/json-transform/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group 'co.nlighten'
version = '0.2.1'
version = '0.2.3'

ext {
gsonVersion = "2.10.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public abstract class JsonTransformer<JE, JA extends Iterable<JE>, JO extends JE> implements Transformer {

static final String OBJ_DESTRUCT_KEY = "*";
static final String FUNCTION_PREFIX = "$$";

private final JsonAdapter<JE, JA, JO> adapter;
protected final JE definition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ private static <JE, JA extends Iterable<JE>, JO extends JE> void findAllVariable
var matcher = variableDetectionRegExp.matcher(string);
while (matcher.find()) {
var v = matcher.group();
if (v.endsWith("$$")) v = v.substring(0, v.length() - 2);
if (!result.containsKey(v)) result.put(v, path);
}
} else if (adapter.jArray.is(element)) {
Expand All @@ -37,9 +36,14 @@ private static <JE, JA extends Iterable<JE>, JO extends JE> void findAllVariable
}
} else if (adapter.jObject.is(element)) {
var coll = adapter.jObject.type.cast(element);
var isObjectFunction = adapter.jObject.keySet(coll).stream().anyMatch(x -> x.startsWith("$$"));
adapter.jObject.keySet(coll).forEach(x -> {
var value = adapter.jObject.get(coll, x);
findAllVariableUses(adapter, value, result, JsonTransformer.OBJ_DESTRUCT_KEY.equals(x) ? path : path + "." + x);
findAllVariableUses(adapter, value, result,
JsonTransformer.OBJ_DESTRUCT_KEY.equals(x) || isObjectFunction
? path
: path + "." + x
);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,21 @@ void strings() {
"$.input2", "$",
"#now", "$"
), result);

result = JsonTransformerUtils.findAllVariableUses(adapter, adapter.parse("""
{
"filters": {
"locked": {
"$$map": "$.parameters.locked$$",
"to": "$$boolean:##current",
"x_target": "$.parameters.x_source"
}
}
}
"""));
Assertions.assertEquals(Map.of(
"$.parameters.locked$$", "$.filters.locked",
"$.parameters.x_source", "$.filters.locked"
), result);
}
}

0 comments on commit 96808a2

Please sign in to comment.