nomix

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package nomix provides a set of types and functions for handling tags and metadata in a generic way.

Index

Examples

Constants

View Source
const (
	KindBool = 0b00000001_00000000 | KindInt64
	KindInt  = 0b00000010_00000000 | KindInt64
)

Derived Tag kinds. Derived kinds are types that are derived from base kinds.

View Source
const (
	KindByteSlice    = 0b00000000_00000001 | KindSlice
	KindStringSlice  = KindString | KindSlice
	KindInt64Slice   = KindInt64 | KindSlice
	KindFloat64Slice = KindFloat64 | KindSlice
	KindTimeSlice    = KindTime | KindSlice
	KindBoolSlice    = KindBool | KindSlice
	KindIntSlice     = KindInt | KindSlice
)

Multi value (slice) Tag kinds.

Variables

View Source
var (
	// ErrMissing represents a missing set element.
	ErrMissing = errors.New("missing element")

	// ErrInvType represents an invalid element type.
	ErrInvType = errors.New("invalid element type")

	// ErrInvFormat represents an invalid element format.
	ErrInvFormat = errors.New("invalid element format")

	// ErrInvValue represents an invalid element value.
	ErrInvValue = errors.New("invalid element value")

	// ErrNoCreator represents a missing tag creator for a type.
	ErrNoCreator = errors.New("creator not found")

	// ErrNoSpec represents a missing tag spec for a type.
	ErrNoSpec = errors.New("spec not found")

	// ErrNotImpl represents a missing method implementation or
	// functionality for a type.
	ErrNotImpl = errors.New("not implemented")
)

Metadata parsing and casting errors.

Functions

func RegisterSpec added in v0.2.0

func RegisterSpec(typ any, spec KindSpec)

RegisterSpec registers a tag creator for the given type. If the type already exists, it is overwritten.

func WithBaseHEX

func WithBaseHEX(opts *Options)

WithBaseHEX sets base to hexadecimal when parsing integers.

func WithLocString

func WithLocString(opts *Options)

WithLocString is the MetaSet option allowing string timezone names.

Types

type Bool

type Bool = single[bool]

Bool is a tag for a single bool value.

func CreateBool added in v0.2.0

func CreateBool(name string, val any, _ ...Option) (*Bool, error)

CreateBool casts the given value to bool. Returns the Bool instance with the given name and nil error on success. Returns nil and ErrInvType if the value is not the bool type.

func NewBool

func NewBool(name string, val bool) *Bool

NewBool returns a new instance of Bool.

func ParseBool

func ParseBool(name, val string, _ ...Option) (*Bool, error)

ParseBool parses string representation of the boolean tag.

type BoolSlice

type BoolSlice = slice[bool]

BoolSlice is a tag for a slice of bool values.

func CreateBoolSlice added in v0.2.0

func CreateBoolSlice(name string, val any, _ ...Option) (*BoolSlice, error)

CreateBoolSlice casts the value to []bool. Returns the BoolSlice instance with the given name and nil error on success. Returns nil and ErrInvType if the value is the []bool type.

func NewBoolSlice

func NewBoolSlice(name string, val ...bool) *BoolSlice

NewBoolSlice returns a new instance of BoolSlice.

type ByteSlice

type ByteSlice = slice[byte]

ByteSlice is a tag for a slice of bytes.

func CreateByteSlice added in v0.2.0

func CreateByteSlice(name string, val any, _ ...Option) (*ByteSlice, error)

CreateByteSlice casts the value to []byte. Returns the ByteSlice instance with the given name and nil error on success. Returns nil and ErrInvType if the value is not the []byte type.

func NewByteSlice

func NewByteSlice(name string, val ...byte) *ByteSlice

NewByteSlice returns a new instance of ByteSlice.

type Definition added in v0.2.0

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

Definition represents a named tag definition. In other words, it wraps a KindSpec and a tag name.

func Define added in v0.2.0

func Define(name string, spec KindSpec, rules ...verax.Rule) *Definition

Define defines named Tag.

func (*Definition) TagCreate added in v0.2.0

func (def *Definition) TagCreate(val any, opts ...Option) (Tag, error)

TagCreate creates a new Tag matching the definition. It does not validate the value.

func (*Definition) TagKind added in v0.2.0

func (def *Definition) TagKind() TagKind

TagKind returns the tag definition kind.

func (*Definition) TagName added in v0.2.0

func (def *Definition) TagName() string

TagName returns the tag definition name.

func (*Definition) TagParse added in v0.2.0

func (def *Definition) TagParse(val string, opts ...Option) (Tag, error)

func (*Definition) Validate added in v0.3.0

