Documentation ¶
Overview ¶
Package structtags parses struct tags based on the pattern used in the encoding packages of the go stdlib.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Tag ¶
type Tag struct { Value string Options TagOptions }
Tag contains a tag value and options.
func Parse ¶
Parse parses a string tag into a Tag struct. A tag defined as `myTag:"value,abc,def"` will be parsed as:
Tag { Value: "value", Options: ["abc", "def"], }
Example ¶
package main import ( "fmt" "reflect" "4d63.com/structtags" ) func main() { s := struct { A string `myTag:"a,omitempty,flatten"` B string `myTag:",omitempty"` C int }{} t := reflect.TypeOf(s) for i := 0; i < t.NumField(); i++ { f := t.Field(i) if tagStr, ok := f.Tag.Lookup("myTag"); ok { tag := structtags.Parse(tagStr) fmt.Printf("Tag: %#v\n", tag.Value) fmt.Println(" Omit Empty:", tag.Options.Contains("omitempty")) fmt.Println(" Flatten:", tag.Options.Contains("flatten")) } } }
Output: Tag: "a" Omit Empty: true Flatten: true Tag: "" Omit Empty: true Flatten: false
type TagOptions ¶
type TagOptions []string
TagOptions contains the options for a tag.
func (TagOptions) Contains ¶
func (o TagOptions) Contains(option string) bool
Contains checks if the option is contained in the TagOptions.
Click to show internal directories.
Click to hide internal directories.