package builder
Import Path
github.com/lann/builder (on go.dev)
Dependency Relation
imports 4 packages, and imported by one package
Involved Source Files
Package builder provides a method for writing fluent immutable builders.
reflect.go
registry.go
Code Examples
package main
import (
"fmt"
"github.com/lann/builder"
)
// Simple math expression allowing multiple adds/subtracts and a single
// (optional) multiply.
type simpleExpr struct {
Multiplier int
Adds []int
Subtracts []int
}
func (e simpleExpr) Equals() (total int) {
for _, i := range e.Adds {
total += i
}
for _, i := range e.Subtracts {
total -= i
}
if e.Multiplier != 0 {
total *= e.Multiplier
}
return
}
// Start builder definition
type simpleExprBuilder builder.Builder
func (b simpleExprBuilder) Multiplier(i int) simpleExprBuilder {
return builder.Set(b, "Multiplier", i).(simpleExprBuilder)
}
func (b simpleExprBuilder) Add(i int) simpleExprBuilder {
return builder.Append(b, "Adds", i).(simpleExprBuilder)
}
func (b simpleExprBuilder) Subtract(i int) simpleExprBuilder {
return builder.Append(b, "Subtracts", i).(simpleExprBuilder)
}
func (b simpleExprBuilder) Equals() int {
return builder.GetStruct(b).(simpleExpr).Equals()
}
// SimpleExprBuilder is an empty builder
var SimpleExprBuilder = builder.Register(simpleExprBuilder{}, simpleExpr{}).(simpleExprBuilder)
// End builder definition
func main() {
b := SimpleExprBuilder.Add(10).Subtract(3)
// Intermediate values can be reused
b2 := b.Multiplier(2)
b3 := b.Multiplier(3)
fmt.Println(b.Equals(), b2.Equals(), b3.Equals())
}
Package-Level Type Names (only one, which is exported)
Builder stores a set of named values.
New types can be declared with underlying type Builder and used with the
functions in this package. See example.
Instances of Builder should be treated as immutable. It is up to the
implementor to ensure mutable values set on a Builder are not mutated while
the Builder is in use.
builderMap ps.Map
var EmptyBuilder
Package-Level Functions (total 17, in which 10 are exported)
Append returns a copy of the given builder with new value(s) appended to the
named list. If the value was previously unset or set with Set (even to a e.g.
slice values), the new value(s) will be appended to an empty list.
Delete returns a copy of the given builder with the given named value unset.
Extend behaves like Append, except it takes a single slice or array value
which will be concatenated to the named list.
Unlike a variadic call to Append - which requires a []interface{} value -
Extend accepts slices or arrays of any type.
Extend will panic if the given value is not a slice, array, or nil.
Get retrieves a single named value from the given builder.
If the value has not been set, it returns (nil, false). Otherwise, it will
return (value, true).
If the named value was last set with Append or Extend, the returned value
will be a slice. If the given Builder has been registered with Register or
RegisterType and the given name is an exported field of the registered
struct, the returned slice will have the same type as that field. Otherwise
the slice will have type []interface{}. It will panic if the given name is a
registered struct's exported field and the value set on the Builder is not
assignable to the field.
GetMap returns a map[string]interface{} of the values set in the given
builder.
See notes on Get regarding returned slices.
GetStruct builds a new struct from the given registered builder.
It will return nil if the given builder's type has not been registered with
Register or RegisterValue.
All values set on the builder with names that start with an uppercase letter
(i.e. which would be exported if they were identifiers) are assigned to the
corresponding exported fields of the struct.
GetStruct will panic if any of these "exported" values are not assignable to
their corresponding struct fields.
GetStructLike builds a new struct from the given builder with the same type
as the given struct.
All values set on the builder with names that start with an uppercase letter
(i.e. which would be exported if they were identifiers) are assigned to the
corresponding exported fields of the struct.
ScanStruct will panic if any of these "exported" values are not assignable to
their corresponding struct fields.
Register wraps RegisterType, taking instances instead of Types.
Returns an empty instance of the registered builder type which can be used
as the initial value for builder expressions. See example.
RegisterType maps the given builderType to a structType.
This mapping affects the type of slices returned by Get and is required for
GetStruct to work.
Returns a Value containing an empty instance of the registered builderType.
RegisterType will panic if builderType's underlying type is not Builder or
if structType's Kind is not Struct.
Set returns a copy of the given builder with a new value set for the given
name.
Set (and all other functions taking a builder in this package) will panic if
the given builder's underlying type is not Builder.
Package-Level Variables (total 5, in which 1 are exported)
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |