Install:
go get github.com/muir/reflectutils
Reflectutils is simply a repository for functions useful for working with Golang's reflect package.
Here's the highlights:
func WalkStructElements(t reflect.Type, f func(reflect.StructField) bool)
Recursively walking a struct with reflect has a pitfalls:
- It isn't recurse with respect to embeded structs
- The
Index
field ofreflect.Structfield
of embedded structs is not relative to your starting point.
WalkStructElements() walks
embedded elements and it updates StructField.Index
so that it is
relative to the root struct that was passed in.
func MakeStringSetter(t reflect.Type, optArgs ...StringSetterArg) (func(target reflect.Value, value string) error, error)
MakeStringSetter()
returns a function that can be used to assing to reflect.Value
given a
string value. It can handle arrays and slices (splits strings on commas).
Use SplitTag() to break a struct tag into it's elements and then use Tag.Fill() to parse it into a struct.
For example:
type TagInfo struct {
Name string `pt:"0"` // positional, first argument
Train bool `pt:"train"` // boolean: true: "train", "train=true"; false: "!train", "train=false"
Count int `pt:"count"` // integer value will be parsed
}
st := reflect.StructTag(`foo:"bar,!train,count=9"`)
var tagInfo TagInfo
err := GetTag(st, "foo").Fill(&tagInfo)
// tagInfo.Name will be "bar"
// tagInfo.Train will be false
// tagInfo.Count will be 9
The TypeName()
function exists to disambiguate between type names that are
versioned. reflect.Type.String()
will hides package versions. This doesn't
matter unless you've, unfortunately, imported multiple versions of the same
package.
The FillInDefaultValues()
function will look at for a struct tag named "default"
and use that value to fill in values where no value has been set.
Reflectutils is used by several packages. Backwards compatability is expected.