Documentation
¶
Index ¶
- Constants
- Variables
- func WithBaseHEX(opts *Options)
- func WithLocString(opts *Options)
- type Bool
- type BoolSlice
- type ByteSlice
- type Float64
- type Float64Slice
- type Int
- type Int64
- type Int64Slice
- type IntSlice
- type JSON
- type MetaAllGetter
- type MetaAllSetter
- type MetaAppender
- type MetaFromGetter
- type MetaSet
- func (set MetaSet) MetaCount() int
- func (set MetaSet) MetaDelete(key string)
- func (set MetaSet) MetaDeleteAll()
- func (set MetaSet) MetaGet(key string) any
- func (set MetaSet) MetaGetAll() map[string]any
- func (set MetaSet) MetaGetBool(key string) (bool, error)
- func (set MetaSet) MetaGetBoolSlice(key string) ([]bool, error)
- func (set MetaSet) MetaGetFloat64(key string) (float64, error)
- func (set MetaSet) MetaGetFloat64Slice(key string) ([]float64, error)
- func (set MetaSet) MetaGetInt(key string) (int, error)
- func (set MetaSet) MetaGetInt64(key string) (int64, error)
- func (set MetaSet) MetaGetInt64Slice(key string) ([]int64, error)
- func (set MetaSet) MetaGetIntSlice(key string) ([]int, error)
- func (set MetaSet) MetaGetJOSN(key string) (json.RawMessage, error)
- func (set MetaSet) MetaGetString(key string) (string, error)
- func (set MetaSet) MetaGetStringSlice(key string) ([]string, error)
- func (set MetaSet) MetaGetTime(key string, opts ...Option) (time.Time, error)
- func (set MetaSet) MetaGetTimeSlice(key string, opts ...Option) ([]time.Time, error)
- func (set MetaSet) MetaSet(key string, value any)
- type Metadata
- type Option
- type Options
- type String
- type StringSlice
- type Tag
- type TagAllGetter
- type TagComparer
- type TagKind
- type TagSet
- func (set TagSet) MetaGetAll() map[string]any
- func (set TagSet) TagCount() int
- func (set TagSet) TagDelete(name string)
- func (set TagSet) TagDeleteAll()
- func (set TagSet) TagGet(name string) Tag
- func (set TagSet) TagGetAll() map[string]Tag
- func (set TagSet) TagGetBool(name string) (*Bool, error)
- func (set TagSet) TagGetBoolSlice(name string) (*BoolSlice, error)
- func (set TagSet) TagGetFloat64(name string) (*Float64, error)
- func (set TagSet) TagGetFloat64Slice(name string) (*Float64Slice, error)
- func (set TagSet) TagGetInt(name string) (*Int, error)
- func (set TagSet) TagGetInt64(name string) (*Int64, error)
- func (set TagSet) TagGetInt64Slice(name string) (*Int64Slice, error)
- func (set TagSet) TagGetIntSlice(name string) (*IntSlice, error)
- func (set TagSet) TagGetJSON(name string) (*JSON, error)
- func (set TagSet) TagGetString(name string) (*String, error)
- func (set TagSet) TagGetStringSlice(name string) (*StringSlice, error)
- func (set TagSet) TagGetTime(name string) (*Time, error)
- func (set TagSet) TagGetTimeSlice(name string) (*TimeSlice, error)
- func (set TagSet) TagSet(tags ...Tag)
- type TagType
- type TagValueComparer
- type Tagger
- type Time
- type TimeSlice
Examples ¶
Constants ¶
const ( KindBool = 0b00000001_00000000 | KindInt64 KindInt = 0b00000010_00000000 | KindInt64 )
Derived Tag kinds. Derived kinds are types that are derived from base kinds.
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 ¶
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 Float64 ¶
type Float64 = single[float64]
Float64 is a tag for a single float64 value.
func NewFloat64 ¶
NewFloat64 returns a new instance of Float64.
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 Int64 ¶
type Int64 = single[int64]
Int64 is a tag for a single int64 value.
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 JSON ¶
type JSON = slice[byte]
JSON is a tag for a json.RawMessage value.
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 ¶
NewMetaSet returns a new MetaSet instance. By default, the new map is initialized with the length equal to 10.
func (MetaSet) MetaDelete ¶
func (MetaSet) MetaDeleteAll ¶
func (set MetaSet) MetaDeleteAll()
MetaDeleteAll deletes all metadata from the set.
func (MetaSet) MetaGetAll ¶
func (MetaSet) MetaGetBool ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
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 WithMeta ¶
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 ¶
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 ¶
WithTimeFormat is the MetaSet option setting string time format.
func WithTimeLoc ¶
WithTimeLoc is the MetaSet option setting location for parsed time strings.
func WithZeroTime ¶
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.
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.
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.
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 (TagSet) MetaGetAll ¶
func (TagSet) TagDeleteAll ¶
func (set TagSet) TagDeleteAll()
TagDeleteAll deletes all tags from the set.
func (TagSet) TagGetBool ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
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.