Documentation
¶
Overview ¶
Package options provides a set of options for the client.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Apply ¶
Apply applies a slice of functions to a value.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/options"
)
type Server struct {
Name string
}
func WithName(p string) options.Option[*Server] {
return func(s *Server) {
s.Name = p
}
}
func main() {
s := &Server{}
options.Apply(
s,
WithName("localhost"),
)
fmt.Printf("%s\n", s.Name)
}
Output: localhost
func ApplyE ¶
ApplyE applies a slice of error-returning functions to a value and stops on the first encountered error.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/foomo/go/options"
)
type Server struct {
Name string
}
func WithNameE(p string) options.OptionE[*Server] {
return func(s *Server) error {
if len(p) == 0 {
return errors.New("invalid name")
}
s.Name = p
return nil
}
}
func main() {
s := &Server{}
err := options.ApplyE(
s,
WithNameE("localhost"),
)
fmt.Println(err)
fmt.Println(s.Name)
}
Output: <nil> localhost
Types ¶
type Builder ¶
Builder is a generic option builder that collects functional options.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/options"
)
type MyOptions struct {
Name string
}
type MyOptionsBuilder struct {
options.Builder[*MyOptions]
}
func NewMyOptionsBuilder() *MyOptionsBuilder {
return &MyOptionsBuilder{
options.Builder[*MyOptions]{},
}
}
func (b *MyOptionsBuilder) WithName(name string) *MyOptionsBuilder {
b.Opts = append(b.Opts, func(o *MyOptions) {
o.Name = name
})
return b
}
func main() {
b := NewMyOptionsBuilder()
b.WithName("example")
o := MyOptions{}
options.Build(&o, b)
fmt.Println(o)
}
Output: {example}
type BuilderE ¶
type BuilderE[T any] struct { Opts []OptionE[T] }
BuilderE is a generic option builder that collects functional error-returning options.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/options"
)
type MyOptions struct {
Name string
}
type MyOptionsBuilderE struct {
options.BuilderE[*MyOptions]
}
func MyBuilderE() *MyOptionsBuilderE {
return &MyOptionsBuilderE{
options.BuilderE[*MyOptions]{},
}
}
func (b *MyOptionsBuilderE) WithName(name string) *MyOptionsBuilderE {
b.Opts = append(b.Opts, func(o *MyOptions) error {
o.Name = name
return nil
})
return b
}
func main() {
b := MyBuilderE()
b.WithName("example")
o := MyOptions{}
if err := options.BuildE(&o, b); err != nil {
panic(err)
}
fmt.Println(o)
}
Output: {example}
Click to show internal directories.
Click to hide internal directories.