README
structtag 
structtag provides an easy way of parsing and manipulating struct tag fields. Please vendor the library as it might change in future versions.
Install
go get github.com/fatih/structtag
Example
package main
import (
"fmt"
"reflect"
"sort"
"github.com/fatih/structtag"
)
func main() {
type t struct {
t string `json:"foo,omitempty,string" xml:"foo"`
}
// get field tag
tag := reflect.TypeOf(t{}).Field(0).Tag
// ... and start using structtag by parsing the tag
tags, err := structtag.Parse(string(tag))
if err != nil {
panic(err)
}
// iterate over all tags
for _, t := range tags.Tags() {
fmt.Printf("tag: %+v\n", t)
}
// get a single tag
jsonTag, err := tags.Get("json")
if err != nil {
panic(err)
}
fmt.Println(jsonTag) // Output: json:"foo,omitempty,string"
fmt.Println(jsonTag.Key) // Output: json
fmt.Println(jsonTag.Name) // Output: foo
fmt.Println(jsonTag.Options) // Output: [omitempty string]
// change existing tag
jsonTag.Name = "foo_bar"
jsonTag.Options = nil
tags.Set(jsonTag)
// add new tag
tags.Set(&structtag.Tag{
Key: "hcl",
Name: "foo",
Options: []string{"squash"},
})
// print the tags
fmt.Println(tags) // Output: json:"foo_bar" xml:"foo" hcl:"foo,squash"
// sort tags according to keys
sort.Sort(tags)
fmt.Println(tags) // Output: hcl:"foo,squash" json:"foo_bar" xml:"foo"
}
Documentation
Index ¶
- type Tag
- type Tags
- func (t *Tags) AddOptions(key string, options ...string)
- func (t *Tags) Delete(keys ...string)
- func (t *Tags) DeleteOptions(key string, options ...string)
- func (t *Tags) Get(key string) (*Tag, error)
- func (t *Tags) Keys() []string
- func (t *Tags) Len() int
- func (t *Tags) Less(i int, j int) bool
- func (t *Tags) Set(tag *Tag) error
- func (t *Tags) String() string
- func (t *Tags) Swap(i int, j int)
- func (t *Tags) Tags() []*Tag
Constants ¶
Variables ¶
Functions ¶
Types ¶
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" Name string // Options is a part of the value. It contains a slice of tag options i.e: // `json:"foo,omitempty". Here options is: ["omitempty"] Options []string }
Tag defines a single struct's string literal tag
type Tags ¶
type Tags struct {
// contains filtered or unexported fields
}
Tags represent a set of tags from a single struct field
func (*Tags) AddOptions ¶
AddOptions adds the given option for the given key. If the option already exists it doesn't add it again.
func (*Tags) DeleteOptions ¶
DeleteOptions deletes the given options for the given key
func (*Tags) Get ¶
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) Keys ¶
Tags returns a slice of tags. The order is the original tag order unless it was changed.