func (def *Definition) Validate(val any) error

Validate validates the given value against the definition.

NOTE: The TagCreator is first used to create a Tag instance with the provided value this means that all supported by TagCreator types are supported.

type Float64

type Float64 = single[float64]

Float64 is a tag for a single float64 value.

func CreateFloat64 added in v0.2.0

func CreateFloat64(name string, val any, _ ...Option) (*Float64, error)

CreateFloat64 casts the value to float64. Returns the Float64 instance with the given name and nil error if the value is a byte, int, int8, int16, int32, int64, float32, or float64. Returns nil and ErrInvType if the value's type is not a supported numeric type.

NOTE: For int64 values outside ±2^53 range, the result is undefined. TODO(rz): Return an error when the above happens.

func NewFloat64

func NewFloat64(name string, val float64) *Float64

NewFloat64 returns a new instance of Float64.

func ParseFloat64

func ParseFloat64(name, v string, _ ...Option) (*Float64, error)

ParseFloat64 parses string representation of the 64-bit floating point tag.

type Float64Slice

type Float64Slice = slice[float64]

Float64Slice is a tag for a slice of float64 values.

func CreateFloat64Slice added in v0.2.0

func CreateFloat64Slice(name string, val any, _ ...Option) (*Float64Slice, error)

CreateFloat64Slice casts the value to []float64. Returns the Float64Slice instance with the given name and nil error if the value is a []int, []int8, []int16, []int32, []int64, []float32, or []float64. Returns nil and ErrInvType if the value's type is not a supported numeric slice type.

func NewFloat64Slice

func NewFloat64Slice(name string, v ...float64) *Float64Slice

NewFloat64Slice returns a new instance of Float64Slice.

type Int

type Int = single[int]

Int is a tag for a single int value.

func CreateInt added in v0.2.0

func CreateInt(name string, val any, _ ...Option) (*Int, error)

CreateInt casts the value to int. Returns the Int instance with the given type and nil error on success. Returns nil and ErrInvType if the value is not the int type.

func NewInt

func NewInt(name string, v int) *Int

NewInt returns a new instance of Int.

func ParseInt

func ParseInt(name, v string, opts ...Option) (*Int, error)

ParseInt parses string representation of the integer tag.

type Int64

type Int64 = single[int64]

Int64 is a tag for a single int64 value.

func CreateInt64 added in v0.2.0

func CreateInt64(name string, val any, _ ...Option) (*Int64, error)

CreateInt64 casts the value to int64. Returns the Int64 instance with the given name and nil error if the value is a byte, int, int8, int16, int32, or int64. Returns nil and ErrInvType if the value's type is not a supported numeric type.

func NewInt64

func NewInt64(name string, v int64) *Int64

NewInt64 returns a new instance of Int64.

func ParseInt64

func ParseInt64(name, v string, opts ...Option) (*Int64, error)

ParseInt64 parses string representation of the 64-bit integer tag.

type Int64Slice

type Int64Slice = slice[int64]

Int64Slice is a tag for a slice of int64 values.

func CreateInt64Slice added in v0.2.0

func CreateInt64Slice(name string, val any, _ ...Option) (*Int64Slice, error)

CreateInt64Slice casts the value to []int64. Returns the Int64Slice instance with the given name and nil error if the value is a []int, []int8, []int16, []int32, or []int64. Returns nil and ErrInvType if the value's type is not a supported numeric slice type.

func NewInt64Slice

func NewInt64Slice(name string, v ...int64) *Int64Slice

NewInt64Slice returns a new instance of Int64Slice.

type IntSlice

type IntSlice = slice[int]

IntSlice is a tag for a slice of int values.

func CreateIntSlice added in v0.2.0

func CreateIntSlice(name string, val any, _ ...Option) (*IntSlice, error)

CreateIntSlice casts the value to []int. Returns the IntSlice instance with the given name and nil error on success. Returns nil and ErrInvType if the value is not []int type.

func NewIntSlice

func NewIntSlice(name string, v ...int) *IntSlice

NewIntSlice returns a new instance of IntSlice.

type JSON

type JSON = slice[byte]

JSON is a tag for a json.RawMessage value.

func CreateJSON added in v0.2.0

func CreateJSON(name string, val any, _ ...Option) (*JSON, error)

CreateJSON casts the value to json.RawMessage. Returns the JSON with the specified name and nil error if the value is a valid JSON represented as json.RawMessage, []byte or string. Returns nil and error if the value's type is not a supported type or the value is not a valid JSON.

func NewJSON

func NewJSON(name string, v json.RawMessage) *JSON

NewJSON returns a new instance of JSON.

func ParseJSON

func ParseJSON(name, v string, _ ...Option) (*JSON, error)

ParseJSON parses string representation of the raw JSON tag.

type KindSpec added in v0.2.0

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

KindSpec is a specification for a tag kind.

func BoolSliceSpec added in v0.2.0

func BoolSliceSpec() KindSpec

BoolSliceSpec returns a KindSpec for BoolSlice type.

func BoolSpec added in v0.2.0

func BoolSpec() KindSpec

BoolSpec returns a KindSpec for Bool type.

func ByteSliceSpec added in v0.2.0

func ByteSliceSpec() KindSpec

ByteSliceSpec returns a KindSpec for ByteSlice type.

func Float64SliceSpec added in v0.2.0

func Float64SliceSpec() KindSpec

Float64SliceSpec returns a KindSpec for Float64Slice type.

func Float64Spec added in v0.2.0

func Float64Spec() KindSpec

Float64Spec returns a KindSpec for Float64 type.

func GetSpec added in v0.3.0

func GetSpec(typ any) KindSpec

GetSpec returns the KindSpec for the given type.

func Int64SliceSpec added in v0.2.0

func Int64SliceSpec() KindSpec

Int64SliceSpec returns a KindSpec for Int64Slice type.

func Int64Spec added in v0.2.0

func Int64Spec() KindSpec

Int64Spec returns a KindSpec for Int64 type.

func IntSliceSpec added in v0.2.0

func IntSliceSpec() KindSpec

IntSliceSpec returns a KindSpec for IntSlice type.

func IntSpec added in v0.2.0

func IntSpec() KindSpec

IntSpec returns a KindSpec for Int type.

func JSONSpec added in v0.2.0

func JSONSpec() KindSpec

JSONSpec returns a KindSpec for JSON type.

func NewKindSpec added in v0.2.0

func NewKindSpec(knd TagKind, tcr TagCreateFunc, tpr TagParseFunc) KindSpec

NewKindSpec creates a new KindSpec instance.

func StringSliceSpec added in v0.2.0

func StringSliceSpec() KindSpec

StringSliceSpec returns a KindSpec for StringSlice type.

func StringSpec added in v0.2.0

func StringSpec() KindSpec

StringSpec returns a KindSpec for String type.

func TimeSliceSpec added in v0.2.0

func TimeSliceSpec() KindSpec

TimeSliceSpec returns a KindSpec for TimeSlice type.

func TimeSpec added in v0.2.0

func TimeSpec() KindSpec

TimeSpec returns a KindSpec for Time type.

func (KindSpec) IsZero added in v0.2.0

func (ks KindSpec) IsZero() bool

func (KindSpec) TagCreate added in v0.2.0

func (ks KindSpec) TagCreate(
	name string,
	value any,
	opts ...Option,
) (Tag, error)

TagCreate creates a new Tag matching the [tagKind] in the spec.

func (KindSpec) TagParse added in v0.2.0

func (ks KindSpec) TagParse(name, val string, opts ...Option) (Tag, error)

TagParse creates a Tag based on its string representation.

type MetaAllGetter

type MetaAllGetter interface {
	// MetaGetAll returns all tags in the set as a map. May return nil. The
	// returned map should be treated as read-only.
	MetaGetAll() map[string]any
}

MetaAllGetter is an interface wrapping MetaGetAll method.

type MetaAllSetter

type MetaAllSetter interface {
	// MetaSetAll sets metadata on the implementor. If the value with the given
	// name already exists in the set, it will be overwritten. The nil values
	// must be ignored.
	MetaSetAll(map[string]any)
}

MetaAllSetter is an interface wrapping MetaSetAll method.

type MetaAppender

type MetaAppender interface {
	// MetaAppend appends all implementor metadata to the passed map.
	MetaAppend(map[string]any)
}

MetaAppender is an interface wrapping MetaAppend method.

type MetaFromGetter

type MetaFromGetter interface {
	// MetaSetFrom sets metadata on the implementor from the [MetaAllGetter].
	MetaSetFrom(src MetaAllGetter)
}

MetaFromGetter is an interface wrapping MetaFromGetter method.

type MetaSet

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

MetaSet represents a set of metadata key-values.

Example
set := NewMetaSet()

set.MetaSet("A", 42)
set.MetaSet("B", true)
set.MetaSet("C", "foo")

fmt.Printf("There are %d entries in the set:\n", set.MetaCount())
fmt.Printf("- A: %v\n", set.MetaGet("A"))
fmt.Printf("- B: %v\n", set.MetaGet("B"))
fmt.Printf("- C: %v\n", set.MetaGet("C"))

