Skip to content

How to use library for functions? #259

@alexec

Description

@alexec

How can I convert the "greet" example into a lib please?

This does not work:

import org.projectnessie.cel.EnvOption;
import org.projectnessie.cel.Library;
import org.projectnessie.cel.ProgramOption;
import org.projectnessie.cel.checker.Decls;
import org.projectnessie.cel.interpreter.functions.Overload;

import java.util.List;

import static org.projectnessie.cel.common.types.StringT.stringOf;

public class GreetLib implements Library {
    @Override
    public List<EnvOption> getCompileOptions() {
        return List.of(
                EnvOption.declarations(
                        List.of(
                                Decls.newVar("i", Decls.String),
                                Decls.newVar("you", Decls.String),
                                Decls.newFunction(
                                        "greet",
                                        Decls.newInstanceOverload(
                                                "string_greet_string", List.of(Decls.String, Decls.String), Decls.String)))
                )
        );
    }

    @Override
    public List<ProgramOption> getProgramOptions() {
        return List.of(
                ProgramOption.functions(
                        Overload.binary(
                                "string_greet_string",
                                (lhs, rhs) ->
                                        stringOf(String.format("Hello %s! Nice to meet you, I'm %s.\n", rhs,
                                                lhs))))

        );
    }
}
    @Test
    void merge() throws Exception {
        String greeting = ScriptHost
                .newBuilder()
                .build()
                .buildScript("i.greet(you)")
                .withLibraries(new GreetLib())
                .build()
                .execute(String.class, Map.of("i", "alex", "you", "you"));

        assertEquals("hello alex", greeting);
    }

Results in:

org.projectnessie.cel.tools.ScriptExecutionException: no such overload: string.greet[](string)

	at org.projectnessie.cel.tools.Script.evaluate(Script.java:59)
	at org.projectnessie.cel.tools.Script.execute(Script.java:45)
	at com.intuit.pavedroad.apimanagement.exteventingestorapp.rest.services.cel.GreetLibTest.merge(GreetLibTest.java:33)
	

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions