Documentation
¶
Index ¶
- Constants
- Variables
- func RegisterSpec(typ any, spec KindSpec)
- func WithBaseHEX(opts *Options)
- func WithLocString(opts *Options)
- type Bool
- type BoolSlice
- type ByteSlice
- type Definition
- type Float64
- type Float64Slice
- type Int
- type Int64
- type Int64Slice
- type IntSlice
- type JSON
- type KindSpec
- func BoolSliceSpec() KindSpec
- func BoolSpec() KindSpec
- func ByteSliceSpec() KindSpec
- func Float64SliceSpec() KindSpec
- func Float64Spec() KindSpec
- func GetSpec(typ any) KindSpec
- func Int64SliceSpec() KindSpec
- func Int64Spec() KindSpec
- func IntSliceSpec() KindSpec
- func IntSpec() KindSpec
- func JSONSpec() KindSpec
- func NewKindSpec(knd TagKind, tcr TagCreateFunc, tpr TagParseFunc) KindSpec
- func StringSliceSpec() KindSpec
- func StringSpec() KindSpec
- func TimeSliceSpec() KindSpec
- func TimeSpec() KindSpec
- 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(name string) (bool, error)
- func (set MetaSet) MetaGetBoolSlice(name string) ([]bool, error)
- func (set MetaSet) MetaGetFloat64(name string) (float64, error)
- func (set MetaSet) MetaGetFloat64Slice(name string) ([]float64, error)
- func (set MetaSet) MetaGetInt(name string) (int, error)
- func (set MetaSet) MetaGetInt64(name string) (int64, error)
- func (set MetaSet) MetaGetInt64Slice(name string) ([]int64, error)
- func (set MetaSet) MetaGetIntSlice(name string) ([]int, error)
- func (set MetaSet) MetaGetJOSN(name string) (json.RawMessage, error)
- func (set MetaSet) MetaGetString(name string) (string, error)
- func (set MetaSet) MetaGetStringSlice(name string) ([]string, error)
- func (set MetaSet) MetaGetTime(name string, opts ...Option) (time.Time, error)
- func (set MetaSet) MetaGetTimeSlice(name string, opts ...Option) ([]time.Time, error)
- func (set MetaSet) MetaSet(key string, value any)
- type Metadata
- type Option
- type Options
- type Registry
- func (reg Registry) Create(name string, val any, opts ...Option) (Tag, error)
- func (reg Registry) CreatorForKind(knd TagKind) (TagCreateFunc, error)
- func (reg Registry) CreatorForType(typ any) (TagCreateFunc, error)
- func (reg Registry) Register(typ any, spec KindSpec) KindSpec
- func (reg Registry) Spec(typ any) KindSpec
- type String
- type StringSlice
- type Tag
- type TagAllGetter
- type TagComparer
- type TagCreateFunc
- type TagCreator
- type TagKind
- type TagNamedCreator
- type TagNamedParser
- type TagParseFunc
- type TagParser
- 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") // 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
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
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.
type BoolSlice ¶
type BoolSlice = slice[bool]
BoolSlice is a tag for a slice of bool values.
func CreateBoolSlice ¶ added in v0.2.0
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.
type ByteSlice ¶
type ByteSlice = slice[byte]
ByteSlice is a tag for a slice of bytes.
func CreateByteSlice ¶ added in v0.2.0
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.
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 (td *Definition) TagCreate(val any, opts ...Option) (Tag, error)
func (*Definition) TagKind ¶ added in v0.2.0
func (td *Definition) TagKind() TagKind
TagKind returns the tag definition kind.
func (*Definition) TagName ¶ added in v0.2.0
func (td *Definition) TagName() string
TagName returns the tag definition name.
func (*Definition) TagParse ¶ added in v0.2.0
func (td *Definition) TagParse(val string, opts ...Option) (Tag, error)
func (*Definition) Validate ¶ added in v0.3.0
func (td *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
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 ¶
NewFloat64 returns a new instance of Float64.
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
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.
type Int64 ¶
type Int64 = single[int64]
Int64 is a tag for a single int64 value.
func CreateInt64 ¶ added in v0.2.0
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.
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
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.
type JSON ¶
type JSON = slice[byte]
JSON is a tag for a json.RawMessage value.
func CreateJSON ¶ added in v0.2.0
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.
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
func ByteSliceSpec ¶ added in v0.2.0
func ByteSliceSpec() KindSpec
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
func Int64SliceSpec ¶ added in v0.2.0
func Int64SliceSpec() KindSpec
Int64SliceSpec returns a KindSpec for Int64Slice type.
func IntSliceSpec ¶ added in v0.2.0
func IntSliceSpec() KindSpec
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
func TimeSliceSpec ¶ added in v0.2.0
func TimeSliceSpec() KindSpec
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(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 ¶
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 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
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.
type String ¶
type String = single[string]
String is a tag for a single byte value.
func CreateString ¶ added in v0.2.0
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.
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.
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
TagCreateFunc function signature for creating Tag instances.
func CreateFunc ¶ added in v0.2.0
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.
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
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
TagParseFunc function signature for creating Tag instances from their string representation.
func ParseFunc ¶ added in v0.2.0
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
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 (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.
type Time ¶
Time is a tag for a single time.Time value.
func CreateTime ¶ added in v0.2.0
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.
type TimeSlice ¶
TimeSlice is a tag for a slice of time.Time values.
func CreateTimeSlice ¶ added in v0.2.0
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.