fmt.Printf("\nGetting metadata values:\n")

// Tag exists but is of a different type.
metaA, err := set.MetaGetBool("A")
fmt.Printf("  A: %v; err: %v\n", metaA, err)

metaC, err := set.MetaGetString("C")
fmt.Printf("  C: %v;   err: %v\n", metaC, err)
Output:

There are 3 entries in the set:
- A: 42
- B: true
- C: foo

Getting metadata values:
  A: false; err: A: invalid element type
  C: foo;   err: <nil>

func NewMetaSet

func NewMetaSet(opts ...Option) MetaSet

NewMetaSet returns a new MetaSet instance. By default, the new map is initialized with the length equal to 10.

func (MetaSet) MetaCount

func (set MetaSet) MetaCount() int

MetaCount returns the number of entries in the metadata set.

func (MetaSet) MetaDelete

func (set MetaSet) MetaDelete(key string)

func (MetaSet) MetaDeleteAll

func (set MetaSet) MetaDeleteAll()

MetaDeleteAll deletes all metadata from the set.

func (MetaSet) MetaGet

func (set MetaSet) MetaGet(key string) any

func (MetaSet) MetaGetAll

func (set MetaSet) MetaGetAll() map[string]any

func (MetaSet) MetaGetBool

func (set MetaSet) MetaGetBool(name string) (bool, error)

MetaGetBool gets a metadata value by name as a bool. Returns the value and nil error if it exists and is the bool type. Returns false and ErrMissing if the value is missing, or empty string and ErrInvType if the value is of a different type.

func (MetaSet) MetaGetBoolSlice

func (set MetaSet) MetaGetBoolSlice(name string) ([]bool, error)

MetaGetBoolSlice gets a metadata value by name as a []bool. Returns the slice and nil error if it exists and is the []bool type. Returns nil and ErrMissing if the value is missing, or nil and ErrInvType if the value is of a different type.

func (MetaSet) MetaGetFloat64

func (set MetaSet) MetaGetFloat64(name string) (float64, error)

MetaGetFloat64 gets a metadata value by name as a float64. Returns the value and nil error if it exists and is one of int, int8, int16, int32, int64, float32, float64 types. Returns 0.0 and ErrMissing if the value is missing, or 0.0 and ErrInvType if the value is of a different type.

NOTE: For int64 values outside ±2^53 range, the result is undefined.

func (MetaSet) MetaGetFloat64Slice

func (set MetaSet) MetaGetFloat64Slice(name string) ([]float64, error)

MetaGetFloat64Slice gets a metadata value by name as a []float. Returns the slice and nil error if it exists and is one of []int, []int8, []int16, []int32, []int64, []float32 or []float64 types. Returns nil and ErrMissing if the value is missing, or nil and ErrInvType if the value is of a different type.

NOTE: For int64 values outside ±2^53 range, the result is undefined.

func (MetaSet) MetaGetInt

func (set MetaSet) MetaGetInt(name string) (int, error)

MetaGetInt gets a metadata value by name as an int. Returns the value and nil error if it exists and is the int type. Returns false and ErrMissing if the value is missing, or empty string and ErrInvType if the value is of a different type.

func (MetaSet) MetaGetInt64

func (set MetaSet) MetaGetInt64(name string) (int64, error)

MetaGetInt64 gets a metadata value by name as an int64. Returns the value and nil error if it exists and is one of int, int8, int16, int32 or int64 types. Returns 0 and ErrMissing if the value is missing, or 0 and ErrInvType if the value is of a different type.

func (MetaSet) MetaGetInt64Slice

func (set MetaSet) MetaGetInt64Slice(name string) ([]int64, error)

MetaGetInt64Slice gets a metadata value by name as a []int64. Returns the slice and nil error if it exists and is one of []int, []int8, []int16, []int32 or []int64 types. Returns nil and ErrMissing if the value is missing, or nil and ErrInvType if the value is of a different type.

func (MetaSet) MetaGetIntSlice

func (set MetaSet) MetaGetIntSlice(name string) ([]int, error)

MetaGetIntSlice gets a metadata value by name as a []int. Returns the slice and nil error if it exists and is the []int type. Returns nil and ErrMissing if the value is missing, or nil and ErrInvType if the value is of a different type.

func (MetaSet) MetaGetJOSN

func (set MetaSet) MetaGetJOSN(name string) (json.RawMessage, error)

MetaGetJOSN gets a metadata value by name as a json.RawMessage. Returns the value and nil error if it exists and is one of json.RawMessage, string, []byte types. Returns nil and ErrMissing if the value is missing, or nil and ErrInvType if the value is of a different type.

