Documentation
¶
Index ¶
- func ParseToFatih(tag string, escapeComma bool) (*structtag.Tags, error)
- func ParseToMap(tag string) (map[string]string, error)
- func ParseToMapMultikeys(tag string) (map[string][]string, error)
- func ParseToMapValues(tag string, escapeComma bool) (map[string][]string, error)
- func ParseToSlice(tag string) ([]sliceraw.Tag, error)
- func ParseToSliceStructured(tag string, opt *structured.Options) (*structured.Tag, error)
- func ParseToSliceValues(tag string, escapeComma bool) ([]slicevalues.Tag, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseToFatih ¶
ParseToFatih parses a struct tag to a *structtag.Tags. The value is split on comma.
func ParseToMap ¶
ParseToMap parses a struct tag to a `map[string]string`.
Example ¶
package main
import (
"fmt"
"reflect"
"github.com/ldez/structtags"
)
func main() {
type MyStruct struct {
Field string `a:"1,2" b:"hello"`
}
// Gets the raw tag from the struct field.
rawTag := reflect.TypeOf(MyStruct{}).Field(0).Tag
data, err := structtags.ParseToMap(string(rawTag))
if err != nil {
panic(err)
}
fmt.Println(data)
}
Output: map[a:1,2 b:hello]
func ParseToMapMultikeys ¶
ParseToMapMultikeys parses a struct tag to a `map[string][]string`. For non-conventional tags where the key is repeated.
Example ¶
package main
import (
"fmt"
"reflect"
"github.com/ldez/structtags"
)
func main() {
type MyStruct struct {
Field string `a:"1,2" b:"hello" b:"world"`
}
// Gets the raw tag from the struct field.
rawTag := reflect.TypeOf(MyStruct{}).Field(0).Tag
data, err := structtags.ParseToMapMultikeys(string(rawTag))
if err != nil {
panic(err)
}
fmt.Println(data)
}
Output: map[a:[1,2] b:[hello world]]
func ParseToMapValues ¶
ParseToMapValues parses a struct tag to a `map[string][]string`. The value is split on comma.
Example ¶
package main
import (
"fmt"
"reflect"
"github.com/ldez/structtags"
)
func main() {
type MyStruct struct {
Field string `a:"1,2" b:"hello\\,world"`
}
// Gets the raw tag from the struct field.
rawTag := reflect.TypeOf(MyStruct{}).Field(0).Tag
data, err := structtags.ParseToMapValues(string(rawTag), false)
if err != nil {
panic(err)
}
fmt.Println(data)
}
Output: map[a:[1 2] b:[hello\ world]]
Example (Escaped_comma) ¶
package main
import (
"fmt"
"reflect"
"github.com/ldez/structtags"
)
func main() {
type MyStruct struct {
Field string `a:"1,2" b:"hello\\,world"`
}
// Gets the raw tag from the struct field.
rawTag := reflect.TypeOf(MyStruct{}).Field(0).Tag
data, err := structtags.ParseToMapValues(string(rawTag), true)
if err != nil {
panic(err)
}
fmt.Println(data)
}
Output: map[a:[1 2] b:[hello\,world]]
func ParseToSlice ¶
ParseToSlice parses a struct tag to a slice of parser.Tag.
Example ¶
package main
import (
"fmt"
"reflect"
"github.com/ldez/structtags"
)
func main() {
type MyStruct struct {
Field string `a:"1,2" b:"hello"`
}
// Gets the raw tag from the struct field.
rawTag := reflect.TypeOf(MyStruct{}).Field(0).Tag
data, err := structtags.ParseToSlice(string(rawTag))
if err != nil {
panic(err)
}
fmt.Println(data)
}
Output: [{a 1,2} {b hello}]
func ParseToSliceStructured ¶ added in v0.4.0
func ParseToSliceStructured(tag string, opt *structured.Options) (*structured.Tag, error)
ParseToSliceStructured parses a struct tag to a structured.Tag. Allows modifying the struct tags. The value is split on comma.
Example ¶
package main
import (
"fmt"
"reflect"
"github.com/ldez/structtags"
"github.com/ldez/structtags/structured"
)
func main() {
type MyStruct struct {
Field string `b:"hello" a:"1,2" c:"world"`
}
// Gets the raw tag from the struct field.
rawTag := reflect.TypeOf(MyStruct{}).Field(0).Tag
tag, err := structtags.ParseToSliceStructured(string(rawTag), nil)
if err != nil {
panic(err)
}
// Iterates over all entries from the struct tag.
for entry := range tag.Seq() {
fmt.Printf("entry: %s\n", entry)
}
// get a single tag
entryA := tag.Get("a")
if entryA == nil {
panic("no entry with key a")
}
fmt.Println("key `a`, entry:", entryA)
fmt.Println("key `a`, entry key:", entryA.Key)
fmt.Println("key `a`, entry value:", entryA.RawValue)
// change existing tag
values, err := entryA.Values()
if err != nil {
panic(err)
}
values = append(values, "test")
entryA.RawValue = values.String()
// Adds a new entry to the struct tag.
err = tag.Add(&structured.Entry{
Key: "e",
RawValue: "foo,bar",
})
if err != nil {
panic(err)
}
fmt.Println("tag:", tag)
// Sorts the entries.
tag.Sort()
fmt.Println("sorted tag:", tag)
}
Output: entry: b="hello" entry: a="1,2" entry: c="world" key `a`, entry: a="1,2" key `a`, entry key: a key `a`, entry value: 1,2 tag: b="hello" a="1,2,test" c="world" e="foo,bar" sorted tag: a="1,2,test" b="hello" c="world" e="foo,bar"
func ParseToSliceValues ¶
func ParseToSliceValues(tag string, escapeComma bool) ([]slicevalues.Tag, error)
ParseToSliceValues parses a struct tag to a slice of slicevalues.Tag. The value is split on comma.
Example ¶
package main
import (
"fmt"
"reflect"
"github.com/ldez/structtags"
)
func main() {
type MyStruct struct {
Field string `a:"1,2" b:"hello\\,world"`
}
// Gets the raw tag from the struct field.
rawTag := reflect.TypeOf(MyStruct{}).Field(0).Tag
data, err := structtags.ParseToSliceValues(string(rawTag), false)
if err != nil {
panic(err)
}
fmt.Println(data)
}
Output: [{a [1 2]} {b [hello\ world]}]
Example (Escaped_comma) ¶
package main
import (
"fmt"
"reflect"
"github.com/ldez/structtags"
)
func main() {
type MyStruct struct {
Field string `a:"1,2" b:"hello\\,world"`
}
// Gets the raw tag from the struct field.
rawTag := reflect.TypeOf(MyStruct{}).Field(0).Tag
data, err := structtags.ParseToSliceValues(string(rawTag), true)
if err != nil {
panic(err)
}
fmt.Println(data)
}
Output: [{a [1 2]} {b [hello\,world]}]
Types ¶
This section is empty.