Starlark

Starlark

On the web UI, lekkos are presented in a Python dialect called Starlark (opens in a new tab).

Example
export(
    Config(
        description = "Example lekko in Starlark form",
        default = False,
        overrides = [
            ("rate > 0.75", False)
            ("region in [\"Africa\", \"Asia\"]", True),
            ("verbose == true", True),
        ],
    ),
)

Starlark is a configuration language initially developed for the Bazel build tool. As such, it's well-suited for configuration data and why it was chosen to be the initial DSL (domain-specific language) for Lekko. It's Python-like in syntax, which many developers are familiar with. It also executes hermetically, ensuring that the code does not have access to I/O like the file system or the network.

ℹ️

We're working on bringing your native languages like TypeScript and Go to the web UI editing experience soon. Stay tuned!

Syntax

Lekkos written in Starlark - and their syntactic components - have semantically equivalent counterparts in other languages. To learn more about the semantic components of lekkos, see Anatomy of a lekko.

Description

Descriptions in Starlark are equivalent to function-level comments in other languages (e.g. jsdoc)

Example
export(
    Config(
        description = "Example lekko in Starlark form",
        default = False,
        overrides = [
            ("rate > 0.75", False)
            ("region in [\"Africa\", \"Asia\"]", True),
            ("verbose == true", True),
        ],
    ),
)

Default value

The default value, specified by the default field, is the value returned by the lekko when none of the overrides are satisfied. This is equivalent to the final return value in native language functions.

Example
export(
    Config(
        description = "Example lekko in Starlark form",
        default = False,
        overrides = [
            ("rate > 0.75", False)
            ("region in [\"Africa\", \"Asia\"]", True),
            ("verbose == true", True),
        ],
    ),
)

Overrides

Lekkos define conditional logic based on context variables, and each conditional branch is called an override. This is equivalent to each branch in an if statement.

Overrides are composed of rules (the conditions) and return values. In Starlark, each override is written as a tuple containing the rule and the return value.

Example
export(
    Config(
        description = "Example lekko in Starlark form",
        default = False,
        overrides = [
            ("rate > 0.75", False)
            ("region in [\"Africa\", \"Asia\"]", True),
            ("verbose == true", True),
        ],
    ),
)

Rules

In Starlark, rules are written as string literals in their own custom language. This language supports a rich set of operators and arbitrary logical nesting to support complex conditional logic.

ℹ️

Currently, rules must be defined and passed as string literals in Starlark code. This means that double quotes around string values must be escaped (e.g. "x == \"value\"" instead of "x == "value"").

StarlarkSupported TypesExampleNotes
eq or ==bool, string, int, float"user_id == 1234"
ne or !=bool, string, int, float"user_id != 1234"
lt or <int, float"ratio < 0.3"
le or <=int, float"ratio <= 0.3"
gt or >int, float"ratio > 0.3"
ge or >=int, float"ratio >= 0.3"
costring"error_msg co \"parsing error\""
swstring"pod_name sw \"main\""
ewstring"email ew \"@lekko.com\""
inbool, string, int, float"env in [\"dev\", \"staging\"]Values inside the list must be of the same type

Type

Unlike in our supported languages, you don't explicitly write the return type in Starlark. Instead, the types of the default value and each return value in all of the lekko's overrides must be the same.

Example
export(
    Config(
        description = "Example lekko in Starlark form",
        default = False,
        overrides = [
            ("rate > 0.75", False)
            ("region in [\"Africa\", \"Asia\"]", True),
            ("verbose == true", True),
        ],
    ),
)