nomix

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

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")
)

Metadata parsing and casting errors.

Functions

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 NewBool

func NewBool(name string, v bool) *Bool

NewBool returns a new instance of Bool.

type BoolSlice

type BoolSlice = slice[bool]

BoolSlice is a tag for a slice of bool values.

func NewBoolSlice

func NewBoolSlice(name string, v ...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 NewByteSlice

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

NewByteSlice returns a new instance of ByteSlice.

type Float64

type Float64 = single[float64]

Float64 is a tag for a single float64 value.

func NewFloat64

func NewFloat64(name string, v 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 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 NewInt

func NewInt(name string, v int) *Int

NewInt returns a new instance of Int.

type Int64

type Int64 = single[int64]

Int64 is a tag for a single int64 value.

func NewInt64

func NewInt64(name string, v int64) *Int64

NewInt64 returns a new instance of Int64.

func ParseInt

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

ParseInt parses string representation of the integer tag.

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 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 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 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 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 ...func(*Options)) 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(key 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(key 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(key 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(key 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(key 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(key 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(key 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(key 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(key 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(key 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(key 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(key 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(key 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 String

type String = single[string]

String is a tag for a single byte value.

func NewString

func NewString(name, v string) *String

NewString returns a new instance of String.

func ParseString

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

ParseString creates string type tag. Never returns an error.

type StringSlice

type StringSlice = slice[string]

StringSlice is a tag for a slice of strings.

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 ParseBool

func ParseBool(name, v string, _ ...Option) (Tag, error)

ParseBool parses string representation of the boolean tag.

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 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.

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 ...func(*Options)) 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 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 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