How do I write more than a one-liner without a subprocess?
Available — v0.0.1 (experimental)starlark
Transform records with Starlark
A Starlark script — a process(item, params) function — runs inside the runner, sandboxed, with no I/O. No subprocess, no permissions.
Available in v0.0.1: the files, README and tests are in examples/starlark-transform of the openflange-go repository. Commands run from the repository root.
Needs: only the openflange CLI.
A pipe is a data-processing step with declared inputs and outputs.
What goes in, what comes out
Input (stdin)
{"id":1,"lines":[{"sku":"a","qty":2,"cents":1250},{"sku":"b","qty":1,"cents":99}]}Command
printf '%s\n' '{"id":1,"lines":[{"sku":"a","qty":2,"cents":1250},{"sku":"b","qty":1,"cents":99}]}' | openflange run ./examples/starlark-transformOutput (stdout)
{"id":1,"items":3,"totalCents":2599,"currency":"EUR"}Files and further commands
examples/starlark-transform/totals.star — the process function
def process(item, params):
reason = _problem(item)
if reason != None:
if enabled("rejected"):
emit("rejected", {"order": item, "reason": reason})
return
fail("malformed order (%s) and the rejected output is not connected" % reason)
total = 0
count = 0
for line in item["lines"]:
total += line["qty"] * line["cents"]
count += line["qty"]
out = {
"id": item["id"],
"items": count,
"totalCents": total,
"currency": params["currency"],
}
if total > params["largeAbove"]:
out["large"] = True
emit("totals", out)What to notice
- Starlark is a small, deterministic Python dialect that runs inside the runner. It cannot open files or sockets, so it needs no grants.
process(item, params)is called once per record, in order.emit(port, value)writes to a named output;enabled(port)tells whether an optional output is connected.- Integers are exact: totals are computed in cents, and a 30-digit quantity keeps every digit.
- Parameters (
currency,largeAbove) are validated before the run starts. - For a single expression you need no file at all: the standard library’s
std:maptakes one as a parameter.