blob: 3b18550bb970896a98990c65095f258d7849aba0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# Builder - fluent immutable builders for Go
[](https://godoc.org/github.com/lann/builder)
[](https://travis-ci.org/lann/builder)
Builder was originally written for
[Squirrel](https://github.com/lann/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:
```go
resp := ReqBuilder.
Url("http://golang.org").
Header("User-Agent", "Builder").
Get()
```
Builder uses **immutable** persistent data structures
([these](https://github.com/mndrix/ps), specifically)
so that each step in your method chain can be reused:
```go
build := WordBuilder.AddLetters("Build")
builder := build.AddLetters("er")
building := build.AddLetters("ing")
```
Builder makes it easy to **build** structs using the **builder** pattern
(*surprise!*):
```go
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)
```
```go
MuppetBuilder.
Name("Beaker").
AddFriend("Dr. Honeydew").
Build()
=> Muppet{Name:"Beaker", Friends:[]string{"Dr. Honeydew"}}
```
## License
Builder is released under the
[MIT License](http://www.opensource.org/licenses/MIT).
|