Documentation
¶
Overview ¶
Package tagops is aimed to provide a set of functions to work with struct tags.
Index ¶
- func Keys(m map[string]any) []string
- func MapValues(out *[]any, m map[string]any, order []string) (err error)
- func PrepareToMap(opts ...Option) func(any) map[string]any
- func Tags(a any, tag string) []string
- func ToMap(a any, tag string, omitempty bool, flatten bool) map[string]any
- func Values(a any, tag string) ([]any, error)
- type Mapper
- type Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MapValues ¶
MapValues populates slice out with values from map m in the key order specified by order. The size of out slice will be adjusted to order size to accomodate for all values.
func PrepareToMap ¶
PrepareToMap returns a ToMap function with options set by opts.
func Tags ¶
Tags returns a sorted list of names in tags, given a struct object. The empty fields are included and the map is flattened.
func ToMap ¶
ToMap converts an argument a which should be some struct type, to a map[tag]value. If omitempty is specified, fields having empty values and tag option "omitempty" are skipped. If flatten is true, all nested non-anonymous structs are flattened into the parent map.
Example (Anonymous) ¶
// Anonymous structures are always flattened.
type Person struct {
Name string `json:"name,omitempty"`
Age int `json:"age,omitempty"`
}
type Employee struct {
Person
Position string `json:"position,omitempty"`
}
p := Employee{
Person: Person{
Name: "Bob",
Age: 30,
},
Position: "Manager",
}
mp := ToMap(p, "json", true, false) // flatten is ignored
printjson(mp)
Output: { "age": 30, "name": "Bob", "position": "Manager" }
Example (Flatten) ¶
type Address struct {
Street string `json:"street,omitempty"`
City string `json:"city,omitempty"`
ZIP int `json:"zip"`
}
type Person struct {
Name string `json:"name,omitempty"`
Age int `json:"age,omitempty"`
Address Address `json:"address,omitempty"`
}
p := Person{
Name: "Alice",
Age: 26,
Address: Address{
Street: "123 Main St",
City: "Anytown",
ZIP: 0,
},
}
mp := ToMap(p, "json", true, true)
printjson(mp)
Output: { "age": 26, "city": "Anytown", "name": "Alice", "street": "123 Main St", "zip": 0 }
Example (Nested) ¶
type Address struct {
Street string `json:"street,omitempty"`
City string `json:"city,omitempty"`
ZIP int `json:"zip"`
}
type Person struct {
Name string `json:"name,omitempty"`
Age int `json:"age,omitempty"`
Address Address `json:"address,omitempty"`
}
p := Person{
Name: "Alice",
Age: 26,
Address: Address{
Street: "123 Main St",
City: "Anytown",
ZIP: 0,
},
}
mp := ToMap(p, "json", true, false)
printjson(mp)
Output: { "address": { "city": "Anytown", "street": "123 Main St", "zip": 0 }, "age": 26, "name": "Alice" }
Types ¶
type Mapper ¶
type Mapper struct {
// Tag is the tag name.
Tag string
// Omitempty omits empty fields.
Omitempty bool
// Flatten flattens named nested structs (anonymous structs are always
// flattened).
Flatten bool
}
Mapper is a struct to map struct fields to map key/values. The struct fields are mapped to map keys using the Tag. No tag value leads to undefined behavior. The Mapper can be configured with options.