• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.gitignoreH A D16-Feb-20188

.travis.ymlH A D16-Feb-201842

LICENSEH A D16-Feb-20181 KiB

README.mdH A D16-Feb-20181.7 KiB

builder.goH A D16-Feb-20186.4 KiB

builder_test.goH A D16-Feb-20183.9 KiB

example_test.goH A D16-Feb-20181.3 KiB

reflect.goH A D16-Feb-2018479

registry.goH A D16-Feb-20181.5 KiB

README.md

1# Builder - fluent immutable builders for Go
2
3[![GoDoc](https://godoc.org/github.com/lann/builder?status.png)](https://godoc.org/github.com/lann/builder)
4[![Build Status](https://travis-ci.org/lann/builder.png?branch=master)](https://travis-ci.org/lann/builder)
5
6Builder was originally written for
7[Squirrel](https://github.com/lann/squirrel), a fluent SQL generator. It
8is probably the best example of Builder in action.
9
10Builder helps you write **fluent** DSLs for your libraries with method chaining:
11
12```go
13resp := ReqBuilder.
14    Url("http://golang.org").
15    Header("User-Agent", "Builder").
16    Get()
17```
18
19Builder uses **immutable** persistent data structures
20([these](https://github.com/mndrix/ps), specifically)
21so that each step in your method chain can be reused:
22
23```go
24build := WordBuilder.AddLetters("Build")
25builder := build.AddLetters("er")
26building := build.AddLetters("ing")
27```
28
29Builder makes it easy to **build** structs using the **builder** pattern
30(*surprise!*):
31
32```go
33import "github.com/lann/builder"
34
35type Muppet struct {
36    Name string
37    Friends []string
38}
39
40type muppetBuilder builder.Builder
41
42func (b muppetBuilder) Name(name string) muppetBuilder {
43    return builder.Set(b, "Name", name).(muppetBuilder)
44}
45
46func (b muppetBuilder) AddFriend(friend string) muppetBuilder {
47    return builder.Append(b, "Friends", friend).(muppetBuilder)
48}
49
50func (b muppetBuilder) Build() Muppet {
51    return builder.GetStruct(b).(Muppet)
52}
53
54var MuppetBuilder = builder.Register(muppetBuilder{}, Muppet{}).(muppetBuilder)
55```
56```go
57MuppetBuilder.
58    Name("Beaker").
59    AddFriend("Dr. Honeydew").
60    Build()
61
62=> Muppet{Name:"Beaker", Friends:[]string{"Dr. Honeydew"}}
63```
64
65## License
66
67Builder is released under the
68[MIT License](http://www.opensource.org/licenses/MIT).
69