utils

package
v0.59.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2024 License: MIT Imports: 6 Imported by: 4

Documentation

Overview

Package utils provides various helper functions used in the library.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoFieldCollected means no field could be collected
	ErrNoFieldCollected = errors.New("no field was collected")
	// ErrArgNotStruct means the argument should be a struct but wasn't
	ErrArgNotStruct = errors.New("argument is not a struct")
	// ErrNotCollectable means the value can't be collected on the given platform
	ErrNotCollectable = fmt.Errorf("cannot be collected on %s %s", runtime.GOOS, runtime.GOARCH)
	// ErrNotExported means the struct has an unexported field
	ErrNotExported = errors.New("field not exported by the struct")
	// ErrCannotRender means a field which cannot be rendered
	ErrCannotRender = errors.New("field inner type cannot be rendered")
	// ErrNoValueMethod means a field doesn't have a Value method
	ErrNoValueMethod = errors.New("field doesn't have the expected Value method")
	// ErrNoJSONTag means a field doesn't have a json tag
	ErrNoJSONTag = errors.New("field doesn't have a json tag")
)

Functions

func AsJSON added in v0.47.0

func AsJSON[T any](info *T, useDefault bool) (interface{}, []string, error)

AsJSON takes a structure and returns a marshal-able object representing the fields of the struct, the lists of errors for fields for which the collection failed, and an error if it failed.

If useDefault is true, fields which failed to be collected will be included in the marshal-able object as their default value, otherwise they are ignored.

Fields which are not exported, don't have a json tag or are not of type Value[T] for a T which can be rendered cause the function to return an error.

The string list contain errors of fields which failed to be collected. Fields which are not collected on the platform are ignored.

If the error is non-nil, the first two parameters are unspecified.

func StringFromBytes added in v0.47.0

func StringFromBytes(slice []byte) string

StringFromBytes converts a null-terminated (C-style) string to a Go string

The given slice must be null-terminated.

func ValueParseFloat64Setter added in v0.47.0

func ValueParseFloat64Setter(value *Value[float64]) func(string, error)

ValueParseFloat64Setter returns a function which parses a float64 from its string argument and stores the result in the given Value

func ValueParseInt64Setter added in v0.47.0

func ValueParseInt64Setter(value *Value[uint64]) func(string, error)

ValueParseInt64Setter returns a function which parses an uint64 from its string argument and stores the result in the given Value

func ValueParseSetter added in v0.47.0

func ValueParseSetter[T any](value *Value[T], parse func(string) (T, error)) func(string, error)

ValueParseSetter returns a function which parses its input and stores the result in the given Value

func ValueStringSetter added in v0.47.0

func ValueStringSetter(value *Value[string]) func(string, error)

ValueStringSetter returns a function which stores a string and an error in the given Value

Types

type Jsonable added in v0.48.0

type Jsonable interface {
	AsJSON() (interface{}, []string, error)
}

Jsonable represents a type which can be converted to a mashallable object

type Value added in v0.47.0

type Value[T any] struct {
	// contains filtered or unexported fields
}

Value represents either an error or an actual value of type T.

The default value of the type is an error saying that the value was not initialized.

func NewErrorValue added in v0.47.0

func NewErrorValue[T any](err error) Value[T]

NewErrorValue initializes a Value[T] with the given error.

Note that if err is nil, the returned Value[T] is fundamentally equivalent to a Value[T] containing the default value of T and no error.

func NewValue added in v0.47.0

func NewValue[T any](value T) Value[T]

NewValue initializes a Value[T] with the given value of type T and no error.

func NewValueFrom added in v0.47.0

func NewValueFrom[T any](value T, err error) Value[T]

NewValueFrom returns a Value[T] from a value and an error. If the error is non-nil then it represents this error, otherwise it represents the value.

This is a convenient function to get a Value from a function which returns a value and an error.

func (*Value[T]) Error added in v0.47.0

func (value *Value[T]) Error() error

Error returns the error stored in the Value[T], or nil if it doesn't contain an error.

func (*Value[T]) Value added in v0.47.0

func (value *Value[T]) Value() (T, error)

Value returns the value and the error stored in the Value[T].

If it contains an error, the returned value is unspecified.

func (*Value[T]) ValueOrDefault added in v0.48.0

func (value *Value[T]) ValueOrDefault() T

ValueOrDefault returns the value stored in the Value[T], or the default value of the type in case of error.

Jump to

Keyboard shortcuts

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