Reference
Package github.com/breeso/bree-go (imported as bree), the bree-gen command, and the engine library they link.
The engine library
bree-go evaluates rules with the Bree engine, written in Rust, linked in with cgo as the static library libbree.a. Build it from a checkout of breeso/bree:
export CGO_LDFLAGS="-L$(bash ~/bree/scripts/libbree.sh)"| Platforms | Linux amd64 and arm64. macOS and Windows are not supported yet. |
| Builds need | Rust 1.99+, a C++ compiler and objcopy (from binutils) |
| Programs need | cgo (CGO_ENABLED=1, the default) and a C compiler at build time |
| Size | libbree.a is about 44 MB. The Laravel Go example, which links it, builds to a 61 MB binary. |
| Version | bree.Version is the version of the linked engine |
The header, include/bree.h, ships with bree-go, so cgo only needs to be told where the library is. Build the library from the same commit of breeso/bree as the bree-go version you use: the two share a C interface that may change until the first release.
The script strips the LLVM bitcode sections from the archive (objcopy --remove-section=.llvmbc --remove-section=.llvmcmd), because Go's linker refuses archives that carry them.
Go's build cache does not track C libraries. After rebuilding libbree.a, run go clean -cache, or programs keep linking the old one.
Opening an engine
engine, err := bree.Open(rules.Rules, bree.WithLimits(bree.Limits{Timeout: 200 * time.Millisecond}))
if err != nil {
return err // a *bree.LoadError listing every problem
}
defer engine.Close()bree.Open(bundle, opts...) |
Loads a bundle that bree-gen generated: type checks the rules, runs their tests, and checks each rule still has the types the code was generated for. |
bree.OpenDir(dir, opts...) |
Loads a directory with no generated types to check against. For tools, and programs that only use Evaluate. |
engine.Close() |
Frees the engine and stops its reload loop. Calls after Close fail. Safe to call twice. |
An engine is safe to use from many goroutines. Open one when the program starts and share it.
| Option | |
|---|---|
bree.WithDir(dir) |
Load the rules from a directory instead of the bundle's embedded files, keeping the bundle's types. |
bree.WithReload(interval) |
Look for changes in the WithDir directory this often. A change is used once it type checks, passes its tests and keeps the generated types. See Reloading. |
bree.WithLogger(logger) |
Where reloads and refused changes are logged. Default: slog.Default(). |
bree.WithLimits(limits) |
Step, memory and time limits for every evaluation. |
Limits
type Limits struct {
MaxSteps uint64 // default 1,000,000
MaxMemoryBytes uint64 // default 16 MiB
Timeout time.Duration // default 1 second
}A zero field keeps the default. See The runtime.
Deciding
bree-gen generates a bree.Rule[Input, Output] for each rule:
rules.X.Decide(engine, input) |
Evaluates with the generated input struct. |
rules.X.DecideJSON(engine, body) |
Evaluates JSON input, such as a request body. The rule's Input type checks it, so a handler needs no struct of its own, and the output is still typed. |
engine.Evaluate(name, json) |
Evaluates any rule by name, JSON in and out. |
engine.Rules() |
The loaded rules: name, digest and fingerprint. |
A decision:
type Decision[O any] struct {
Rule string // "free_shipping"
Digest string // "sha256:...", over the rule and every module it requires
Output O
}Errors
Evaluations fail with a *bree.Error:
d, err := rules.FreeShipping.Decide(engine, input)
var e *bree.Error
if errors.As(err, &e) && e.Code == bree.InvalidInput {
for _, v := range e.Violations {
fmt.Println(v.Field, v.Message) // order_total_cents must not be negative
}
}| Code | Constant | HTTPStatus() |
|---|---|---|
invalid_request |
bree.InvalidRequest |
400 |
rule_not_found |
bree.RuleNotFound |
404 |
types_mismatch |
bree.TypesMismatch |
409 |
invalid_input |
bree.InvalidInput |
422 |
invalid_output |
bree.InvalidOutput |
500 |
rule_error |
bree.RuleError |
500 |
execution_limit_exceeded |
bree.ExecutionLimitExceeded |
500 |
memory_limit_exceeded |
bree.MemoryLimitExceeded |
500 |
Violation.Field is a dotted path such as service_limits.0.service, counting array positions from 0. What each code means is in The runtime.
Open and OpenDir fail with a *bree.LoadError, whose Problems each have a File, a Line (nil when the problem is with the whole file) and a Message, and print as file:line: message.
bree-gen
//go:generate go run github.com/breeso/bree-go/cmd/bree-gen| Flag | Default | |
|---|---|---|
-rules |
. |
The rules directory, relative to the package. It must be inside the package, because //go:embed cannot reach outside it. |
-package |
the package go generate runs in, or of the Go files in the directory |
The package of the generated file. |
-out |
bree_rules.go |
The file to write. |
-check |
Write nothing; exit 1 if the file is missing or out of date. For CI. |
It type checks the rules, runs their tests and writes the file only if everything passes. Otherwise it prints each problem, says the file was not written, and exits 1.
What it generates
For each rule, types named after the rule (free_shipping gives FreeShippingInput, FreeShippingOutput, FreeShippingCountry), so rules never collide in one package, and a bree.Rule value named after the rule (FreeShipping). For the directory, Rules, a bree.Bundle with the sources embedded.
| Luau | Go |
|---|---|
boolean, number, int, string |
bool, float64, int64, string |
"a" | "b" |
a string type, with a constant per value: FreeShippingCountryCA |
T? on a field |
*T with omitempty; slices and maps stay as they are |
{ T } |
[]T |
{ [string]: T } |
map[string]T |
| a table with fields | a struct with a json tag per field |
Field and constant names follow Go's spelling of initialisms: order_id is OrderID, "CA" is CA. Luau comments become Go doc comments. Structs with non-optional slices or maps get a MarshalJSON method that sends a nil slice or map as an empty one, so a zero value is valid input.
Using it as a library
bree.ReadFiles(fsys, root) reads the .luau files under root, and bree.GenerateGo(root, pkg, files) returns the generated file, gofmt'd. bree-gen is a thin wrapper around both.