-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathexample_test.go
60 lines (45 loc) · 1.34 KB
/
example_test.go
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
package builder_test
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 Example_basic() {
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())
// Output:
// 7 14 21
}