Documentation ¶
Overview ¶
Package fields provides a view of the fields of a struct that follows the Go rules, amended to consider tags and case insensitivity.
Usage ¶
First define a function that interprets tags:
func parseTag(st reflect.StructTag) (name string, keep bool, other interface{}) { ... }
The function's return values describe whether to ignore the field completely or provide an alternate name, as well as other data from the parse that is stored to avoid re-parsing.
Next, construct a Cache, passing your function. As its name suggests, a Cache remembers field information for a type, so subsequent calls with the same type are very fast.
cache := fields.NewCache(parseTag)
To get the fields of a struct type as determined by the above rules, call the Fields method:
fields := cache.Fields(reflect.TypeOf(MyStruct{}))
The return value can be treated as a slice of Fields.
Given a string, such as a key or column name obtained during unmarshalling, call Match on the list of fields to find a field whose name is the best match:
field := fields.Match(name)
Match looks for an exact match first, then falls back to a case-insensitive comparison.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
A Cache records information about the fields of struct types.
A Cache is safe for use by multiple goroutines.
func NewCache ¶
NewCache constructs a Cache. Its argument should be a function that accepts a struct tag and returns three values: an alternative name for the field extracted from the tag, a boolean saying whether to keep the field or ignore it, and additional data that is stored with the field information to avoid having to parse the tag again.
func (*Cache) Fields ¶
Fields returns all the exported fields of t, which must be a struct type. It follows the standard Go rules for embedded fields, modified by the presence of tags. The result is sorted lexicographically by index.
If not nil, the given parseTag function should extract and return a name from the struct tag. This name is used instead of the field's declared name.
These rules apply in the absence of tags: Anonymous struct fields are treated as if their inner exported fields were fields in the outer struct (embedding). The result includes all fields that aren't shadowed by fields at higher level of embedding. If more than one field with the same name exists at the same level of embedding, it is excluded. An anonymous field that is not of struct type is treated as having its type as its name.
Tags modify these rules as follows: A field's tag is used as its name. An anonymous struct field with a name given in its tag is treated as a field having that name, rather than an embedded struct (the struct's fields will not be returned). If more than one field with the same name exists at the same level of embedding, but exactly one of them is tagged, then the tagged field is reported and the others are ignored.
type Field ¶
type Field struct { Name string // effective field name NameFromTag bool // did Name come from a tag? Type reflect.Type // field type Index []int // index sequence, for reflect.Value.FieldByIndex ParsedTag interface{} // third return value of the parseTag function // contains filtered or unexported fields }
A Field records information about a struct field.
type List ¶
type List []Field
A List is a list of Fields.
func (List) Match ¶
Match returns the field in the list whose name best matches the supplied name, nor nil if no field does. If there is a field with the exact name, it is returned. Otherwise the first field (sorted by index) whose name matches case-insensitively is returned.
func (List) MatchBytes ¶
MatchBytes is identical to Match, except that the argument is a byte slice.