core

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2023 License: ISC Imports: 17 Imported by: 2

README

Core is a low-dependency collection of things I find useful in Go.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrStringRegexpNoMatch = errors.New("string did not match regexp")

ErrStringRegexpNoMatch is an error wrapped and returned by functions created by ParseStringRegexp if the string passed did not match the regular expression used.

Functions

func FilteringHTTPHandler

func FilteringHTTPHandler(handler http.Handler, filters ...HTTPFilterFunc) http.Handler

FilteringHTTPHandler returns a handler that will check that a request was not filtered before handing it over to the passed handler.

func Flag added in v0.3.0

func Flag[T any](fs *flag.FlagSet, name string, value T, usage string, parse ParseFunc[T]) *T

Flag works like other flag.FlagSet methods, except it is generic. The passed ParseFunc will be used to parse raw arguments into a useful T value. A valid *T is returned for use by the caller.

func FlagFeatureVar added in v0.5.0

func FlagFeatureVar(fs *flag.FlagSet, f *Feature, name, usage string)

func FlagSlice added in v0.3.0

func FlagSlice[T any](fs *flag.FlagSet, name string, values []T, usage string, parse ParseFunc[T], sep string) *[]T

FlagSlice works like FlagT, except slices are created; flags created that way can therefore be repeated. A valid *[]T is returned for use by the caller.

A separator can also be passed so that multiple values may be passed as a single argument. An empty string disables that behavior. Note that having a separator still allows for repeated flags, so the following, with a ‘,’ separator, are equivalent:

- -flag=val -flag=val-2 -flag=val-3 - -flag=val,val-2 -flag=val-3 - -flag=val,val-2,val-3

func FlagSliceVar added in v0.3.0

func FlagSliceVar[T any](fs *flag.FlagSet, p *[]T, name string, usage string, parse ParseFunc[T], sep string)

FlagSliceVar works like FlagTSlice, except it is up to the caller to supply a valid *[]T.

func FlagVar added in v0.3.0

func FlagVar[T any](fs *flag.FlagSet, p *T, name string, usage string, parse ParseFunc[T])

FlagVar works like FlagT, except it is up to the caller to supply a valid *T.

func InitFlagSet added in v0.3.0

func InitFlagSet(fs *flag.FlagSet, env []string, cfg map[string]string, args []string) (err error)

InitFlagSet initializes a flag.FlagSet by setting flags in the following order: environment variables, then an arbitrary map, then command line arguments.

Note that InitFlagSet does not require the use of the Flag functions defined in this package. Standard flags will work just as well.

func Listen

func Listen(addr string) (net.Listener, error)

Listen is a wrapper around net.Listen. If addr cannot be split in two parts around the first colon found, Listen will try to create a UNIX or TCP net.Listener depending on whether addr contains a slash.

func MapKeys added in v0.6.0

func MapKeys[T ~map[K]V, K comparable, V any](m T) []K

MapKeys returns a slice containing all the keys of the map supplied. It basically is https://pkg.go.dev/golang.org/x/exp/maps#Keys, but that package is still unstable.

func Must

func Must[T any](val T, err error) T

Must panics if err is not nil. It returns val otherwise.

func ParseString

func ParseString(s string) (string, error)

ParseString is a trivial function that is designed to be used with FlagSlice and FlagSliceVar.

func ParseTime

func ParseTime(s string) (time.Time, error)

ParseTime parses a string according to the time.RFC3339 format.

func SliceMap added in v0.6.0

func SliceMap[T ~[]S, S, U any](f func(S) U, ts T) []U

SliceMap applies a function to a slice and returns a new slice made of the returned values.

Types

type Feature added in v0.5.0

type Feature struct {
	Name string
	// contains filtered or unexported fields
}

Feature represent a code feature that can be enabled and disabled.

Feature must not be copied after its first use.

func FlagFeature added in v0.5.0

func FlagFeature(fs *flag.FlagSet, name string, enabled bool, usage string) *Feature

FlagFeature creates a feature that, i.e. a boolean flag that can potentially be changed at run time.

func (*Feature) Disable added in v0.5.0

func (f *Feature) Disable()

func (*Feature) Enable added in v0.5.0

func (f *Feature) Enable()

func (*Feature) Enabled added in v0.5.0

func (f *Feature) Enabled() bool

func (*Feature) String added in v0.5.0

func (f *Feature) String() string

type HTTPFilterFunc

type HTTPFilterFunc func(http.ResponseWriter, *http.Request) bool

HTTPFilterFunc describes a filtering function for HTTP headers. The filtering function must return true if a request should be filtered and false otherwise. The filtering function may only call functions on the http.ResponseWriter or change the http.Request if a request is filtered.

func FilterHTTPMethod

func FilterHTTPMethod(methods ...string) HTTPFilterFunc

