structtag

package
v0.8.8 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2025 License: MIT, Apache-2.0 Imports: 8 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTagSyntax      = errors.New("bad syntax for struct tag pair")
	ErrTagKeySyntax   = errors.New("bad syntax for struct tag key")
	ErrTagValueSyntax = errors.New("bad syntax for struct tag value")

	ErrKeyNotSet      = errors.New("tag key not exist")
	ErrTagNotExist    = errors.New("tag not exist")
	ErrTagIgnore      = errors.New("tag ignore")
	ErrTagKeyMismatch = errors.New("mismatch between key and tag.key")
)

Functions

func CustomTagLookup added in v0.6.0

func CustomTagLookup(customTag, key string) (value string, ok bool)

func ParseSettingTagIntoStruct added in v0.6.0

func ParseSettingTagIntoStruct(tag string, sep byte, settings any) error

func ParseSettingTagToMap added in v0.6.0

func ParseSettingTagToMap(tag string, sep byte) map[string]string

ParseSettingTag 适用于子tag为多配置项

type example struct {
	db  string `specifyTagName:"config:db;default:postgres`
}

default sep ;

func ParseSettingTagToStruct added in v0.6.0

func ParseSettingTagToStruct[T any](tag string, sep byte) (*T, error)

Types

type CustomTag added in v0.6.0

type CustomTag string

CustomTag 适用于子tag形式等同于struct tag,区别是struct tag使用"而CustomTag使用'

func (CustomTag) Lookup added in v0.6.0

func (tag CustomTag) Lookup(key string) (value string, ok bool)

type SettingTag added in v0.6.0

type SettingTag string

type Tag

type Tag struct {
	// Key is the tag key, such as json, xml, etc..
	// i.e: `json:"foo,omitempty". Here key is: "json"
	Key string

	// Name is a part of the value
	// i.e: `json:"foo,omitempty". Here name is: "foo"
	Value string
}

Tag defines a single struct's string literal tag

func (*Tag) AddOptions added in v0.6.0

func (tag *Tag) AddOptions(options ...string)

AddOptions adds the given option for the given key. If the option already exists it doesn't add it again.

func (*Tag) DeleteOptions added in v0.6.0

func (tag *Tag) DeleteOptions(options ...string)

DeleteOptions deletes the given options for the given key

func (*Tag) GoString

func (t *Tag) GoString() string

GoString implements the fmt.GoStringer interface

func (*Tag) HasOption

func (t *Tag) HasOption(opt string) bool

HasOption returns true if the given option is available in options

func (*Tag) Name

func (t *Tag) Name() string

Value returns the raw value of the tag, i.e. if the tag is `json:"foo,omitempty", the Value is "foo,omitempty"

func (*Tag) Options

func (t *Tag) Options() []string

func (*Tag) String

func (t *Tag) String() string

String reassembles the tag into a valid tag field representation

type Tags

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

Tags represent a set of tags from a single struct field

func Parse

func Parse(tag string) (*Tags, error)

Parse parses a single struct field tag and returns the set of tags.

func (*Tags) Delete

func (t *Tags) Delete(keys ...string)

Delete deletes the tag for the given keys

func (*Tags) Get

func (t *Tags) Get(key string) (*Tag, error)

Get returns the tag associated with the given key. If the key is present in the tag the value (which may be empty) is returned. Otherwise the returned value will be the empty string. The ok return value reports whether the tag exists or not (which the return value is nil).

func (*Tags) Iter added in v0.6.0

func (t *Tags) Iter() iter.Seq[*Tag]

func (*Tags) Keys

func (t *Tags) Keys() []string

Tags returns a Slice of tags. The order is the original tag order unless it was changed.

func (*Tags) Len

func (t *Tags) Len() int

func (*Tags) Less

func (t *Tags) Less(i int, j int) bool

func (*Tags) MustGet added in v0.6.0

func (t *Tags) MustGet(key string) *Tag

func (*Tags) Set

func (t *Tags) Set(tag *Tag) error

Set sets the given tag. If the tag key already exists it'll override it

func (*Tags) String

func (t *Tags) String() string

String reassembles the tags into a valid literal tag field representation

func (*Tags) Swap

func (t *Tags) Swap(i int, j int)

func (*Tags) Tags

func (t *Tags) Tags() []*Tag

Tags returns a Slice of tags. The order is the original tag order unless it was changed.

Jump to

Keyboard shortcuts

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