Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lann/builder
Fluent immutable builders for golang
https://github.com/lann/builder
Last synced: 4 days ago
JSON representation
Fluent immutable builders for golang
- Host: GitHub
- URL: https://github.com/lann/builder
- Owner: lann
- License: mit
- Created: 2014-01-13T00:45:06.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2018-08-02T20:07:27.000Z (over 6 years ago)
- Last Synced: 2024-10-30T18:05:33.165Z (17 days ago)
- Language: Go
- Homepage:
- Size: 17.6 KB
- Stars: 220
- Watchers: 3
- Forks: 17
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Builder - fluent immutable builders for Go
[![GoDoc](https://godoc.org/github.com/lann/builder?status.png)](https://godoc.org/github.com/lann/builder)
[![Build Status](https://travis-ci.org/lann/builder.png?branch=master)](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).