Bree

Getting started with Go

This guide adds a rule to a Go program: write it, generate its Go types, and call it. The rules themselves are the same as in any Bree project; Rules and types covers them in full.

Requirements

  • Linux on amd64 or arm64. macOS and Windows are not supported yet.
  • Go 1.24 or newer, with cgo enabled (the default) and a C compiler.
  • Rust 1.99 or newer, a C++ compiler and objcopy, to build the engine.

Build the engine

bree-go links the Bree engine, which is written in Rust, as a static library. There are no prebuilt libraries yet, so build one from the engine's source:

git clone https://github.com/breeso/bree ~/bree
export CGO_LDFLAGS="-L$(bash ~/bree/scripts/libbree.sh)"

The script builds libbree.a (a few minutes the first time) and prints the folder it is in; CGO_LDFLAGS tells cgo to link it from there. Set it in every shell that builds your program, or in your shell profile. Build the library from the same commit of breeso/bree as the bree-go version you use.

After rebuilding the library, run go clean -cache. Go's build cache does not notice that a C library changed, and would keep linking the old one.

Add bree-go

go get github.com/breeso/bree-go@main

Write a rule

Rules live in a Go package of their own. Make a rules folder with a Go file that runs bree-gen:

// rules/rules.go

// Package rules holds the business rules, in Luau.
package rules

//go:generate go run github.com/breeso/bree-go/cmd/bree-gen

and the rule, rules/free_shipping.luau:

--!strict
-- Whether an order ships free.

export type Country = "CA" | "US"

export type Input = {
	-- The order total before tax, in cents.
	order_total_cents: int,
	country: Country,
}

export type Output = {
	free: boolean,
}

local THRESHOLD_CENTS = { CA = 7500, US = 5000 }

return function(input: Input): Output
	if input.order_total_cents < 0 then
		invalid("order_total_cents", "must not be negative")
	end
	return { free = input.order_total_cents >= THRESHOLD_CENTS[input.country] }
end

and its tests, rules/free_shipping.test.luau:

--!strict
local free_shipping = require("./free_shipping")

test("US orders ship free from $50", function()
	expect(free_shipping({ order_total_cents = 5000, country = "US" })).toEqual({ free = true })
end)

test("Canadian orders under $75 pay for shipping", function()
	expect(free_shipping({ order_total_cents = 7499, country = "CA" }).free).toBe(false)
end)

test("a negative total is rejected", function()
	expect(function()
		free_shipping({ order_total_cents = -1, country = "US" })
	end).toBeInvalid("order_total_cents")
end)

These are the files bree init writes, if you install the bree command. It also writes bree.d.luau, which lets your editor check rules as you type; see Editor setup.

Generate

go generate ./...

bree-gen type checks the rules, runs the tests, and writes rules/bree_rules.go. Commit it, as you would any generated Go file. For the rule above it contains:

// FreeShipping evaluates rule "free_shipping" (free_shipping.luau).
//
// Whether an order ships free.
var FreeShipping = bree.Rule[FreeShippingInput, FreeShippingOutput]{Name: "free_shipping", Fingerprint: "ef6a9cd84918712d"}

type FreeShippingInput struct {
	// The order total before tax, in cents.
	OrderTotalCents int64               `json:"order_total_cents"`
	Country         FreeShippingCountry `json:"country"`
}

type FreeShippingCountry string

const (
	FreeShippingCountryCA FreeShippingCountry = "CA"
	FreeShippingCountryUS FreeShippingCountry = "US"
)

type FreeShippingOutput struct {
	Free bool `json:"free"`
}

and Rules, the rule sources embedded with //go:embed. Type names start with the rule's name, so rules never collide in one package.

Call it

package main

import (
	"fmt"
	"log"

	"github.com/breeso/bree-go"

	"example.com/shop/rules"
)

func main() {
	engine, err := bree.Open(rules.Rules)
	if err != nil {
		log.Fatal(err)
	}
	defer engine.Close()

	d, err := rules.FreeShipping.Decide(engine, rules.FreeShippingInput{
		OrderTotalCents: 8000,
		Country:         rules.FreeShippingCountryCA,
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("free shipping:", d.Output.Free)
}
$ go run .
free shipping: true

Open one engine when the program starts and share it: it is safe to use from many goroutines. bree.Open checks and tests the rules again as it loads them, and refuses rules whose types differ from the generated ones.

Break it

Each of these mistakes stops before the program builds.

Change >= to > in the rule. The types are fine, but a test fails, and bree-gen leaves bree_rules.go as it was:

$ go generate ./...
free_shipping.test.luau:5: test "US orders ship free from $50" failed
    expected { free = true }
         got { free = false }

bree-gen: 1 problem; bree_rules.go was not written
exit status 1
rules/rules.go:4: running "go": exit status 1

Misspell a field in the rule:

free_shipping.luau:22: Key 'order_totl_cents' not found in table 'Input'

bree-gen: 1 problem; bree_rules.go was not written

Use a field the rule does not have in Go:

$ go build .
./main.go:20:3: unknown field OrderTotal in struct literal of type rules.FreeShippingInput

In CI

Run go generate ./... and fail if anything changed, so a rule edited without regenerating cannot be merged:

go generate ./...
git diff --exit-code

Or, from the rules package, check without writing: go run github.com/breeso/bree-go/cmd/bree-gen -check.

Next steps

Edit this page on GitHub