Search in sources :

Example 1 with Script

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();
}
Also used : Script(org.projectnessie.cel.tools.Script) MetaTest(org.projectnessie.cel.types.jackson.types.MetaTest) ScriptHost(org.projectnessie.cel.tools.ScriptHost) Test(org.junit.jupiter.api.Test) MetaTest(org.projectnessie.cel.types.jackson.types.MetaTest)

Example 2 with Script

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());
}
Also used : Script(org.projectnessie.cel.tools.Script) ObjectListEnum(org.projectnessie.cel.types.jackson.types.ObjectListEnum) ScriptHost(org.projectnessie.cel.tools.ScriptHost) ClassWithEnum(org.projectnessie.cel.types.jackson.types.ClassWithEnum) Test(org.junit.jupiter.api.Test) MetaTest(org.projectnessie.cel.types.jackson.types.MetaTest)

Example 3 with Script

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);
        }
    });
}
Also used : Script(org.projectnessie.cel.tools.Script) ScriptException(org.projectnessie.cel.tools.ScriptException) CommitMeta(org.projectnessie.model.CommitMeta) ReferenceMetadata(org.projectnessie.model.ReferenceMetadata) ImmutableReferenceMetadata(org.projectnessie.model.ImmutableReferenceMetadata)

Example 4 with Script

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);
        }
    });
}
Also used : Script(org.projectnessie.cel.tools.Script) ScriptException(org.projectnessie.cel.tools.ScriptException) Operation(org.projectnessie.model.Operation)

Aggregations

Script (org.projectnessie.cel.tools.Script)4 Test (org.junit.jupiter.api.Test)2 ScriptException (org.projectnessie.cel.tools.ScriptException)2 ScriptHost (org.projectnessie.cel.tools.ScriptHost)2 MetaTest (org.projectnessie.cel.types.jackson.types.MetaTest)2 ClassWithEnum (org.projectnessie.cel.types.jackson.types.ClassWithEnum)1 ObjectListEnum (org.projectnessie.cel.types.jackson.types.ObjectListEnum)1 CommitMeta (org.projectnessie.model.CommitMeta)1 ImmutableReferenceMetadata (org.projectnessie.model.ImmutableReferenceMetadata)1 Operation (org.projectnessie.model.Operation)1 ReferenceMetadata (org.projectnessie.model.ReferenceMetadata)1