eloquent

command module
v0.0.0-...-a06dd94 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 12, 2021 License: MIT, Unlicense Imports: 11 Imported by: 0

README

eloquent

Generate fluent APIs in Go.

A "fluent" API is sometimes called the "builder pattern": method chaining is used to optionally set values on an object.

Such methods are tedius to write by-hand, because there is no logic.

This tool writes the methods for you. Invoke with go generate, or manually.

Usage

// foo.go
//go:generate eloquent $GOFILE

type Foo struct {
    // First doc comment.
    First string
    // Second doc comment.
    Second bool
    Third int
    ignored float32
}

go generate

// foo_eloquent.go

// WithFirst doc comment.
func (f Foo) WithFirst(s string) Foo {
    f.First = s
    return f
}

// WithSecond doc comment.
func (f Foo) WithSecond(b string) Foo {
    f.Second = b
    return f
}

func (f Foo) WithThird(n string) Foo {
    f.Third = n
    return f
}
// main.go

func main() {
    foo := NewFoo().
        WithFirst("first").
        WithSecond(false).
        WithThird(3)
}

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL