attr

package module
v0.0.0-...-25d0939 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2022 License: MIT Imports: 2 Imported by: 47

README

attr

the Go key-value data structure library

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Map

func Map(attrs ...Attr) map[string]any

Types

type Attr

type Attr interface {
	// Key returns the string key of the attribute Attr
	Key() string
	// Value returns the (any) value of the attribute Attr
	Value() any
	// WithKey returns a copy of this Attr, with key `key`
	WithKey(key string) Attr
	// WithValue returns a copy of this Attr, with value `value`
	//
	// It must be the same type of the original Attr, otherwise returns
	// nil
	WithValue(value any) Attr
}

Attr interface describes the behavior that a serializable attribute should have.

Besides retrieving its key and value, it also permits creating a copy of the original Attr with a different key or a different value

func Complex

func Complex[T ComplexRestriction](key string, value T) Attr

Complex is a generic function to create Attr attributes based on complex values. It converts the values to complex128

func Float

func Float[T FloatRestriction](key string, value T) Attr

Float is a generic function to create Attr attributes based on float values. It converts the values to float64

func Int

func Int[T IntRestriction](key string, value T) Attr

Int is a generic function to create Attr attributes based on int values. It converts the values to int64

func New

func New[T any](key string, value T) Attr

New is a generic function to create an Attr

Using a generic approach allows the Attr.WithValue method to be scoped with certain constraints for specific applications

func Ptr

func Ptr[T any](key string, value *T) Attr

Ptr is a generic function to create an Attr from a pointer value

Using a generic approach allows the Attr.WithValue method to be scoped with certain constraints for specific applications

func String

func String[T CharRestriction](key string, value T) Attr

String is a generic function to create Attr attributes based on string values. It converts the values to string

func Uint

func Uint[T UintRestriction](key string, value T) Attr

Uint is a generic function to create Attr attributes based on uint values. It converts the values to uint64

type Attrs

type Attrs []Attr

func (Attrs) MarshalJSON

func (attrs Attrs) MarshalJSON() ([]byte, error)

MarshalJSON encodes the attributes as a JSON object (key-value pairs)

func (Attrs) MarshalText

func (attrs Attrs) MarshalText() (text []byte, err error)

func (Attrs) String

func (attrs Attrs) String() string

String implements fmt.Stringer

type BoolRestriction

type BoolRestriction interface {
	~bool
}

BoolRestriction is a constraint that only accepts booleans

type CharRestriction

type CharRestriction interface {
	~string | ~byte | ~rune | ~[]byte | ~[]rune
}

CharRestriction is a constraint that only accepts stringifiable tokens

type ComplexRestriction

type ComplexRestriction interface {
	~complex64 | ~complex128
}

ComplexRestriction is a constraint that only accepts complex values

type FloatRestriction

type FloatRestriction interface {
	~float32 | ~float64
}

FloatRestriction is a constraint that only accepts float values

type IntRestriction

type IntRestriction interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

IntRestriction is a constraint that only accepts int values

type NumberRestriction

type NumberRestriction interface {
	IntRestriction | UintRestriction | FloatRestriction | ComplexRestriction
}

NumberRestriction is a constraint that only accepts number values, as a combination of other constraints

type TextRestriction

type TextRestriction interface {
	CharRestriction | BoolRestriction | TimeRestriction
}

TextRestriction is a constraint that only accepts text values, as a combination of other constraints

type TimeRestriction

type TimeRestriction interface {
	time.Time
}

TimeRestriction is a constraint that only accepts time.Time values

type UintRestriction

type UintRestriction interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

UintRestriction is a constraint that only accepts uint values

Jump to

Keyboard shortcuts

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