func (MetaSet) MetaGetString

func (set MetaSet) MetaGetString(name string) (string, error)

MetaGetString gets a metadata value by name as a string. Returns the value and nil error if it exists and is the string type. Returns empty string and ErrMissing if the value is missing, or empty string and ErrInvType if the value is of a different type.

func (MetaSet) MetaGetStringSlice

func (set MetaSet) MetaGetStringSlice(name string) ([]string, error)

MetaGetStringSlice gets a metadata value by name as a []string. Returns the slice and nil error if it exists and is the []string type. Returns nil and ErrMissing if the value is missing, or nil and ErrInvType if the value is of a different type.

func (MetaSet) MetaGetTime

func (set MetaSet) MetaGetTime(name string, opts ...Option) (time.Time, error)

MetaGetTime gets a metadata value by name as a time.Time. Returns the value and nil error if it exists and is one of time.Time or string time representation. You may customize string the parsing by using the WithTimeFormat and WithTimeLoc options. Returns zero value time and ErrMissing if the value is missing, or zero vale time and ErrInvType if the value is of a different type.

To support string zero time values, use the WithZeroTime option.

func (MetaSet) MetaGetTimeSlice

func (set MetaSet) MetaGetTimeSlice(name string, opts ...Option) ([]time.Time, error)

MetaGetTimeSlice gets a metadata value by name as a slice of time.Time values. Returns the value and nil error if it exists and is a slice of time.Time or slice of string time representations. You may customize string the parsing by using the WithTimeFormat and WithTimeLoc options. Returns nil and ErrMissing if the value is missing, zero vale time and ErrInvType if the value is of a different type or, zero value time and ErrInvFormat if the value is not a valid time string.

To support string zero time values, use the WithZeroTime option.

func (MetaSet) MetaSet

func (set MetaSet) MetaSet(key string, value any)

type Metadata

type Metadata interface {
	// MetaGet retrieves from the set a metadata value by its name. If the name
	// does not exist in the set, it returns nil.
	MetaGet(name string) any

	// MetaSet adds a named value to the set. If the value with the given name
	// already exists in the set, it will be overwritten. Setting a nil value
	// must be implemented as a no-op.
	MetaSet(name string, value any)

	// MetaDelete removes from the set the metadata value by name. If the name
	// does not exist, the method has no effect.
	MetaDelete(name string)
}

Metadata is an interface for managing a collection of distinctly named metadata values. The implementations must not allow for nil values to be stored in the set.

type Option

type Option func(*Options)

Option represents an option function.

func WithLen

func WithLen(n int) Option

WithLen is an option to set the default length for the map.

func WithMeta

func WithMeta(m map[string]any) Option

WithMeta is an option to set the initial map for the MetaSet.

The caller must not use the passed map after the call to this option. The MetaSet becomes its new owner.

func WithTags

func WithTags(m map[string]Tag) Option

WithTags is an option to set the initial map for the TagSet.

The caller must not use the passed map after the call to this option. The TagSet becomes its new owner.

func WithTimeFormat

func WithTimeFormat(format string) Option

WithTimeFormat is the MetaSet option setting string time format.

func WithTimeLoc

func WithTimeLoc(loc *time.Location) Option

WithTimeLoc is the MetaSet option setting location for parsed time strings.

func WithZeroTime

func WithZeroTime(zero ...string) Option

WithZeroTime is MetaSet option setting zero time values.

type Options

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

Options represent a set of options used by MetaSet and TagSet.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns default options.

type Registry added in v0.2.0

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

Registry represents a collection of [KindSpec]s.

func NewRegistry added in v0.2.0

func NewRegistry() Registry

NewRegistry returns a new Registry instance.

func (Registry) Create added in v0.2.0

func (reg Registry) Create(name string, val any, opts ...Option) (Tag, error)

Create creates a new Tag for the given value. The value's type must be registered.

func (Registry) CreatorForKind added in v0.2.0

func (reg Registry) CreatorForKind(knd TagKind) (TagCreateFunc, error)

CreatorForKind returns the TagCreateFunc for the given TagKind.

func (Registry) CreatorForType added in v0.2.0

func (reg Registry) CreatorForType(typ any) (TagCreateFunc, error)

CreatorForType returns the TagCreateFunc for the given type.

func (Registry) Register added in v0.2.0

func (reg Registry) Register(typ any, spec KindSpec) KindSpec

Register registers a tag creator for the given type. If the type already exists, it is overwritten. Returns the previous creator function when the type was already registered, nil otherwise.

func (Registry) Spec added in v0.3.0

