Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): rename json and ion filter/functions #4465

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class DocumentationGenerator {
@Override
public Map<String, Filter> getFilters() {
Map<String, Filter> filters = new HashMap<>();
filters.put("json", new JsonEncodeFilter());
filters.put("json", new ToJsonFilter());
return filters;
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public Map<String, Filter> getFilters() {
filters.put("jq", new JqFilter());
filters.put("escapeChar", new EscapeCharFilter());
filters.put("json", new JsonFilter());
filters.put("jsonEncode", new JsonEncodeFilter());
filters.put("toJson", new ToJsonFilter());
filters.put("keys", new KeysFilter());
filters.put("number", new NumberFilter());
filters.put("urldecode", new UrlDecoderFilter());
Expand Down Expand Up @@ -109,7 +109,7 @@ public Map<String, Function> getFunctions() {

functions.put("now", new NowFunction());
functions.put("json", new JsonFunction());
functions.put("jsonDecode", new JsonDecodeFunction());
functions.put("fromJson", new FromJsonFunction());
functions.put("currentEachOutput", new CurrentEachOutputFunction());
functions.put("secret", secretFunction);
functions.put("kv", kvFunction);
Expand All @@ -124,7 +124,7 @@ public Map<String, Function> getFunctions() {
functions.put("decrypt", new DecryptFunction());
functions.put("yaml", new YamlFunction());
functions.put("printContext", new PrintContextFunction());
functions.put("ionDecode", new IonDecodeFunction());
functions.put("fromIon", new FromIonFunction());

return functions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

@Slf4j
@Deprecated
public class JsonFilter extends JsonEncodeFilter {
public class JsonFilter extends ToJsonFilter {

@Override
public Object apply(Object input, Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) throws PebbleException {
log.warn("The 'json' filter is deprecated, please use 'jsonEncode' instead");
log.warn("The 'json' filter is deprecated, please use 'toJson' instead");

return super.apply(input, args, self, context, lineNumber);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.List;
import java.util.Map;

public class JsonEncodeFilter implements Filter {
public class ToJsonFilter implements Filter {
private static final ObjectMapper MAPPER = JacksonMapper.ofJson();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.List;
import java.util.Map;

public class IonDecodeFunction implements Function {
public class FromIonFunction implements Function {
private static final ObjectMapper MAPPER = JacksonMapper.ofIon();

public List<String> getArgumentNames() {
Expand All @@ -21,15 +21,15 @@ public List<String> getArgumentNames() {
@Override
public Object execute(Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) {
if (!args.containsKey("ion")) {
throw new PebbleException(null, "The 'ion' function expects an argument 'ion'.", lineNumber, self.getName());
throw new PebbleException(null, "The 'fromIon' function expects an argument 'ion'.", lineNumber, self.getName());
}

if (args.get("ion") == null) {
return null;
}

if (!(args.get("ion") instanceof String)) {
throw new PebbleException(null, "The 'ion' function expects an argument 'ion' with type string.", lineNumber, self.getName());
throw new PebbleException(null, "The 'fromIon' function expects an argument 'ion' with type string.", lineNumber, self.getName());
}

String ion = (String) args.get("ion");;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.List;
import java.util.Map;

public class JsonDecodeFunction implements Function {
public class FromJsonFunction implements Function {
private static final ObjectMapper MAPPER = JacksonMapper.ofJson();

public List<String> getArgumentNames() {
Expand All @@ -21,15 +21,15 @@ public List<String> getArgumentNames() {
@Override
public Object execute(Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) {
if (!args.containsKey("json")) {
throw new PebbleException(null, "The 'json' function expects an argument 'json'.", lineNumber, self.getName());
throw new PebbleException(null, "The 'fromJson' function expects an argument 'json'.", lineNumber, self.getName());
}

if (args.get("json") == null) {
return null;
}

if (!(args.get("json") instanceof String)) {
throw new PebbleException(null, "The 'json' function expects an argument 'json' with type string.", lineNumber, self.getName());
throw new PebbleException(null, "The 'fromJson' function expects an argument 'json' with type string.", lineNumber, self.getName());
}

String json = (String) args.get("json");;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

@Slf4j
@Deprecated
public class JsonFunction extends JsonDecodeFunction {
public class JsonFunction extends FromJsonFunction {

@Override
public Object execute(Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) {
log.warn("The 'json' function is deprecated, please use 'jsonDecode' instead");
log.warn("The 'json' function is deprecated, please use 'fromJson' instead");

return super.execute(args, self, context, lineNumber);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

@KestraTest
class JsonEncodeFilterTest {
class ToJsonFilterTest {
@Inject
VariableRenderer variableRenderer;

Expand Down Expand Up @@ -47,44 +47,44 @@ void jsonEncodeFilter() throws IllegalVariableEvaluationException {
))
);

String render = variableRenderer.render("{{ vars.second.string | jsonEncode }}", vars);
String render = variableRenderer.render("{{ vars.second.string | toJson }}", vars);
assertThat(render, is("\"string\""));

render = variableRenderer.render("{{ vars.second.int | jsonEncode }}", vars);
render = variableRenderer.render("{{ vars.second.int | toJson }}", vars);
assertThat(render, is("1"));

render = variableRenderer.render("{{ vars.second.float | jsonEncode }}", vars);
render = variableRenderer.render("{{ vars.second.float | toJson }}", vars);
assertThat(render, is("1.123"));

render = variableRenderer.render("{{ vars.second.list | jsonEncode }}", vars);
render = variableRenderer.render("{{ vars.second.list | toJson }}", vars);
assertThat(render, is("[\"string\",1,1.123]"));

render = variableRenderer.render("{{ vars.second.bool | jsonEncode }}", vars);
render = variableRenderer.render("{{ vars.second.bool | toJson }}", vars);
assertThat(render, is("true"));

render = variableRenderer.render("{{ vars.second.date | jsonEncode }}", vars);
render = variableRenderer.render("{{ vars.second.date | toJson }}", vars);
assertThat(render, is("\"" + date.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME) + "\""));

render = variableRenderer.render("{{ vars.second.map | jsonEncode }}", vars);
render = variableRenderer.render("{{ vars.second.map | toJson }}", vars);
assertThat(render, containsString("\"int\":1"));
assertThat(render, containsString("\"int\":1"));
assertThat(render, containsString("\"float\":1.123"));
assertThat(render, containsString("\"string\":\"string\""));
assertThat(render, startsWith("{"));
assertThat(render, endsWith("}"));

render = variableRenderer.render("{{ {\"empty_object\":{}} | jsonEncode }}", Map.of());
render = variableRenderer.render("{{ {\"empty_object\":{}} | toJson }}", Map.of());
assertThat(render, is("{\"empty_object\":{}}"));

render = variableRenderer.render("{{ null | jsonEncode }}", Map.of());
render = variableRenderer.render("{{ null | toJson }}", Map.of());
assertThat(render, is("null"));
}

@Test
void exception() {
assertThrows(IllegalVariableEvaluationException.class, () -> variableRenderer.render("{{ | jsonEncode }}", Map.of()));
assertThrows(IllegalVariableEvaluationException.class, () -> variableRenderer.render("{{ | toJson }}", Map.of()));

assertThrows(IllegalVariableEvaluationException.class, () -> variableRenderer.render("{{ {not: json} | jsonEncode }}", Map.of()));
assertThrows(IllegalVariableEvaluationException.class, () -> variableRenderer.render("{{ {not: json} | toJson }}", Map.of()));
}

@Test
Expand All @@ -95,7 +95,7 @@ void jsonFilter() throws IllegalVariableEvaluationException {
))
);

String render = variableRenderer.render("{{ vars.second.string | jsonEncode }}", vars);
String render = variableRenderer.render("{{ vars.second.string | toJson }}", vars);
assertThat(render, is("\"string\""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

@KestraTest
class IonDecodeFunctionTest {
class FromIonFunctionTest {
@Inject
VariableRenderer variableRenderer;

@Test
void ionDecodeFunction() throws IllegalVariableEvaluationException {
String render = variableRenderer.render("{{ ionDecode('{date:2024-04-21T23:00:00.000Z, title:\"Main_Page\",views:109787}').title }}", Map.of());
String render = variableRenderer.render("{{ fromIon('{date:2024-04-21T23:00:00.000Z, title:\"Main_Page\",views:109787}').title }}", Map.of());
assertThat(render, is("Main_Page"));

render = variableRenderer.render("{{ ionDecode(null) }}", Map.of());
render = variableRenderer.render("{{ fromIon(null) }}", Map.of());
assertThat(render, emptyString());
}

@Test
void exception() {
assertThrows(IllegalVariableEvaluationException.class, () -> variableRenderer.render("{{ ionDecode() }}", Map.of()));
assertThrows(IllegalVariableEvaluationException.class, () -> variableRenderer.render("{{ fromIon() }}", Map.of()));

assertThrows(IllegalVariableEvaluationException.class, () -> variableRenderer.render("{{ ionDecode('{not: ion') }}", Map.of()));
assertThrows(IllegalVariableEvaluationException.class, () -> variableRenderer.render("{{ fromIon('{not: ion') }}", Map.of()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,33 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

@KestraTest
class JsonDecodeFunctionTest {
class FromJsonFunctionTest {
@Inject
VariableRenderer variableRenderer;

@Test
void jsonDecodeFunction() throws IllegalVariableEvaluationException {
String render = variableRenderer.render("{{ jsonDecode('{\"test1\": 1, \"test2\": 2, \"test3\": 3}').test1 }}", Map.of());
String render = variableRenderer.render("{{ fromJson('{\"test1\": 1, \"test2\": 2, \"test3\": 3}').test1 }}", Map.of());
assertThat(render, is("1"));

render = variableRenderer.render("{{ jsonDecode('{\"test1\": [{\"test1\": 666}, 2, 3], \"test2\": 2, \"test3\": 3}').test1[0].test1 }}", Map.of());
render = variableRenderer.render("{{ fromJson('{\"test1\": [{\"test1\": 666}, 2, 3], \"test2\": 2, \"test3\": 3}').test1[0].test1 }}", Map.of());
assertThat(render, is("666"));

render = variableRenderer.render("{{ jsonDecode('[1, 2, 3]')[0] }}", Map.of());
render = variableRenderer.render("{{ fromJson('[1, 2, 3]')[0] }}", Map.of());
assertThat(render, is("1"));

render = variableRenderer.render("{{ jsonDecode('{\"empty_object\":{}}') }}", Map.of());
render = variableRenderer.render("{{ fromJson('{\"empty_object\":{}}') }}", Map.of());
assertThat(render, is("{\"empty_object\":{}}"));

render = variableRenderer.render("{{ jsonDecode(null) }}", Map.of());
render = variableRenderer.render("{{ fromJson(null) }}", Map.of());
assertThat(render, emptyString());
}

@Test
void exception() {
assertThrows(IllegalVariableEvaluationException.class, () -> variableRenderer.render("{{ jsonDecode() }}", Map.of()));
assertThrows(IllegalVariableEvaluationException.class, () -> variableRenderer.render("{{ fromJson() }}", Map.of()));

assertThrows(IllegalVariableEvaluationException.class, () -> variableRenderer.render("{{ jsonDecode('{not: json}') }}", Map.of()));
assertThrows(IllegalVariableEvaluationException.class, () -> variableRenderer.render("{{ fromJson('{not: json}') }}", Map.of()));
}

@Test
Expand Down