FilterHTTPMethod is an HTTPFilterFunc that filters requests based on the HTTP methods passed. Requests that do not have a matching method will be filtered.

type NoCopy added in v0.3.0

type NoCopy struct{}

NoCopy flags a type that embeds it as not to be copied. Go does not prevent values from being copied, but ‘go vet’ will pick it up and signal it, which can then be caught by many CI/CD pipelines.

See https://github.com/golang/go/issues/8005#issuecomment-190753527 for more details.

func (*NoCopy) Lock added in v0.3.0

func (*NoCopy) Lock()

func (*NoCopy) Unlock added in v0.3.0

func (*NoCopy) Unlock()

type ParseFunc

type ParseFunc[T any] func(string) (T, error)

ParseFunc describes functions that will parse a string and return a value or an error.

func ParseProtobufEnum added in v0.5.0

func ParseProtobufEnum[T ~int32](values map[string]int32) ParseFunc[T]

ParseProtobufEnum returns a ParseFunc that will return the appropriate enum value or a UnknownEnumValueError if the string passed did not match any of the values supplied.

Strings are compared in uppercase only, so ‘FOO,’ ‘foo,’, and ‘fOo’ all refer to the same value.

Callers should pass the protoc-generated *_value directly. See https://developers.google.com/protocol-buffers/docs/reference/go-generated#enum for more details.

func ParseStringEnum added in v0.5.0

func ParseStringEnum(values ...string) ParseFunc[string]

ParseStringEnum returns a ParseFunc that will return the string passed if it matched any of the values supplied. If no such match is found, an UnknownEnumValueError is returned.

Note that unlike ParseProtobufEnum, comparison is case-sensitive.

func ParseStringRegexp added in v0.7.0

func ParseStringRegexp(r *regexp.Regexp) ParseFunc[string]

ParseStringRegexp returns a ParseFunc that will return the string passed if it matches the regular expression.

func ParseStringerEnum added in v0.6.0

func ParseStringerEnum[T fmt.Stringer](values ...T) ParseFunc[T]

ParseStringerEnum returns a ParseFunc that will return the first value having a string value matching the string passed.

type PipeListener

type PipeListener struct {
	// contains filtered or unexported fields
}

PipeListener is a net.Listener that works over a pipe. It provides dialer functions that can be used in an HTTP client or gRPC options.

PipeListener must not be copied after its first use.

func ListenPipe

func ListenPipe() *PipeListener

func (*PipeListener) Accept

func (p *PipeListener) Accept() (net.Conn, error)

func (*PipeListener) Addr

func (p *PipeListener) Addr() net.Addr

func (*PipeListener) Close

func (p *PipeListener) Close() error

func (*PipeListener) Dial

func (p *PipeListener) Dial(_, _ string) (net.Conn, error)

func (*PipeListener) DialContext

func (p *PipeListener) DialContext(ctx context.Context, _, _ string) (net.Conn, error)

func (*PipeListener) DialContextGRPC

func (p *PipeListener) DialContextGRPC(ctx context.Context, _ string) (net.Conn, error)

type T

type T struct {
	*testing.T
	Options cmp.Options
	// contains filtered or unexported fields
}

T is a wrapper around the standard testing.T. It adds a few helper functions, but behaves otherwise like testing.T.

func (*T) Assert added in v0.2.0

func (t *T) Assert(b bool) bool

func (*T) AssertEqual

func (t *T) AssertEqual(exp, actual any) bool

func (*T) AssertErrorAs added in v0.5.0

func (t *T) AssertErrorAs(target any, err error) bool

func (*T) AssertErrorIs

func (t *T) AssertErrorIs(target, err error) bool

func (*T) AssertNot added in v0.2.0

func (t *T) AssertNot(b bool) bool

func (*T) AssertNotEqual

func (t *T) AssertNotEqual(notExp, actual any) bool

func (*T) AssertNotPanics

func (t *T) AssertNotPanics(f func()) (b bool)

func (*T) AssertPanics

func (t *T) AssertPanics(f func()) bool

func (*T) AssertPanicsWith

func (t *T) AssertPanicsWith(f func(), exp any) (b bool)

func (*T) Go

func (t *T) Go(f func())

func (*T) Must

func (t *T) Must(b bool)

func (*T) Run

func (t *T) Run(name string, f func(t *T))

func (*T) Wait

func (t *T) Wait()

type UnknownEnumValueError added in v0.5.0

type UnknownEnumValueError[T any] struct {
	Actual   string
	Expected []T
}

UnknownEnumValueError is returned by the functions produced by ParseProtobufEnum and ParseStringEnum when an unknown value is encountered.

func (UnknownEnumValueError[T]) Error added in v0.5.0

func (err UnknownEnumValueError[T]) Error() string

Jump to

Keyboard shortcuts

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