func (reg Registry) Spec(typ any) KindSpec

Spec returns the KindSpec for the given type.

type String

type String = single[string]

String is a tag for a single byte value.

func CreateString added in v0.2.0

func CreateString(name string, v any, _ ...Option) (*String, error)

CreateString casts the value to a string. Returns the String instance with the given name and nil error on success. Returns nil and ErrInvType if the value is not the string type.

func NewString

func NewString(name, v string) *String

NewString returns a new instance of String.

type StringSlice

type StringSlice = slice[string]

StringSlice is a tag for a slice of strings.

func CreateStringSlice added in v0.2.0

func CreateStringSlice(name string, val any, _ ...Option) (*StringSlice, error)

CreateStringSlice casts the value to []string. Returns the StringSlice instance with the given name and nil error on success. Returns nil and ErrInvType if the value is not the []string type.

func NewStringSlice

func NewStringSlice(name string, v ...string) *StringSlice

NewStringSlice returns a new instance of StringSlice.

type Tag

type Tag interface {
	// TagName returns tag name.
	TagName() string

	// TagKind returns the [TagKind] holding the information about the type of
	// the tag value. Use it interprets the value returned by the [Tag.TagValue]
	// method.
	TagKind() TagKind

	// TagValue returns tag value.
	// You may use the value returned by the [Tag.TagKind] method
	// to cast it to the proper type.
	TagValue() any
}

Tag is an interface representing a tag.

Tags are named and typed values that can be used to annotate objects.

func CreateTag added in v0.2.0

func CreateTag(name string, val any) (Tag, error)

CreateTag creates a new Tag for the given name and value. The value's type must be first registered with RegisterSpec.

type TagAllGetter

type TagAllGetter interface {
	// TagGetAll returns all tags in the set as a map. May return nil. The
	// returned map should be treated as read-only.
	TagGetAll() map[string]Tag
}

TagAllGetter is an interface for retrieving all tags in the set.

type TagComparer

type TagComparer interface {
	// TagSame returns true if both tags have the same name, kind and value.
	TagSame(other Tag) bool
}

TagComparer is an interface for comparing tags.

type TagCreateFunc added in v0.2.0

type TagCreateFunc func(name string, val any, opts ...Option) (Tag, error)

TagCreateFunc function signature for creating Tag instances.

func CreateFunc added in v0.2.0

func CreateFunc[T Tag](fn func(name string, val any, opts ...Option) (T, error)) TagCreateFunc

CreateFunc creates a TagCreateFunc from a function that creates concrete tag instances.

Examples:

CreateFunc(CreateBool)
CreateFunc(CreateInt64)

func CreatorForKind added in v0.2.0

func CreatorForKind(knd TagKind) (TagCreateFunc, error)

CreatorForKind returns the TagCreateFunc for the given TagKind.

func CreatorForType added in v0.2.0

func CreatorForType(typ any) (TagCreateFunc, error)

CreatorForType returns the TagCreateFunc for the given type.

type TagCreator added in v0.2.0

type TagCreator interface {
	// TagCreate creates the appropriate [Tag] instance based on the value's
	// type. It returns the [ErrNoCreator] error if the value's type is not
	// supported. The name must be set by the implementer.
	TagCreate(value any, opts ...Option) (Tag, error)
}

TagCreator is an interface for creating Tag instances.

type TagKind

type TagKind uint16

TagKind describes the type of Tag value.

const (
	KindString  TagKind = 0b00000000_00000010
	KindInt64   TagKind = 0b00000000_00000100
	KindFloat64 TagKind = 0b00000000_00001000
	KindTime    TagKind = 0b00000000_00010000
	KindJSON    TagKind = 0b00000000_00100000
)

Base Tag kinds.

const KindSlice TagKind = 0b01000000_00000000

KindSlice is a TagKind type modifier indicating it is a slice.

func (TagKind) String added in v0.2.0

func (tk TagKind) String() string

String implements fmt.Stringer.

nolint: cyclop

type TagNamedCreator added in v0.2.0

type TagNamedCreator interface {
	// TagCreate creates the appropriate [Tag] instance based on the value's
	// type. It returns the [ErrNoCreator] error if the value's type is not
	// supported.
	TagCreate(name string, value any, opts ...Option) (Tag, error)
}

TagNamedCreator is an interface for creating named Tag instances.

type TagNamedParser added in v0.2.0

type TagNamedParser interface {
	TagParse(val string, opts ...Option) (Tag, error)
}

TagNamedParser is an interface for creating named Tag instances from their string representation. The name must be set by the implementer.

type TagParseFunc added in v0.2.0

type TagParseFunc func(name, val string, opts ...Option) (Tag, error)

