Categorygithub.com/lann/builder
modulepackage
0.0.0-20180802200727-47ae307949d0
Repository: https://github.com/lann/builder.git
Documentation: pkg.go.dev

# README

Builder - fluent immutable builders for Go

GoDoc Build Status

Builder was originally written for Squirrel, a fluent SQL generator. It is probably the best example of Builder in action.

Builder helps you write fluent DSLs for your libraries with method chaining:

resp := ReqBuilder.
    Url("http://golang.org").
    Header("User-Agent", "Builder").
    Get()

Builder uses immutable persistent data structures (these, specifically) so that each step in your method chain can be reused:

build := WordBuilder.AddLetters("Build")
builder := build.AddLetters("er")
building := build.AddLetters("ing")

Builder makes it easy to build structs using the builder pattern (surprise!):

import "github.com/lann/builder"

type Muppet struct {
    Name string
    Friends []string
}

type muppetBuilder builder.Builder

func (b muppetBuilder) Name(name string) muppetBuilder {
    return builder.Set(b, "Name", name).(muppetBuilder)
}

func (b muppetBuilder) AddFriend(friend string) muppetBuilder {
    return builder.Append(b, "Friends", friend).(muppetBuilder)
}

func (b muppetBuilder) Build() Muppet {
    return builder.GetStruct(b).(Muppet)
}

var MuppetBuilder = builder.Register(muppetBuilder{}, Muppet{}).(muppetBuilder)
MuppetBuilder.
    Name("Beaker").
    AddFriend("Dr. Honeydew").
    Build()

=> Muppet{Name:"Beaker", Friends:[]string{"Dr. Honeydew"}}

License

Builder is released under the MIT License.

# Functions

Append returns a copy of the given builder with new value(s) appended to the named 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.
Get retrieves a single named value from the given builder.
GetMap returns a map[string]interface{} of the values set in the given builder.
GetStruct builds a new struct from the given registered builder.
GetStructLike builds a new struct from the given builder with the same type as the given struct.
Register wraps RegisterType, taking instances instead of Types.
RegisterType maps the given builderType to a structType.
Set returns a copy of the given builder with a new value set for the given name.

# Variables

No description provided by the author

# Structs

Builder stores a set of named values.