--- spec: openflange.org/pipe/v0.1 id: order-totals version: 0.1.0 title: Order totals description: Computes the total of each order from its line items and routes malformed orders to `rejected`. license: Apache-2.0 keywords: [starlark, example, orders] maturity: experimental scope: record inputs: orders: description: 'Orders: {"id", "lines": [{"sku", "qty", "cents"}]}.' outputs: totals: description: 'One record per valid order: {"id", "items", "totalCents", "currency"} (+ "large" when above the threshold).' rejected: required: false description: 'Orders that could not be processed, as {"order", "reason"}. If not connected, a malformed order fails the run.' parameters: currency: type: string default: EUR pattern: "^[A-Z]{3}$" description: ISO 4217 code copied into each total. largeAbove: type: integer default: 100000 minimum: 0 description: Totals above this many cents are flagged with "large":true. implementation: type: script language: starlark path: totals.star --- # Order totals A Starlark **script** pipe: several statements, helper functions, two outputs and parameters. It runs in-process — no subprocess, no permissions. ```sh printf '%s\n' '{"id":1,"lines":[{"sku":"a","qty":2,"cents":1250},{"sku":"b","qty":1,"cents":99}]}' \ | openflange run ./examples/starlark-transform # {"id":1,"items":3,"totalCents":2599,"currency":"EUR"} ``` What the script shows: - `process(item, params)` is called once per input record, in order; - `emit(port, value)` writes to a named output — any number of times per item; - `enabled(port)` tells whether an optional output is connected, so the script can decide between "route the bad record" and "fail loudly"; - integers are exact (totals are computed in cents, never in floats); - `fail(msg)` stops the run with `E_RUNTIME`. Malformed orders go to `rejected` when that output is connected: ```sh openflange run ./examples/starlark-transform \ --input orders=orders.ndjson --output totals=- --output rejected=rejected.ndjson ``` Test it with `openflange test ./examples/starlark-transform`. Implements the [OpenFlange](https://openflange.org/) specification.