TagParseFunc function signature for creating Tag instances from their string representation.

func ParseFunc added in v0.2.0

func ParseFunc[T Tag](fn func(name string, val string, opts ...Option) (T, error)) TagParseFunc

ParseFunc creates a TagParseFunc from a function that creates concrete tag instances from their string representation.

Examples:

ParseFunc(ParseBool)
ParseFunc(ParseInt64)

type TagParser added in v0.2.0

type TagParser interface {
	TagParse(name string, val string, opts ...Option) (Tag, error)
}

TagParser is an interface for creating Tag instances from their string representation.

type TagSet

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

TagSet represents a set of tags.

Example
set := NewTagSet()

set.TagSet(NewInt("A", 42), NewBool("B", true), NewString("C", "foo"))

fmt.Printf("There are %d tags in the set:\n", set.TagCount())
fmt.Printf("- A: %v\n", set.TagGet("A").TagValue())
fmt.Printf("- B: %v\n", set.TagGet("B").TagValue())
fmt.Printf("- C: %v\n", set.TagGet("C").TagValue())

fmt.Printf("\nGetting typed tags:\n")

// Tag exists but is of a different type.
tagA, err := set.TagGetBool("A")
fmt.Printf("  A: %v; err: %v\n", tagA, err)

tagC, err := set.TagGetString("C")
fmt.Printf("  C: %v;   err: %v\n", tagC, err)
Output:

There are 3 tags in the set:
- A: 42
- B: true
- C: foo

Getting typed tags:
  A: <nil>; err: A: invalid element type
  C: foo;   err: <nil>

func NewTagSet

func NewTagSet(opts ...Option) TagSet

NewTagSet returns a new instance of TagSet.

func (TagSet) MetaGetAll

func (set TagSet) MetaGetAll() map[string]any

func (TagSet) TagCount

func (set TagSet) TagCount() int

TagCount returns the number of entries in the tag set.

func (TagSet) TagDelete

func (set TagSet) TagDelete(name string)

func (TagSet) TagDeleteAll

func (set TagSet) TagDeleteAll()

TagDeleteAll deletes all tags from the set.

func (TagSet) TagGet

func (set TagSet) TagGet(name string) Tag

func (TagSet) TagGetAll

func (set TagSet) TagGetAll() map[string]Tag

func (TagSet) TagGetBool

func (set TagSet) TagGetBool(name string) (*Bool, error)

TagGetBool gets a tag by name as a Bool. Returns the tag and nil error if it exists and is the KindBool kind. Returns nil and ErrMissing if the tag is missing, or nil and ErrInvType if the tag is of a different kind.

func (TagSet) TagGetBoolSlice

func (set TagSet) TagGetBoolSlice(name string) (*BoolSlice, error)

TagGetBoolSlice gets a tag by name as a BoolSlice. Returns the tag and nil error if it exists and is the KindBoolSlice kind. Returns nil and ErrMissing if the tag is missing, or nil and ErrInvType if the tag is of a different kind.

func (TagSet) TagGetFloat64

func (set TagSet) TagGetFloat64(name string) (*Float64, error)

TagGetFloat64 gets a tag by name as a Float64. Returns the tag and nil error if it exists and is the KindFloat64 kind. Returns nil and ErrMissing if the tag is missing, or nil and ErrInvType if the tag is of a different kind.

func (TagSet) TagGetFloat64Slice

func (set TagSet) TagGetFloat64Slice(name string) (*Float64Slice, error)

TagGetFloat64Slice gets a tag by name as a Float64Slice. Returns the tag and nil error if it exists and is the KindFloat64Slice kind. Returns nil and ErrMissing if the tag is missing, or nil and ErrInvType if the tag is of a different kind.

func (TagSet) TagGetInt

func (set TagSet) TagGetInt(name string) (*Int, error)

TagGetInt gets a tag by name as a Int. Returns the tag and nil error if it exists and is the KindInt kind. Returns nil and ErrMissing if the tag is missing, or nil and ErrInvType if the tag is of a different kind.

func (TagSet) TagGetInt64

func (set TagSet) TagGetInt64(name string) (*Int64, error)

TagGetInt64 gets a tag by name as a Int64. Returns the tag and nil error if it exists and is the KindInt64 kind. Returns nil and ErrMissing if the tag is missing, or nil and ErrInvType if the tag is of a different kind.

func (TagSet) TagGetInt64Slice

func (set TagSet) TagGetInt64Slice(name string) (*Int64Slice, error)

