structs

package
v1.16.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package structs provides functions for struct information retrieving and struct conversion.

Inspired and improved from: https://github.com/fatih/structs

Index

Constants

View Source
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 added in v1.15.4

func FieldMap(input 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 added in v1.15.4

func ParseTag(tag string) map[string]string

ParseTag parses tag string into map.

func TagMapField

func TagMapField(object interface{}, priority []string) (map[string]*Field, error)

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 it only retrieves the exported attributes with first letter up-case from struct.

func TagMapName

func TagMapName(pointer interface{}, priority []string) (map[string]string, error)

TagMapName retrieves and returns struct tags as map[tag]attribute from `pointer`.

The parameter `pointer` should be type of struct/*struct.

Note that it only retrieves the exported attributes with first letter up-case from struct.

Types

type Field

type Field struct {
	Value    reflect.Value       // The underlying value of the field.
	Field    reflect.StructField // The underlying field of the field.
	TagValue 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.
}

Field contains information of a struct field .

func TagFields added in v1.10.1

func TagFields(pointer interface{}, priority []string) ([]*Field, error)

TagFields retrieves and returns struct tags as []*Field from `pointer`.

The parameter `pointer` should be type of struct/*struct.

Note that it only retrieves the exported attributes with first letter up-case from struct.

func (*Field) IsEmbedded added in v1.14.3

func (f *Field) IsEmbedded() bool

IsEmbedded returns true if the given field is an anonymous field (embedded)

func (*Field) IsExported added in v1.14.3

func (f *Field) IsExported() bool

IsExported returns true if the given field is exported.

func (*Field) Kind added in v1.15.5

func (f *Field) Kind() reflect.Kind

Kind returns the reflect.Kind for Value of Field `f`.

func (*Field) Name added in v1.14.3

func (f *Field) Name() string

Name returns the name of the given field

func (*Field) OriginalKind added in v1.15.5

func (f *Field) OriginalKind() reflect.Kind

OriginalKind retrieves and returns the original reflect.Kind for Value of Field `f`.

func (*Field) Tag added in v1.10.1

func (f *Field) Tag(key string) string

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) TagLookup added in v1.16.0

func (f *Field) TagLookup(key string) (value string, ok bool)

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.

func (*Field) TagStr added in v1.16.5

func (f *Field) TagStr() string

TagStr returns the tag string of the field.

func (*Field) Type added in v1.15.4

func (f *Field) Type() Type

Type returns the type of the given field

type FieldMapInput added in v1.16.5

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
}

type Type added in v1.15.4

type Type struct {
	reflect.Type
}

Type wraps reflect.Type for additional features.

func StructType added in v1.15.4

func StructType(object interface{}) (*Type, error)

StructType retrieves and returns the struct Type of specified struct/*struct. The parameter `object` should be either type of struct/*struct/[]struct/[]*struct.

func (Type) FieldKeys added in v1.15.4

func (t Type) FieldKeys() []string

FieldKeys returns the keys of current struct/map.

func (Type) Signature added in v1.15.4

func (t Type) Signature() string

Signature returns a unique string as this type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL