Documentation ¶
Overview ¶
Package gstructs provides functions for struct information retrieving.
Index ¶
- Constants
- func FieldMap(in FieldMapInput) (map[string]Field, error)
- func ParseTag(tag string) map[string]string
- func TagMapField(object interface{}, priority []string) (map[string]Field, error)
- func TagMapName(pointer interface{}, priority []string) (map[string]string, error)
- type Field
- func (f *Field) IsEmbedded() bool
- func (f *Field) IsExported() bool
- func (f *Field) Kind() reflect.Kind
- func (f *Field) Name() string
- func (f *Field) OriginalKind() reflect.Kind
- func (f *Field) Tag(key string) string
- func (f *Field) TagJsonName() string
- func (f *Field) TagLookup(key string) (value string, ok bool)
- func (f *Field) TagMap() map[string]string
- func (f *Field) TagStr() string
- func (f *Field) Type() Type
- type FieldMapInput
- type FieldsInput
- type Type
Constants ¶
const ( RecursiveOptionNone = 0 // No recursively retrieving fields as map if the field is an embedded struct. RecursiveOptionEmbedded = 1 // Recursively retrieving fields as map if the field is an embedded struct. RecursiveOptionEmbeddedNoTag = 2 // Recursively retrieving fields as map if the field is an embedded struct and the field has no tag. )
Variables ¶
This section is empty.
Functions ¶
func FieldMap ¶
func FieldMap(in FieldMapInput) (map[string]Field, error)
FieldMap retrieves and returns struct field as map[name/tag]Field from `pointer`.
The parameter `pointer` should be type of struct/*struct.
The parameter `priority` specifies the priority tag array for retrieving from high to low. If it's given `nil`, it returns map[name]Field, of which the `name` is attribute name.
The parameter `recursive` specifies the whether retrieving the fields recursively if the attribute is an embedded struct.
Note that it only retrieves the exported attributes with first letter up-case from struct.
func ParseTag ¶
ParseTag parses tag string into map. For example: ParseTag(`v:"required" p:"id" d:"1"`) => map[v:required p:id d:1].
func TagMapField ¶
TagMapField retrieves struct tags as map[tag]Field from `pointer`, and returns it. The parameter `object` should be either type of struct/*struct/[]struct/[]*struct.
Note that, 1. It only retrieves the exported attributes with first letter up-case from struct. 2. The parameter `priority` should be given, it only retrieves fields that has given tag.
func TagMapName ¶
TagMapName retrieves and returns struct tags as map[tag]attribute from `pointer`.
The parameter `pointer` should be type of struct/*struct.
Note that, 1. It only retrieves the exported attributes with first letter up-case from struct. 2. The parameter `priority` should be given, it only retrieves fields that has given tag.
Types ¶
type Field ¶
type Field struct { Value reflect.Value // The underlying value of the field. Field reflect.StructField // The underlying field of the field. // Retrieved tag name. It depends TagValue. TagName string // Retrieved tag value. // There might be more than one tags in the field, but only one can be retrieved according to calling function rules. TagValue string }
Field contains information of a struct field .
func Fields ¶
func Fields(in FieldsInput) ([]Field, error)
Fields retrieves and returns the fields of `pointer` as slice.
func TagFields ¶
TagFields retrieves and returns struct tags as []Field from `pointer`.
The parameter `pointer` should be type of struct/*struct.
Note that, 1. It only retrieves the exported attributes with first letter up-case from struct. 2. The parameter `priority` should be given, it only retrieves fields that has given tag.
func (*Field) IsEmbedded ¶
IsEmbedded returns true if the given field is an anonymous field (embedded)
func (*Field) IsExported ¶
IsExported returns true if the given field is exported.
func (*Field) OriginalKind ¶
OriginalKind retrieves and returns the original reflect.Kind for Value of Field `f`.
func (*Field) Tag ¶
Tag returns the value associated with key in the tag string. If there is no such key in the tag, Tag returns the empty string.
func (*Field) TagJsonName ¶
TagJsonName returns the `json` tag name string of the field.
func (*Field) TagLookup ¶
TagLookup returns the value associated with key in the tag string. 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 value was explicitly set in the tag string. If the tag does not have the conventional format, the value returned by Lookup is unspecified.
type FieldMapInput ¶
type FieldMapInput struct { // Pointer should be type of struct/*struct. Pointer interface{} // PriorityTagArray specifies the priority tag array for retrieving from high to low. // If it's given `nil`, it returns map[name]Field, of which the `name` is attribute name. PriorityTagArray []string // RecursiveOption specifies the way retrieving the fields recursively if the attribute // is an embedded struct. It is RecursiveOptionNone in default. RecursiveOption int }
FieldMapInput is the input parameter struct type for function FieldMap.
type FieldsInput ¶
type FieldsInput struct { // Pointer should be type of struct/*struct. Pointer interface{} // RecursiveOption specifies the way retrieving the fields recursively if the attribute // is an embedded struct. It is RecursiveOptionNone in default. RecursiveOption int }
FieldsInput is the input parameter struct type for function Fields.
type Type ¶
Type wraps reflect.Type for additional features.
func StructType ¶
StructType retrieves and returns the struct Type of specified struct/*struct. The parameter `object` should be either type of struct/*struct/[]struct/[]*struct.