TagGetInt64Slice gets a tag by name as a Int64Slice. Returns the tag and nil error if it exists and is the KindInt64Slice kind. Returns nil and ErrMissing if the tag is missing, or nil and ErrInvType if the tag is of a different kind.

func (TagSet) TagGetIntSlice

func (set TagSet) TagGetIntSlice(name string) (*IntSlice, error)

TagGetIntSlice gets a tag by name as a IntSlice. Returns the tag and nil error if it exists and is the KindIntSlice kind. Returns nil and ErrMissing if the tag is missing, or nil and ErrInvType if the tag is of a different kind.

func (TagSet) TagGetJSON

func (set TagSet) TagGetJSON(name string) (*JSON, error)

TagGetJSON gets a tag by name as a JSON. Returns the tag and nil error if it exists and is the [KindUUID] kind. Returns nil and ErrMissing if the tag is missing, or nil and ErrInvType if the tag is of a different kind.

func (TagSet) TagGetString

func (set TagSet) TagGetString(name string) (*String, error)

TagGetString gets a tag by name as a String. Returns the tag and nil error if it exists and is the KindString kind. Returns nil and ErrMissing if the tag is missing, or nil and ErrInvType if the tag is of a different kind.

func (TagSet) TagGetStringSlice

func (set TagSet) TagGetStringSlice(name string) (*StringSlice, error)

TagGetStringSlice gets a tag by name as a StringSlice. Returns the tag and nil error if it exists and is the KindStringSlice kind. Returns nil and ErrMissing if the tag is missing, or nil and ErrInvType if the tag is of a different kind.

func (TagSet) TagGetTime

func (set TagSet) TagGetTime(name string) (*Time, error)

TagGetTime gets a tag by name as a Time. Returns the tag and nil error if it exists and is the KindTime kind. Returns nil and ErrMissing if the tag is missing, or nil and ErrInvType if the tag is of a different kind.

func (TagSet) TagGetTimeSlice

func (set TagSet) TagGetTimeSlice(name string) (*TimeSlice, error)

TagGetTimeSlice gets a tag by name as a TimeSlice. Returns the tag and nil error if it exists and is the KindTimeSlice kind. Returns nil and ErrMissing if the tag is missing, or nil and ErrInvType if the tag is of a different kind.

func (TagSet) TagSet

func (set TagSet) TagSet(tags ...Tag)

type TagType

type TagType interface {
	byte | string | int64 | float64 | time.Time |
		int | bool | time.Duration
}

TagType represents supported tag types.

type TagValueComparer

type TagValueComparer interface {
	// TagEqual returns true if both tags are having the same kind and value.
	TagEqual(other Tag) bool
}

TagValueComparer is an interface for comparing tag values.

type Tagger

type Tagger interface {
	// TagGet retrieves from the set a [Tag] by its name. If the name doesn't
	// exist in the set, it returns nil.
	TagGet(name string) Tag

	// TagSet adds instances of [Tag] to the set. If the tag name already
	// exists in the set, it will be overwritten. The nil instances are ignored.
	TagSet(tag ...Tag)

	// TagDelete removes from the set the [Tag] by name. If the name does not
	// exist, the method has no effect.
	TagDelete(name string)
}

Tagger is an interface for managing a collection of distinctly named Tag instances (set). The implementations must not allow for nil values to be stored in the set.

type Time

type Time = single[time.Time]

Time is a tag for a single time.Time value.

func CreateTime added in v0.2.0

func CreateTime(name string, val any, opts ...Option) (*Time, error)

CreateTime casts the value to time.Time, or when the value is a string, it parses it as time.RFC3339Nano time. Returns the Time instance with the given name and nil error on success. Returns nil and error if the value's type is not a supported type or the value is not a valid time representation.

func NewTime

func NewTime(name string, v time.Time) *Time

NewTime returns a new instance of Time.

func ParseTime

func ParseTime(name, v string, opts ...Option) (*Time, error)

ParseTime parses string representation of the time tag. The time is always returned as UTC.

type TimeSlice

type TimeSlice = slice[time.Time]

TimeSlice is a tag for a slice of time.Time values.

func CreateTimeSlice added in v0.2.0

func CreateTimeSlice(name string, val any, _ ...Option) (*TimeSlice, error)

CreateTimeSlice casts the value to []time.Time, or when the value is a []string, it parses its elements as time.RFC3339Nano time. Returns the [TImeSlice] instance with the given name and nil error on success. Returns nil and error if the value's type is not a supported type or the value is not a valid time representation.

func NewTimeSlice

func NewTimeSlice(name string, v []time.Time) *TimeSlice

NewTimeSlice returns a new instance of TimeSlice.

Jump to

Keyboard shortcuts

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