structs

package
v2.3.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MIT Imports: 5 Imported by: 3

Documentation

Overview

Package structs provide several high level functions to manipulate struct, tag, and field.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToMap

func ToMap(v any) (map[string]any, error)

ToMap convert struct to map, only convert exported struct field map key is specified same as struct field tag `json` value.

Types

type Field

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

Field is abstract struct field for provide several high level functions

func (*Field) IsEmbedded

func (f *Field) IsEmbedded() bool

IsEmbedded returns true if the given field is an embedded field.

func (*Field) IsExported

func (f *Field) IsExported() bool

IsExported returns true if the given field is exported.

func (*Field) IsNil added in v2.2.9

func (f *Field) IsNil() bool

IsNil returns true if the given field is nil value.

func (*Field) IsSlice

func (f *Field) IsSlice() bool

IsSlice check if a struct field type is slice or not

func (*Field) IsTargetType added in v2.2.9

func (f *Field) IsTargetType(targetType reflect.Kind) bool

IsTargetType check if a struct field type is target type or not

func (*Field) IsZero

func (f *Field) IsZero() bool

IsZero returns true if the given field is zero value.

func (*Field) Kind

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

Kind returns the field's kind

func (*Field) Name

func (f *Field) Name() string

Name returns the name of the given field

func (*Field) Tag

func (f *Field) Tag() *Tag

Tag returns the value that the key in the tag string.

func (*Field) Value

func (f *Field) Value() any

Value returns the underlying value of the field.

type Struct

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

Struct is abstract struct for provide several high level functions

func New

func New(value any, tagName ...string) *Struct

New returns a new *Struct

func (*Struct) Field

func (s *Struct) Field(name string) (*Field, bool)

Field returns a Field if the given field name was found

func (*Struct) Fields

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

Fields returns all the struct fields within a slice

func (*Struct) IsStruct

func (s *Struct) IsStruct() bool

IsStruct returns true if the given rvalue is a struct

func (*Struct) ToMap

func (s *Struct) ToMap() (map[string]any, error)

ToMap converts the given struct to a map[string]any, where the keys of the keys are the field names and the values of the map are the values of the fields. The default map key is the struct field name, but you can change it. The `json` key is the default tag key. Example:

	// default
	Name string `json:"name"`

	// ignore the field
	Name string							// no tag
 	Age string `json:"-"`				// json ignore tag
	sex string							// unexported field
 	Goal int   `json:"goal,omitempty"`  // omitempty if the field is zero value

 	// custom map key
 	Name string `json:"myName"`

ToMap convert the exported fields of a struct to map.

type Tag

type Tag struct {
	Name    string
	Options []string
}

Tag is abstract struct field tag

func (*Tag) HasOption

func (t *Tag) HasOption(opt string) bool

HasOption check if a struct field tag has option setting.

func (*Tag) IsEmpty

func (t *Tag) IsEmpty() bool

IsEmpty check if a struct field has tag setting.

Jump to

Keyboard shortcuts

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