_struct

package
v0.0.1-20230330-0003 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2023 License: MIT Imports: 10 Imported by: 3

Documentation

Index

Constants

View Source
const (
	InvalidObjType        = "obj type should be non-pointer and type struct"
	InvalidPointerObjType = "obj type should be pointer and type struct"
)
View Source
const (
	DefaultTagName = "structs"
)

Variables

This section is empty.

Functions

func FieldExists

func FieldExists(obj interface{}, fieldName string) bool

func GetExportableFieldsByJSONTag

func GetExportableFieldsByJSONTag(obj interface{}) []string

TODO: move or rename this func

func GetFieldNamesByTagKeyExistence

func GetFieldNamesByTagKeyExistence(obj interface{}, tagName string, tagKey string) []string

GetFieldNamesByTagKeyExistence -> it will return the field names

func GetFieldTagKeyValue

func GetFieldTagKeyValue(obj interface{}, fieldName string, tagName string, tagKey string) string

func GetFieldType

func GetFieldType(obj interface{}, fieldName string) string

func GetFieldTypeName

func GetFieldTypeName(obj interface{}, fieldName string) string

func GetFieldValue

func GetFieldValue(obj interface{}, fieldName string) interface{}

GetFieldValue -> return a structure field value

func GetFieldsByTagExistence

func GetFieldsByTagExistence(obj interface{}, tagName string) []string

func GetName

func GetName(obj interface{}) string

func GetNestedFieldReflectValue

func GetNestedFieldReflectValue(val reflect.Value, path string) (reflect.Value, error)

func GetPointerStructValue

func GetPointerStructValue(obj interface{}) interface{}

func GetType

func GetType(obj interface{}) string

func IsFieldTagExists

func IsFieldTagExists(obj interface{}, fieldName string, tagName string) bool

IsFieldTagExists -> standard version

func IsFieldTagExistsExt

func IsFieldTagExistsExt(obj interface{}, fieldName string, tagName string) (tagExists bool, isFieldFound bool)

IsFieldTagExistsExt -> extended

func IsFieldTagKeyExists

func IsFieldTagKeyExists(obj interface{}, fieldName string, tagName string, tagKey string) bool

func IsPlainStruct

func IsPlainStruct(obj interface{}) bool

IsPlainStruct -> checks if it's not a pointer and it's a clear/clean struct!

func IsPointer

func IsPointer(obj interface{}) bool

func IsStruct

func IsStruct(obj interface{}) bool

IsStruct -> it can be a pointer of a struct or a struct! if it's a &Struct then it will return a ptr!, it's gonna be ptr

func SetAny

func SetAny(obj interface{}, fieldName string, val interface{}) bool

func SetBool

func SetBool(obj interface{}, fieldName string, val bool) bool

func SetDefaultValues

func SetDefaultValues(obj interface{}) error

SetDefaultValues -> Set initializes members in a struct referenced by a pointer. Maps and slices are initialized by `make` and other primitive types are set with default values. `ptr` should be a struct pointer

func SetInterface

func SetInterface(obj interface{}, fieldName string, val interface{}) bool

func SetUUID

func SetUUID(obj interface{}, fieldName string, val uuid.UUID) bool

func StrStrToStrInterface

func StrStrToStrInterface(obj map[string]string) map[string]interface{}

Types

type Helper

type Helper struct {
	// this is the plain structure!
	NonPtrObj interface{}
	// this is the pointer to structure!
	PtrObj interface{}

	TagName string
	// contains filtered or unexported fields
}

func New

func New(obj interface{}) *Helper

func (*Helper) Bool

func (h *Helper) Bool(fieldName string, val bool) bool

func (*Helper) FieldExists

func (h *Helper) FieldExists(fieldName string) bool

func (*Helper) FillMap

func (h *Helper) FillMap(out map[string]interface{})

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

func (*Helper) GetFieldNamesByTagKeyExistence

func (h *Helper) GetFieldNamesByTagKeyExistence(tagName string, tagKey string) []string

func (*Helper) GetFieldReflectType

func (h *Helper) GetFieldReflectType(fieldName string) reflect.Type

func (*Helper) GetFieldTag

func (h *Helper) GetFieldTag(fieldName string) string

func (*Helper) GetFieldTagKeyValue

func (h *Helper) GetFieldTagKeyValue(fieldName string, tagName string, tagKey string) string

func (*Helper) GetFieldTagValue

func (h *Helper) GetFieldTagValue(fieldName string, tagName string) string

func (*Helper) GetFieldType

func (h *Helper) GetFieldType(fieldName string) string

func (*Helper) GetFieldTypeName

func (h *Helper) GetFieldTypeName(fieldName string) string

func (*Helper) GetFieldValue

func (h *Helper) GetFieldValue(fieldName string) interface{}

func (*Helper) GetFieldsByTagExistence

func (h *Helper) GetFieldsByTagExistence(tagName string) []string

func (*Helper) GetPointerStructValue

func (h *Helper) GetPointerStructValue() interface{}

func (*Helper) GetTagValByInputRef

func (h *Helper) GetTagValByInputRef(inputRef *InputRef, fieldName string, tagName string) string

func (*Helper) GetType

func (h *Helper) GetType() string

func (*Helper) IsFieldTagExists

func (h *Helper) IsFieldTagExists(fieldName string, tagName string) bool

func (*Helper) IsFieldTagExistsExt

func (h *Helper) IsFieldTagExistsExt(fieldName string, tagName string) (tagExists bool, isFieldFound bool)

func (*Helper) IsFieldTagKeyExists

func (h *Helper) IsFieldTagKeyExists(inputRef *InputRef, fieldName string, tagName string, tagKey string) bool

func (*Helper) IsPointer

func (h *Helper) IsPointer() bool

func (*Helper) Map

func (h *Helper) Map() map[string]interface{}

Map converts the given struct to a map[string]interface{}, where the keys of the map are the field names and the values of the map the associated values of the fields. The default key string is the struct field name but can be changed in the struct field's tag value. The "structs" key in the struct's field tag value is the key name. Example:

// Field appears in map as key "myName".
Name string `structs:"myName"`

A tag value with the content of "-" ignores that particular field. Example:

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

A tag value with the content of "string" uses the stringer to get the value. Example:

// The value will be output of Animal's String() func.
// Map will panic if Animal does not implement String().
Field *Animal `structs:"field,string"`

A tag value with the option of "flatten" used in a struct field is to flatten its fields in the output map. Example:

// The FieldStruct's fields will be flattened into the output map.
FieldStruct time.Time `structs:",flatten"`

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

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

A tag value with the option of "omitempty" ignores that particular field if the field value is empty. Example:

// Field appears in map as key "myName", but the field is
// skipped if empty.
Field string `structs:"myName,omitempty"`

// Field appears in map as key "Field" (the default), but
// the field is skipped if empty.
Field string `structs:",omitempty"`

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

func (*Helper) SetAny

func (h *Helper) SetAny(fieldName string, val interface{}) bool

func (*Helper) SetBool

func (h *Helper) SetBool(fieldName string, val bool) bool

func (*Helper) SetInterface

func (h *Helper) SetInterface(fieldName string, val interface{}) bool

func (*Helper) SetUUID

func (h *Helper) SetUUID(fieldName string, val uuid.UUID) bool

func (*Helper) StructFields

func (h *Helper) StructFields() []reflect.StructField

StructFields returns the exported struct fields for a given s struct. This is a convenient helper method to avoid duplicate code in some of the functions.

type InputRef

type InputRef struct {
	// you can choose what to set, one of these options
	//Helper *Helper
	TagVal string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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