use of org.projectnessie.cel.tools.Script in project cel-java by projectnessie.
the class JacksonScriptHostTest method simple.
@Test
void simple() throws Exception {
ScriptHost scriptHost = ScriptHost.newBuilder().registry(JacksonRegistry.newRegistry()).build();
Script script = scriptHost.buildScript("param.author == 'foo@bar.baz'").withDeclarations(Decls.newVar("param", Decls.newObjectType(MetaTest.class.getName()))).withTypes(MetaTest.class).build();
MetaTest cmMatch = MetaTest.builder().author("foo@bar.baz").build();
MetaTest cmNoMatch = MetaTest.builder().author("foo@foo.foo").build();
assertThat(script.execute(Boolean.class, singletonMap("param", cmMatch))).isTrue();
assertThat(script.execute(Boolean.class, singletonMap("param", cmNoMatch))).isFalse();
}
use of org.projectnessie.cel.tools.Script in project cel-java by projectnessie.
the class JacksonScriptHostTest method complexInput.
@Test
void complexInput() throws Exception {
ScriptHost scriptHost = ScriptHost.newBuilder().registry(JacksonRegistry.newRegistry()).build();
Script script = scriptHost.buildScript("param.entries[0].type == org.projectnessie.cel.types.jackson.types.ClassWithEnum.ClassEnum.VAL_2").withDeclarations(Decls.newVar("param", Decls.newObjectType(ObjectListEnum.class.getName()))).withTypes(ObjectListEnum.class).build();
ObjectListEnum val = ObjectListEnum.builder().addEntries(ObjectListEnum.Entry.builder().type(ClassEnum.VAL_2).holder(new ClassWithEnum("foo")).build()).build();
assertThat(script.execute(Boolean.class, singletonMap("param", val))).isTrue();
// same as above, but use the 'container'
script = scriptHost.buildScript("param.entries[0].type == ClassWithEnum.ClassEnum.VAL_2").withDeclarations(Decls.newVar("param", Decls.newObjectType(ObjectListEnum.class.getName()))).withContainer("org.projectnessie.cel.types.jackson.types").withTypes(ObjectListEnum.class).build();
assertThat(script.execute(Boolean.class, singletonMap("param", val))).isTrue();
// return the enum
script = scriptHost.buildScript("param.entries[0].type").withDeclarations(Decls.newVar("param", Decls.newObjectType(ObjectListEnum.class.getName()))).withContainer("org.projectnessie.cel.types.jackson.types").withTypes(ObjectListEnum.class).build();
assertThat(script.execute(Integer.class, singletonMap("param", val))).isEqualTo(ClassEnum.VAL_2.ordinal());
}
use of org.projectnessie.cel.tools.Script in project nessie by projectnessie.
the class TreeApiImpl method filterReferences.
/**
* Applies different filters to the {@link Stream} of references on the filter.
*
* @param references The references that different filters will be applied to
* @param filter The filter to filter by
* @return A potentially filtered {@link Stream} of commits based on the filter
*/
private Stream<Reference> filterReferences(Stream<Reference> references, String filter) {
if (Strings.isNullOrEmpty(filter)) {
return references;
}
final Script script;
try {
script = SCRIPT_HOST.buildScript(filter).withContainer(CONTAINER).withDeclarations(REFERENCES_DECLARATIONS).withTypes(REFERENCES_TYPES).build();
} catch (ScriptException e) {
throw new IllegalArgumentException(e);
}
return references.filter(reference -> {
try {
ReferenceMetadata refMeta = reference.getMetadata();
if (refMeta == null) {
refMeta = CELUtil.EMPTY_REFERENCE_METADATA;
}
CommitMeta commit = refMeta.getCommitMetaOfHEAD();
if (commit == null) {
commit = CELUtil.EMPTY_COMMIT_META;
}
return script.execute(Boolean.class, ImmutableMap.of(VAR_REF, reference, VAR_REF_TYPE, reference.getType().name(), VAR_COMMIT, commit, VAR_REF_META, refMeta));
} catch (ScriptException e) {
throw new RuntimeException(e);
}
});
}
use of org.projectnessie.cel.tools.Script in project nessie by projectnessie.
the class TreeApiImpl method filterCommitLog.
/**
* Applies different filters to the {@link Stream} of commits based on the filter.
*
* @param logEntries The commit log that different filters will be applied to
* @param filter The filter to filter by
* @return A potentially filtered {@link Stream} of commits based on the filter
*/
private Stream<LogEntry> filterCommitLog(Stream<LogEntry> logEntries, String filter) {
if (Strings.isNullOrEmpty(filter)) {
return logEntries;
}
final Script script;
try {
script = SCRIPT_HOST.buildScript(filter).withContainer(CONTAINER).withDeclarations(COMMIT_LOG_DECLARATIONS).withTypes(COMMIT_LOG_TYPES).build();
} catch (ScriptException e) {
throw new IllegalArgumentException(e);
}
return logEntries.filter(logEntry -> {
try {
List<Operation> operations = logEntry.getOperations();
if (operations == null) {
operations = Collections.emptyList();
}
// ContentKey has some @JsonIgnore attributes, which would otherwise not be accessible.
List<Object> operationsForCel = operations.stream().map(CELUtil::forCel).collect(Collectors.toList());
return script.execute(Boolean.class, ImmutableMap.of(VAR_COMMIT, logEntry.getCommitMeta(), VAR_OPERATIONS, operationsForCel));
} catch (ScriptException e) {
throw new RuntimeException(e);
}
});
}
Aggregations