Documentation
¶
Overview ¶
Package w provides generic helpers for functions, slices, maps, templates, queues, and process groups.
Index ¶
- Constants
- Variables
- func AddFuncMaps(funcName string, f interface{})
- func AddVersionFlags(flagFunc func(shortVersion, fullVersion string))
- func DefaultNumber[T Number](vals ...T) T
- func DefaultPointer[T any](vals ...*T) *T
- func DefaultString(vals ...string) string
- func E[T any](_ T, err error) error
- func Filter[T any](old []T, f func(item T) bool) []T
- func Find[T any](s []T, f func(T) bool) T
- func FindIndex[T any](s []T, t T, compare func(a, b T) bool) int
- func GroupBy[T any, K comparable, V any](old []T, f func(item T) (groupKey K, value V)) map[K][]V
- func Has[T any](s []T, t T, compare func(a, b T) bool) bool
- func Include[T comparable](s []T, t T) bool
- func Index[T comparable](s []T, t T) int
- func Interfaces[T any](objs []T) []interface{}
- func JSONStringer(v any) fmt.Stringer
- func Limit[T any](s []T, limit int, hidePosition int, manySuffix ...T) []T
- func M[T any](v T, err error) T
- func Map[S any, T any](old []S, f func(item S) T) []T
- func Merge[M ~map[K]V, K comparable, V any](maps ...M) M
- func NewStringer(stringer func() string) fmt.Stringer
- func P[T any](o T) *T
- func Pipe[sT any, dT any](v sT, e error) func(func(sT) (dT, error)) (dT, error)
- func StringEqual(a, b string) bool
- func T[vT any](expr bool, trueVal, falseVal vT) vT
- type Comparable
- type Equal
- type Group
- type Number
- type OneOrMore
- type RingQueue
- type SortedMap
- type SortedMapItem
- type Stringer
- type Template
- func (t *Template) DefinedTemplates() string
- func (t *Template) Execute(wr io.Writer, data any) error
- func (t *Template) ExecuteTemplate(wr io.Writer, name string, data any) error
- func (t *Template) ExecuteToString(data any) (string, error)
- func (t Template) MarshalJSON() ([]byte, error)
- func (t Template) MarshalYAML() (interface{}, error)
- func (t *Template) Name() string
- func (t *Template) UnmarshalJSON(data []byte) (err error)
- func (t *Template) UnmarshalYAML(value *yaml.Node) (err error)
- type TemplateHandler
Constants ¶
const ( // PosRight truncates from the right, keeping the start of the slice. PosRight = iota // PosLeft truncates from the left, keeping the end of the slice. PosLeft // PosCenter truncates from the middle, keeping both ends of the slice. PosCenter )
Variables ¶
var ( // QueueFull is returned by RingQueue.Put when the queue has no remaining capacity. QueueFull = errors.New("queue is full") //nolint:revive // QueueNull is returned by RingQueue.Get when the queue is empty. QueueNull = errors.New("queue is null") //nolint:revive )
var ( // GitCommit is the git commit hash of the build, typically set via -ldflags. GitCommit string // BuildDate is the build timestamp, typically set via -ldflags. BuildDate string // GoVersion is the Go toolchain version used to build the binary, typically set via -ldflags. GoVersion string // Platform is the GOOS/GOARCH of the build, typically set via -ldflags. Platform string // Version is the semantic version of the build. It defaults to "0.0.0". Version string = "0.0.0" )
var ErrStopping = errors.New("program stopping")
ErrStopping is returned by Group.Run when a termination signal is received.
var ExitFunc = os.Exit
ExitFunc is the function used to terminate the process after a shutdown timeout. It defaults to os.Exit.
var NopInterrupt = func(err error) {}
NopInterrupt is an interrupt function that does nothing.
Functions ¶
func AddFuncMaps ¶
func AddFuncMaps(funcName string, f interface{})
AddFuncMaps registers a named function that templates can call.
func AddVersionFlags ¶
func AddVersionFlags(flagFunc func(shortVersion, fullVersion string))
AddVersionFlags normalizes BuildDate to RFC3339 when possible and passes the short and full version strings to flagFunc.
func DefaultNumber ¶
func DefaultNumber[T Number](vals ...T) T
DefaultNumber returns the first non-zero number among vals, or the zero value if all are zero.
func DefaultPointer ¶
func DefaultPointer[T any](vals ...*T) *T
DefaultPointer returns the first non-nil pointer among vals, or nil if all are nil.
func DefaultString ¶
DefaultString returns the first non-empty string among vals, or "" if all are empty.
func Find ¶
Find returns the first element for which f returns true, or the zero value of T if none matches.
func FindIndex ¶
FindIndex returns the index of the first element that compare considers equal to t, or -1 if none matches.
func GroupBy ¶
func GroupBy[T any, K comparable, V any](old []T, f func(item T) (groupKey K, value V)) map[K][]V
GroupBy groups items by the key returned from f and collects the corresponding values.
func Index ¶
func Index[T comparable](s []T, t T) int
Index returns the index of the first occurrence of t in s, or -1 if t is not present.
func Interfaces ¶
func Interfaces[T any](objs []T) []interface{}
Interfaces converts a slice of T to a slice of interface{}.
func JSONStringer ¶
JSONStringer returns an fmt.Stringer that encodes v as JSON.
func Limit ¶
Limit shortens s to at most limit elements, hiding overflow at hidePosition and appending manySuffix.
func Merge ¶
func Merge[M ~map[K]V, K comparable, V any](maps ...M) M
Merge combines maps into a single map. Later maps overwrite earlier keys.
func NewStringer ¶
NewStringer returns an fmt.Stringer that calls stringer to produce its string value.
func Pipe ¶
Pipe returns a function that applies f to v if e is nil, or returns the zero value of dT and e otherwise.
func StringEqual ¶
StringEqual reports whether a and b are equal.
Types ¶
type Comparable ¶
type Comparable interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
~float32 | ~float64 | ~string
}
Comparable is the constraint of ordered types that can be used as SortedMap keys.
type Equal ¶
type Equal[T any] interface { // Equal reports whether the receiver is equal to the given value. Equal(T) bool }
Equal is implemented by types that can compare themselves to a value of type T.
type Group ¶
Group is a collection of actors that run concurrently and interrupt together.
type Number ¶
type Number interface {
~int8 | ~int16 | ~int32 | ~int64 |
~uint8 | ~uint16 | ~uint32 | ~uint64 |
~float64 | ~float32 | ~int | ~uint
}
Number is the constraint of integer and floating-point types accepted by DefaultNumber.
type OneOrMore ¶
type OneOrMore[T any] []T
OneOrMore is a slice that marshals as a single value when it has one element, and as an array otherwise.
func (OneOrMore[T]) MarshalJSON ¶
MarshalJSON implements json.Marshaler, encoding a single-element slice as a JSON value rather than an array.
func (OneOrMore[T]) MarshalYAML ¶
MarshalYAML implements yaml.Marshaler by encoding a single-element slice as a scalar.
func (*OneOrMore[T]) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler, accepting either a single JSON value or a JSON array.
type RingQueue ¶
type RingQueue[T any] struct { // contains filtered or unexported fields }
RingQueue is a concurrent, fixed-capacity ring buffer.
func NewRingQueue ¶
NewRingQueue returns a RingQueue that can hold at most n elements.
func (*RingQueue[T]) Do ¶
func (r *RingQueue[T]) Do(f func(a T))
Do calls f for each element currently in the queue, from front to back.
func (*RingQueue[T]) Get ¶
Get removes and returns the front element. It returns QueueNull if the queue is empty.
func (*RingQueue[T]) List ¶
func (r *RingQueue[T]) List() []T
List returns a snapshot of the elements currently in the queue, from front to back.
type SortedMap ¶
type SortedMap[K Comparable, V any] []SortedMapItem[K, V]
SortedMap is a slice of key-value pairs sorted by key.
func NewSortedMap ¶
func NewSortedMap[K Comparable, V any](kvs ...map[K]V) SortedMap[K, V]
NewSortedMap builds a SortedMap from one or more maps and sorts it by key.
type SortedMapItem ¶
type SortedMapItem[K Comparable, V any] struct { // Key is the map key used for sorting. Key K // Value is the value associated with Key. Value V }
SortedMapItem is a key-value pair stored in a SortedMap.
type Stringer ¶
type Stringer struct {
// contains filtered or unexported fields
}
Stringer wraps a function as an fmt.Stringer.
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
Template holds a parsed text or HTML template together with its raw source.
func NewHTMLTemplate ¶
NewHTMLTemplate parses raw as an html/template and returns a Template.
func NewTextTemplate ¶
NewTextTemplate parses raw as a text/template and returns a Template.
func (*Template) DefinedTemplates ¶
DefinedTemplates returns a string listing the defined template names.
func (*Template) ExecuteTemplate ¶
ExecuteTemplate applies the named template to data and writes the output to wr.
func (*Template) ExecuteToString ¶
ExecuteToString applies the template to data and returns the output as a string.
func (Template) MarshalJSON ¶
MarshalJSON implements json.Marshaler by returning the raw template source.
func (Template) MarshalYAML ¶
MarshalYAML implements yaml.Marshaler by returning the raw template source.
func (*Template) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. A JSON string is parsed as a text template; a single-key object such as {"fn::html": "..."} selects the parser.
type TemplateHandler ¶
type TemplateHandler interface {
// Name returns the name of the template.
Name() string
// ExecuteTemplate applies the named template to data and writes the output to wr.
ExecuteTemplate(wr io.Writer, name string, data any) error
// Execute applies the template to data and writes the output to wr.
Execute(wr io.Writer, data any) error
// DefinedTemplates returns a string listing the defined template names.
DefinedTemplates() string
}
TemplateHandler is the common execution surface shared by text and HTML templates.