structs

package
v0.0.0-...-d161ea0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTagName = "json"

Functions

func FillMap

func FillMap(s interface{}, out map[string]interface{})

FillMap is the same as Map. Instead of returning the output, it fills the given map.

func HasZero

func HasZero(s interface{}) bool

HasZero returns true if any field is equal to a zero value. For more info refer to Struct types HasZero() method. It panics if s's kind is not structs.

func IsStruct

func IsStruct(s interface{}) bool

IsStruct returns true if the given variable is a structs or a pointer to structs.

func IsZero

func IsZero(s interface{}) bool

IsZero returns true if all fields is equal to a zero value. For more info refer to Struct types IsZero() method. It panics if s's kind is not structs.

func Map

func Map(s interface{}) map[string]interface{}

Map converts the given structs to a map[string]interface{}. For more info refer to Struct types Map() method. It panics if s's kind is not structs.

func Name

func Name(s interface{}) string

Name returns the structs's type name within its package. It returns an empty string for unnamed types. It panics if s's kind is not structs.

func Names

func Names(s interface{}) []string

Names returns a slice of field names. For more info refer to Struct types Names() method. It panics if s's kind is not structs.

func String

func String(s interface{}) string

func UrlValues

func UrlValues(s interface{}) url.Values

func Values

func Values(s interface{}) []interface{}

Values converts the given structs to a []interface{}. For more info refer to Struct types Values() method. It panics if s's kind is not structs.

Types

type Field

type Field struct {
	// contains filtered or unexported fields
}

Field represents a single structs field that encapsulates high level functions around the field.

func Fields

func Fields(s interface{}) []*Field

Fields returns a slice of *Field. For more info refer to Struct types Fields() method. It panics if s's kind is not structs.

func (*Field) Field

func (f *Field) Field(name string) *Field

Field returns the field from a nested structs. It panics if the nested structs is not exported or if the field was not found.

func (*Field) FieldOk

func (f *Field) FieldOk(name string) (*Field, bool)

FieldOk returns the field from a nested structs. The boolean returns whether the field was found (true) or not (false).

func (*Field) Fields

func (f *Field) Fields() []*Field

Fields returns a slice of Fields. This is particular handy to get the fields of a nested structs . A structs tag with the content of "-" ignores the checking of that particular field. Example:

// Field is ignored by this package.
Field *http.Request `structs:"-"`

It panics if field is not exported or if field's kind is not structs

func (*Field) IsEmbedded

func (f *Field) IsEmbedded() bool

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

func (*Field) IsExported

func (f *Field) IsExported() bool

IsExported returns true if the given field is exported.

func (*Field) IsZero

func (f *Field) IsZero() bool

IsZero returns true if the given field is not initialized (has a zero value). It panics if the field is not exported.

func (*Field) Kind

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

Kind returns the fields kind, such as "string", "map", "bool", etc ..

func (*Field) Name

func (f *Field) Name() string

Name returns the name of the given field

func (*Field) Set

func (f *Field) Set(val interface{}) error

Set sets the field to given value v. It returns an error if the field is not settable (not addressable or not exported) or if the given value's type doesn't match the fields type.

func (*Field) Tag

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) Value

func (f *Field) Value() interface{}

Value returns the underlying value of the field. It panics if the field is not exported.

func (*Field) Zero

func (f *Field) Zero() error

Zero sets the field to its zero value. It returns an error if the field is not settable (not addressable or not exported).

type Struct

type Struct struct {
	TagName string
	// contains filtered or unexported fields
}

func New

func New(s interface{}, TagName string) *Struct

func (*Struct) Fields

func (s *Struct) Fields() []*Field

Fields returns a slice of Fields. A structs tag with the content of "-" ignores the checking of that particular field. Example:

// Field is ignored by this package.
Field bool `structs:"-"`

It panics if s's kind is not structs.

func (*Struct) FillMap

func (s *Struct) FillMap(out map[string]interface{})

FillMap is the same as Map. Instead of returning the output, it fills the given map.

func (*Struct) HasZero

func (s *Struct) HasZero() bool

HasZero returns true if a field in a structs is not initialized (zero value). A structs tag with the content of "-" ignores the checking of that particular field. Example:

// Field is ignored by this package.
Field bool `structs:"-"`

A value with the option of "omitnested" stops iterating further if the type is a structs. Example:

// Field is not processed further by this package.
Field time.Time     `structs:"myName,omitnested"`
Field *http.Request `structs:",omitnested"`

Note that only exported fields of a structs can be accessed, non exported fields will be neglected. It panics if s's kind is not structs.

func (*Struct) IsZero

func (s *Struct) IsZero() bool

IsZero returns true if all fields in a structs is a zero value (not initialized) A structs tag with the content of "-" ignores the checking of that particular field. Example:

// Field is ignored by this package.
Field bool `structs:"-"`

A value with the option of "omitnested" stops iterating further if the type is a structs. Example:

// Field is not processed further by this package.
Field time.Time     `structs:"myName,omitnested"`
Field *http.Request `structs:",omitnested"`

Note that only exported fields of a structs can be accessed, non exported fields will be neglected. It panics if s's kind is not structs.

func (*Struct) Map

func (s *Struct) Map() map[string]interface{}

func (*Struct) Name

func (s *Struct) Name() string

Name returns the structs's type name within its package. For more info refer to Name() function.

func (*Struct) Names

func (s *Struct) Names() []string

Names returns a slice of field names. A structs tag with the content of "-" ignores the checking of that particular field. Example:

// Field is ignored by this package.
Field bool `structs:"-"`

It panics if s's kind is not structs.

func (*Struct) String

func (s *Struct) String() string

func (*Struct) UrlValues

func (s *Struct) UrlValues(ctz bool) url.Values

func (*Struct) Values

func (s *Struct) Values() []interface{}

Values converts the given s structs's field values to a []interface{}. A structs tag with the content of "-" ignores the that particular field. Example:

// Field is ignored by this package.
Field int `structs:"-"`

A value with the option of "omitnested" stops iterating further if the type is a structs. Example:

// Fields is not processed further by this package.
Field time.Time     `structs:",omitnested"`
Field *http.Request `structs:",omitnested"`

A tag value with the option of "omitempty" ignores that particular field and is not added to the values if the field value is empty. Example:

// Field is skipped if empty
Field string `structs:",omitempty"`

Note that only exported fields of a structs can be accessed, non exported fields will be neglected.

Jump to

Keyboard shortcuts

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