logic

package
v0.8.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package logic groups kit setters and applies them under control-flow helpers. It is the containerkit equivalent of go-contain's tools package.

c := kit.NewContainer(
	logic.Group(
		kit.Name("api"),
		kit.Image("alpine:latest"),
		logic.When(isProd, kit.Env("CACHE", "1")),
	),
)

Helpers return kit.Grouped, which implements every kit option interface, so a group can be passed to NewContainer, NewService, nested Health/Volume scopes, Engine actions, and Compose runtime methods.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Case

func Case(cond bool, opts ...any) kit.Grouped

Case is a Switch branch taken when cond is true.

func CaseFn

func CaseFn(pred Predicate, opts ...any) kit.Grouped

CaseFn is a Switch branch taken when pred() is true.

func Default

func Default(opts ...any) kit.Grouped

Default is a Switch branch that always matches. Place it last.

func Each

func Each[T any](items []T, f func(i int, item T) any) kit.Grouped

Each maps items to setters and groups them. A nil factory is a no-op.

func EachKV

func EachKV[K comparable, V any](items map[K]V, f func(k K, v V) any) kit.Grouped

EachKV maps each key and value to a setter and groups them. A nil map or factory is a no-op. Iteration order follows the map.

func Group

func Group(opts ...any) kit.Grouped

Group combines setters into one. Errors from the inner setters are joined. Nil setters are skipped.

func Map

func Map[T any](items []T, f func(item T) any) kit.Grouped

Map maps items to setters without an index. A nil factory is a no-op.

func On

func On[T comparable](got, want T, opts ...any) kit.Grouped

On is a Switch branch taken when got equals want.

func OnlyIf

func OnlyIf(check Check, opt any) kit.Grouped

OnlyIf applies the setter when check returns true. A failed check returns its error instead of skipping. A nil check is an error.

func Switch

func Switch(cases ...kit.Grouped) kit.Grouped

Switch applies the first matching Case, CaseFn, or Default.

func Unless

func Unless(cond bool, opts ...any) kit.Grouped

Unless applies setters when cond is false.

func UnlessFn

func UnlessFn(pred Predicate, opts ...any) kit.Grouped

UnlessFn applies setters when pred() is false.

func When

func When(cond bool, opts ...any) kit.Grouped

When applies setters when cond is true. Nil setters are skipped.

func WhenElse

func WhenElse(cond bool, thenOpt, elseOpt any) kit.Grouped

WhenElse applies thenOpt when cond is true and elseOpt otherwise.

func WhenElseFn

func WhenElseFn(pred Predicate, thenOpt, elseOpt any) kit.Grouped

WhenElseFn applies thenOpt when pred() is true and elseOpt otherwise.

func WhenFn

func WhenFn(pred Predicate, opts ...any) kit.Grouped

WhenFn applies setters when pred() is true. A nil predicate skips the setters.

Types

type Check

type Check func() (bool, error)

Check is a fallible predicate. Unlike Predicate, a non-nil error stops apply rather than skipping the setter.

type Predicate

type Predicate func() bool

Predicate is a condition evaluated when a setter is applied, not when it is constructed. Use it for flags, environment, and other ambient checks.

func And

func And(preds ...bool) Predicate

And is true when every value is true. The result is a Predicate so it can be passed to WhenFn.

func AndFn

func AndFn(preds ...Predicate) Predicate

AndFn is true when every predicate is true. Nil predicates are skipped.

func Not

func Not(pred Predicate) Predicate

Not is true when pred is false. A nil predicate is false, so Not of it is true.

func Or

func Or(preds ...bool) Predicate

Or is true when any value is true. The result is a Predicate so it can be passed to WhenFn.

func OrFn

func OrFn(preds ...Predicate) Predicate

OrFn is true when any predicate is true. Nil predicates are skipped.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL