xstruct

package
v0.25.15 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

README

GoKit - xstruct

Struct kits for Golang development.

Installation

go get -u github.com/likexian/gokit

Importing

import (
    "github.com/likexian/gokit/xstruct"
)

Documentation

Visit the docs on GoDoc

Example

Define a struct first
// Define Staff struct
type Staff struct {
    Id int64 `json:"id"`
    Name string  `json:"name"`
    Enabled bool  `json:"enabled"`
}

// Init staff struct
staff := Staff{1, "likexian", true}
Use as global functions
// ["Id", "Name", "Enabled"]
names, _ := xstruct.Names(staff)

// [1, "likexian", true]
values, _ := xstruct.Values(staff)

// list all field as [*Field]
fields, _ := xstruct.Fields(staff)

// get struct field value
value, _ := xstruct.Field(staff, "Name").Value()

// set struct field value
xstruct.Set(staff, "Name", "kexian.li")
Use as Interactive mode
// create a xstruct object
s, err := xstruct.New(staff)
if err != nil {
    panic(err)
}

// ["Id", "Name", "Enabled"]
names := s.Names()

// [1, "likexian", true]
values := s.Values()

// list all field as [*Field]
fields := s.Fields()

// get struct field value
value := s.Field("Name").Value()

// set struct field value
s.Set("Name", "kexian.li")

License

Copyright 2012-2024 Li Kexian

Licensed under the Apache License 2.0

Donation

If this project is helpful, please share it with friends.

If you want to thank me, you can give me a cup of coffee.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotStruct not a valid struct
	ErrNotStruct = errors.New("xstruct: not a valid struct")
	// ErrNoField field name is not exists
	ErrNoField = errors.New("xstruct: field name is not exists")
	// ErrNotExported not an exported field
	ErrNotExported = errors.New("xstruct: not an exported field")
	// ErrNotSettable not a settable field
	ErrNotSettable = errors.New("xstruct: not a settable field")
)

Functions

func Author

func Author() string

Author returns package author

func IsStruct

func IsStruct(v interface{}) bool

IsStruct returns if v is a struct

func License

func License() string

License returns package license

func Map

func Map(v interface{}) (map[string]interface{}, error)

Map returns struct name value as map

func Name

func Name(v interface{}) (string, error)

Name returns name of struct

func Names

func Names(v interface{}) ([]string, error)

Names returns names of struct

func Set

func Set(v interface{}, name string, value interface{}) error

Set set value to the field name, must be exported field

func Tags

func Tags(v interface{}, key string) (map[string]string, error)

Tags returns tags of struct

func Values

func Values(v interface{}) ([]interface{}, error)

Values returns values of struct

func Version

func Version() string

Version returns package version

func Zero

func Zero(v interface{}, name string) error

Zero set zero value to the field name, must be exported field

Types

type Fieldx

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

Fieldx storing struct field

func Field

func Field(v interface{}, name string) (*Fieldx, bool)

Field returns a field with name

func Fields

func Fields(v interface{}) ([]*Fieldx, error)

Fields return fields of struct

func MustField

func MustField(v interface{}, name string) *Fieldx

MustField returns a field with name, panic if error

func (*Fieldx) Addr added in v0.24.4

func (f *Fieldx) Addr() interface{}

Addr returns address of field

func (*Fieldx) IsAnonymous

func (f *Fieldx) IsAnonymous() bool

IsAnonymous returns if field is anonymous

func (*Fieldx) IsExport

func (f *Fieldx) IsExport() bool

IsExport returns if field is exported

func (*Fieldx) IsZero

func (f *Fieldx) IsZero() bool

IsZero returns if field have zero value, for example not initialized it panic if field is not exported

func (*Fieldx) Kind

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

Kind returns kind of field

func (*Fieldx) Name

func (f *Fieldx) Name() string

Name returns name of field

func (*Fieldx) Set

func (f *Fieldx) Set(v interface{}) error

Set set value to the field, must be exported field

func (*Fieldx) Tag

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

Tag returns tag of field by key

func (*Fieldx) Value

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

Value returns value of field

func (*Fieldx) Zero

func (f *Fieldx) Zero() error

Zero set field to zero value, must be exported field

type Structx

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

Structx storing struct data

func New

func New(v interface{}) (*Structx, error)

New returns a new xstruct object

func Struct

func Struct(v interface{}, name string) (*Structx, error)

Struct returns nested struct with name

func (*Structx) Field

func (s *Structx) Field(name string) (*Fieldx, bool)

Field returns a field with name

func (*Structx) Fields

func (s *Structx) Fields() []*Fieldx

Fields return fields of struct

func (*Structx) HasField

func (s *Structx) HasField(name string) bool

HasField returns field is exists

func (*Structx) IsStruct

func (s *Structx) IsStruct(name string) bool

IsStruct returns if field name is a struct

func (*Structx) Map

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

Map returns struct name value as map

func (*Structx) MustField

func (s *Structx) MustField(name string) *Fieldx

MustField returns a field with name, panic if error

func (*Structx) Name

func (s *Structx) Name() string

Name returns name of struct

func (*Structx) Names

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

Names returns names of struct

func (*Structx) Set

func (s *Structx) Set(name string, value interface{}) error

Set set value to the field name, must be exported field

func (*Structx) Struct

func (s *Structx) Struct(name string) (*Structx, error)

Struct returns nested struct with name

func (*Structx) Tags

func (s *Structx) Tags(key string) map[string]string

Tags returns tags of struct

func (*Structx) Values

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

Values returns values of struct

func (*Structx) Zero

func (s *Structx) Zero(name string) error

Zero set zero value to the field name, must be exported field

Jump to

Keyboard shortcuts

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