structtag

package
v1.6.6 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 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 ParseSettingTagIntoStruct

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

func ParseSettingTagToMap

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

ParseSettingTagToMap parse setting tag, default sep ;

func ParseSettingTagToStruct

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

func SubTagLookup

func SubTagLookup(subTag, key string) (value string, ok bool)

Types

type SettingTag

type SettingTag string

SettingTag is a tag for setting

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

type SubTag

type SubTag string

SubTag 适用于子tag形式等同于struct tag,区别是struct tag使用 " 而SubTag使用 ' e.g. `key:"example:'1' type:'\\w' test:'\"'"`

func (SubTag) Lookup

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

type Tag

type Tag struct {
	Index int
	// 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
	// Options are the tag options, such as omitempty, required, etc..
	// i.e: `json:"foo,omitempty". Here options is: ["omitempty"]
	Options []string
}

Tag defines a single struct's string literal tag

func (*Tag) AddOptions

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

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) 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 MustParse added in v1.3.6

func MustParse(tag string) *Tags

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, bool)

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

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

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

func (*Tags) Set

func (t *Tags) Set(key, value string, options ...string)

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.

func (*Tags) TryGet added in v1.3.6

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

Jump to

Keyboard shortcuts

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