forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/fatih/structs/structs.go

972 lines
15 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Package structs contains various utilities functions to work with structs.
2024-02-18 10:42:21 +00:00
package structs
import (
"fmt"
"reflect"
)
var (
2024-02-18 10:42:21 +00:00
// DefaultTagName is the default tag name for struct fields which provides
2024-02-18 10:42:21 +00:00
// a more granular to tweak certain structs. Lookup the necessary functions
2024-02-18 10:42:21 +00:00
// for more info.
2024-02-18 10:42:21 +00:00
DefaultTagName = "structs" // struct's field default tag name
2024-02-18 10:42:21 +00:00
)
// Struct encapsulates a struct type to provide several high level functions
2024-02-18 10:42:21 +00:00
// around the struct.
2024-02-18 10:42:21 +00:00
type Struct struct {
raw interface{}
value reflect.Value
2024-02-18 10:42:21 +00:00
TagName string
}
// New returns a new *Struct with the struct s. It panics if the s's kind is
2024-02-18 10:42:21 +00:00
// not struct.
2024-02-18 10:42:21 +00:00
func New(s interface{}) *Struct {
2024-02-18 10:42:21 +00:00
return &Struct{
raw: s,
value: strctVal(s),
2024-02-18 10:42:21 +00:00
TagName: DefaultTagName,
}
2024-02-18 10:42:21 +00:00
}
// Map converts the given struct to a map[string]interface{}, where the keys
2024-02-18 10:42:21 +00:00
// of the map are the field names and the values of the map the associated
2024-02-18 10:42:21 +00:00
// values of the fields. The default key string is the struct field name but
2024-02-18 10:42:21 +00:00
// can be changed in the struct field's tag value. The "structs" key in the
2024-02-18 10:42:21 +00:00
// struct's field tag value is the key name. Example:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// // Field appears in map as key "myName".
2024-06-14 08:41:36 +00:00
// Name string `structs:"myName"`
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// A tag value with the content of "-" ignores that particular field. Example:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// // Field is ignored by this package.
2024-06-14 08:41:36 +00:00
// Field bool `structs:"-"`
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// A tag value with the content of "string" uses the stringer to get the value. Example:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// // The value will be output of Animal's String() func.
2024-06-14 08:41:36 +00:00
// // Map will panic if Animal does not implement String().
2024-06-14 08:41:36 +00:00
// Field *Animal `structs:"field,string"`
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// A tag value with the option of "flatten" used in a struct field is to flatten its fields
2024-02-18 10:42:21 +00:00
// in the output map. Example:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// // The FieldStruct's fields will be flattened into the output map.
2024-06-14 08:41:36 +00:00
// FieldStruct time.Time `structs:",flatten"`
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// A tag value with the option of "omitnested" stops iterating further if the type
2024-02-18 10:42:21 +00:00
// is a struct. Example:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// // Field is not processed further by this package.
2024-06-14 08:41:36 +00:00
// Field time.Time `structs:"myName,omitnested"`
2024-06-14 08:41:36 +00:00
// Field *http.Request `structs:",omitnested"`
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// A tag value with the option of "omitempty" ignores that particular field if
2024-02-18 10:42:21 +00:00
// the field value is empty. Example:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// // Field appears in map as key "myName", but the field is
2024-06-14 08:41:36 +00:00
// // skipped if empty.
2024-06-14 08:41:36 +00:00
// Field string `structs:"myName,omitempty"`
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// // Field appears in map as key "Field" (the default), but
2024-06-14 08:41:36 +00:00
// // the field is skipped if empty.
2024-06-14 08:41:36 +00:00
// Field string `structs:",omitempty"`
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Note that only exported fields of a struct can be accessed, non exported
2024-02-18 10:42:21 +00:00
// fields will be neglected.
2024-02-18 10:42:21 +00:00
func (s *Struct) Map() map[string]interface{} {
2024-02-18 10:42:21 +00:00
out := make(map[string]interface{})
2024-02-18 10:42:21 +00:00
s.FillMap(out)
2024-02-18 10:42:21 +00:00
return out
2024-02-18 10:42:21 +00:00
}
// FillMap is the same as Map. Instead of returning the output, it fills the
2024-02-18 10:42:21 +00:00
// given map.
2024-02-18 10:42:21 +00:00
func (s *Struct) FillMap(out map[string]interface{}) {
2024-02-18 10:42:21 +00:00
if out == nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
fields := s.structFields()
for _, field := range fields {
2024-02-18 10:42:21 +00:00
name := field.Name
2024-02-18 10:42:21 +00:00
val := s.value.FieldByName(name)
2024-02-18 10:42:21 +00:00
isSubStruct := false
2024-02-18 10:42:21 +00:00
var finalVal interface{}
tagName, tagOpts := parseTag(field.Tag.Get(s.TagName))
2024-02-18 10:42:21 +00:00
if tagName != "" {
2024-02-18 10:42:21 +00:00
name = tagName
2024-02-18 10:42:21 +00:00
}
// if the value is a zero value and the field is marked as omitempty do
2024-02-18 10:42:21 +00:00
// not include
2024-02-18 10:42:21 +00:00
if tagOpts.Has("omitempty") {
2024-02-18 10:42:21 +00:00
zero := reflect.Zero(val.Type()).Interface()
2024-02-18 10:42:21 +00:00
current := val.Interface()
if reflect.DeepEqual(current, zero) {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if !tagOpts.Has("omitnested") {
2024-02-18 10:42:21 +00:00
finalVal = s.nested(val)
v := reflect.ValueOf(val.Interface())
2024-02-18 10:42:21 +00:00
if v.Kind() == reflect.Ptr {
2024-02-18 10:42:21 +00:00
v = v.Elem()
2024-02-18 10:42:21 +00:00
}
switch v.Kind() {
2024-02-18 10:42:21 +00:00
case reflect.Map, reflect.Struct:
2024-02-18 10:42:21 +00:00
isSubStruct = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
finalVal = val.Interface()
2024-02-18 10:42:21 +00:00
}
if tagOpts.Has("string") {
2024-02-18 10:42:21 +00:00
s, ok := val.Interface().(fmt.Stringer)
2024-02-18 10:42:21 +00:00
if ok {
2024-02-18 10:42:21 +00:00
out[name] = s.String()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
if isSubStruct && (tagOpts.Has("flatten")) {
2024-02-18 10:42:21 +00:00
for k := range finalVal.(map[string]interface{}) {
2024-02-18 10:42:21 +00:00
out[k] = finalVal.(map[string]interface{})[k]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
out[name] = finalVal
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Values converts the given s struct's field values to a []interface{}. A
2024-02-18 10:42:21 +00:00
// struct tag with the content of "-" ignores the that particular field.
2024-02-18 10:42:21 +00:00
// Example:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// // Field is ignored by this package.
2024-06-14 08:41:36 +00:00
// Field int `structs:"-"`
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// A value with the option of "omitnested" stops iterating further if the type
2024-02-18 10:42:21 +00:00
// is a struct. Example:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// // Fields is not processed further by this package.
2024-06-14 08:41:36 +00:00
// Field time.Time `structs:",omitnested"`
2024-06-14 08:41:36 +00:00
// Field *http.Request `structs:",omitnested"`
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// A tag value with the option of "omitempty" ignores that particular field and
2024-02-18 10:42:21 +00:00
// is not added to the values if the field value is empty. Example:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// // Field is skipped if empty
2024-06-14 08:41:36 +00:00
// Field string `structs:",omitempty"`
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Note that only exported fields of a struct can be accessed, non exported
2024-02-18 10:42:21 +00:00
// fields will be neglected.
2024-02-18 10:42:21 +00:00
func (s *Struct) Values() []interface{} {
2024-02-18 10:42:21 +00:00
fields := s.structFields()
var t []interface{}
for _, field := range fields {
2024-02-18 10:42:21 +00:00
val := s.value.FieldByName(field.Name)
_, tagOpts := parseTag(field.Tag.Get(s.TagName))
// if the value is a zero value and the field is marked as omitempty do
2024-02-18 10:42:21 +00:00
// not include
2024-02-18 10:42:21 +00:00
if tagOpts.Has("omitempty") {
2024-02-18 10:42:21 +00:00
zero := reflect.Zero(val.Type()).Interface()
2024-02-18 10:42:21 +00:00
current := val.Interface()
if reflect.DeepEqual(current, zero) {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if tagOpts.Has("string") {
2024-02-18 10:42:21 +00:00
s, ok := val.Interface().(fmt.Stringer)
2024-02-18 10:42:21 +00:00
if ok {
2024-02-18 10:42:21 +00:00
t = append(t, s.String())
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
if IsStruct(val.Interface()) && !tagOpts.Has("omitnested") {
2024-02-18 10:42:21 +00:00
// look out for embedded structs, and convert them to a
2024-02-18 10:42:21 +00:00
// []interface{} to be added to the final values slice
2024-02-18 10:42:21 +00:00
t = append(t, Values(val.Interface())...)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
t = append(t, val.Interface())
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return t
2024-02-18 10:42:21 +00:00
}
// Fields returns a slice of Fields. A struct tag with the content of "-"
2024-02-18 10:42:21 +00:00
// ignores the checking of that particular field. Example:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// // Field is ignored by this package.
2024-06-14 08:41:36 +00:00
// Field bool `structs:"-"`
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// It panics if s's kind is not struct.
2024-02-18 10:42:21 +00:00
func (s *Struct) Fields() []*Field {
2024-02-18 10:42:21 +00:00
return getFields(s.value, s.TagName)
2024-02-18 10:42:21 +00:00
}
// Names returns a slice of field names. A struct tag with the content of "-"
2024-02-18 10:42:21 +00:00
// ignores the checking of that particular field. Example:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// // Field is ignored by this package.
2024-06-14 08:41:36 +00:00
// Field bool `structs:"-"`
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// It panics if s's kind is not struct.
2024-02-18 10:42:21 +00:00
func (s *Struct) Names() []string {
2024-02-18 10:42:21 +00:00
fields := getFields(s.value, s.TagName)
names := make([]string, len(fields))
for i, field := range fields {
2024-02-18 10:42:21 +00:00
names[i] = field.Name()
2024-02-18 10:42:21 +00:00
}
return names
2024-02-18 10:42:21 +00:00
}
func getFields(v reflect.Value, tagName string) []*Field {
2024-02-18 10:42:21 +00:00
if v.Kind() == reflect.Ptr {
2024-02-18 10:42:21 +00:00
v = v.Elem()
2024-02-18 10:42:21 +00:00
}
t := v.Type()
var fields []*Field
for i := 0; i < t.NumField(); i++ {
2024-02-18 10:42:21 +00:00
field := t.Field(i)
if tag := field.Tag.Get(tagName); tag == "-" {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
f := &Field{
2024-02-18 10:42:21 +00:00
field: field,
2024-02-18 10:42:21 +00:00
value: v.FieldByName(field.Name),
}
fields = append(fields, f)
}
return fields
2024-02-18 10:42:21 +00:00
}
// Field returns a new Field struct that provides several high level functions
2024-02-18 10:42:21 +00:00
// around a single struct field entity. It panics if the field is not found.
2024-02-18 10:42:21 +00:00
func (s *Struct) Field(name string) *Field {
2024-02-18 10:42:21 +00:00
f, ok := s.FieldOk(name)
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
panic("field not found")
2024-02-18 10:42:21 +00:00
}
return f
2024-02-18 10:42:21 +00:00
}
// FieldOk returns a new Field struct that provides several high level functions
2024-02-18 10:42:21 +00:00
// around a single struct field entity. The boolean returns true if the field
2024-02-18 10:42:21 +00:00
// was found.
2024-02-18 10:42:21 +00:00
func (s *Struct) FieldOk(name string) (*Field, bool) {
2024-02-18 10:42:21 +00:00
t := s.value.Type()
field, ok := t.FieldByName(name)
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return nil, false
2024-02-18 10:42:21 +00:00
}
return &Field{
field: field,
value: s.value.FieldByName(name),
2024-02-18 10:42:21 +00:00
defaultTag: s.TagName,
}, true
2024-02-18 10:42:21 +00:00
}
// IsZero returns true if all fields in a struct is a zero value (not
2024-02-18 10:42:21 +00:00
// initialized) A struct tag with the content of "-" ignores the checking of
2024-02-18 10:42:21 +00:00
// that particular field. Example:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// // Field is ignored by this package.
2024-06-14 08:41:36 +00:00
// Field bool `structs:"-"`
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// A value with the option of "omitnested" stops iterating further if the type
2024-02-18 10:42:21 +00:00
// is a struct. Example:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// // Field is not processed further by this package.
2024-06-14 08:41:36 +00:00
// Field time.Time `structs:"myName,omitnested"`
2024-06-14 08:41:36 +00:00
// Field *http.Request `structs:",omitnested"`
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Note that only exported fields of a struct can be accessed, non exported
2024-02-18 10:42:21 +00:00
// fields will be neglected. It panics if s's kind is not struct.
2024-02-18 10:42:21 +00:00
func (s *Struct) IsZero() bool {
2024-02-18 10:42:21 +00:00
fields := s.structFields()
for _, field := range fields {
2024-02-18 10:42:21 +00:00
val := s.value.FieldByName(field.Name)
_, tagOpts := parseTag(field.Tag.Get(s.TagName))
if IsStruct(val.Interface()) && !tagOpts.Has("omitnested") {
2024-02-18 10:42:21 +00:00
ok := IsZero(val.Interface())
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
continue
2024-02-18 10:42:21 +00:00
}
// zero value of the given field, such as "" for string, 0 for int
2024-02-18 10:42:21 +00:00
zero := reflect.Zero(val.Type()).Interface()
// current value of the given field
2024-02-18 10:42:21 +00:00
current := val.Interface()
if !reflect.DeepEqual(current, zero) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return true
2024-02-18 10:42:21 +00:00
}
// HasZero returns true if a field in a struct is not initialized (zero value).
2024-02-18 10:42:21 +00:00
// A struct tag with the content of "-" ignores the checking of that particular
2024-02-18 10:42:21 +00:00
// field. Example:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// // Field is ignored by this package.
2024-06-14 08:41:36 +00:00
// Field bool `structs:"-"`
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// A value with the option of "omitnested" stops iterating further if the type
2024-02-18 10:42:21 +00:00
// is a struct. Example:
2024-02-18 10:42:21 +00:00
//
2024-06-14 08:41:36 +00:00
// // Field is not processed further by this package.
2024-06-14 08:41:36 +00:00
// Field time.Time `structs:"myName,omitnested"`
2024-06-14 08:41:36 +00:00
// Field *http.Request `structs:",omitnested"`
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Note that only exported fields of a struct can be accessed, non exported
2024-02-18 10:42:21 +00:00
// fields will be neglected. It panics if s's kind is not struct.
2024-02-18 10:42:21 +00:00
func (s *Struct) HasZero() bool {
2024-02-18 10:42:21 +00:00
fields := s.structFields()
for _, field := range fields {
2024-02-18 10:42:21 +00:00
val := s.value.FieldByName(field.Name)
_, tagOpts := parseTag(field.Tag.Get(s.TagName))
if IsStruct(val.Interface()) && !tagOpts.Has("omitnested") {
2024-02-18 10:42:21 +00:00
ok := HasZero(val.Interface())
2024-02-18 10:42:21 +00:00
if ok {
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
continue
2024-02-18 10:42:21 +00:00
}
// zero value of the given field, such as "" for string, 0 for int
2024-02-18 10:42:21 +00:00
zero := reflect.Zero(val.Type()).Interface()
// current value of the given field
2024-02-18 10:42:21 +00:00
current := val.Interface()
if reflect.DeepEqual(current, zero) {
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return false
2024-02-18 10:42:21 +00:00
}
// Name returns the structs's type name within its package. For more info refer
2024-02-18 10:42:21 +00:00
// to Name() function.
2024-02-18 10:42:21 +00:00
func (s *Struct) Name() string {
2024-02-18 10:42:21 +00:00
return s.value.Type().Name()
2024-02-18 10:42:21 +00:00
}
// structFields returns the exported struct fields for a given s struct. This
2024-02-18 10:42:21 +00:00
// is a convenient helper method to avoid duplicate code in some of the
2024-02-18 10:42:21 +00:00
// functions.
2024-02-18 10:42:21 +00:00
func (s *Struct) structFields() []reflect.StructField {
2024-02-18 10:42:21 +00:00
t := s.value.Type()
var f []reflect.StructField
for i := 0; i < t.NumField(); i++ {
2024-02-18 10:42:21 +00:00
field := t.Field(i)
2024-02-18 10:42:21 +00:00
// we can't access the value of unexported fields
2024-02-18 10:42:21 +00:00
if field.PkgPath != "" {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
// don't check if it's omitted
2024-02-18 10:42:21 +00:00
if tag := field.Tag.Get(s.TagName); tag == "-" {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
f = append(f, field)
2024-02-18 10:42:21 +00:00
}
return f
2024-02-18 10:42:21 +00:00
}
func strctVal(s interface{}) reflect.Value {
2024-02-18 10:42:21 +00:00
v := reflect.ValueOf(s)
// if pointer get the underlying element≤
2024-02-18 10:42:21 +00:00
for v.Kind() == reflect.Ptr {
2024-02-18 10:42:21 +00:00
v = v.Elem()
2024-02-18 10:42:21 +00:00
}
if v.Kind() != reflect.Struct {
2024-02-18 10:42:21 +00:00
panic("not struct")
2024-02-18 10:42:21 +00:00
}
return v
2024-02-18 10:42:21 +00:00
}
// Map converts the given struct to a map[string]interface{}. For more info
2024-02-18 10:42:21 +00:00
// refer to Struct types Map() method. It panics if s's kind is not struct.
2024-02-18 10:42:21 +00:00
func Map(s interface{}) map[string]interface{} {
2024-02-18 10:42:21 +00:00
return New(s).Map()
2024-02-18 10:42:21 +00:00
}
// FillMap is the same as Map. Instead of returning the output, it fills the
2024-02-18 10:42:21 +00:00
// given map.
2024-02-18 10:42:21 +00:00
func FillMap(s interface{}, out map[string]interface{}) {
2024-02-18 10:42:21 +00:00
New(s).FillMap(out)
2024-02-18 10:42:21 +00:00
}
// Values converts the given struct to a []interface{}. For more info refer to
2024-02-18 10:42:21 +00:00
// Struct types Values() method. It panics if s's kind is not struct.
2024-02-18 10:42:21 +00:00
func Values(s interface{}) []interface{} {
2024-02-18 10:42:21 +00:00
return New(s).Values()
2024-02-18 10:42:21 +00:00
}
// Fields returns a slice of *Field. For more info refer to Struct types
2024-02-18 10:42:21 +00:00
// Fields() method. It panics if s's kind is not struct.
2024-02-18 10:42:21 +00:00
func Fields(s interface{}) []*Field {
2024-02-18 10:42:21 +00:00
return New(s).Fields()
2024-02-18 10:42:21 +00:00
}
// Names returns a slice of field names. For more info refer to Struct types
2024-02-18 10:42:21 +00:00
// Names() method. It panics if s's kind is not struct.
2024-02-18 10:42:21 +00:00
func Names(s interface{}) []string {
2024-02-18 10:42:21 +00:00
return New(s).Names()
2024-02-18 10:42:21 +00:00
}
// IsZero returns true if all fields is equal to a zero value. For more info
2024-02-18 10:42:21 +00:00
// refer to Struct types IsZero() method. It panics if s's kind is not struct.
2024-02-18 10:42:21 +00:00
func IsZero(s interface{}) bool {
2024-02-18 10:42:21 +00:00
return New(s).IsZero()
2024-02-18 10:42:21 +00:00
}
// HasZero returns true if any field is equal to a zero value. For more info
2024-02-18 10:42:21 +00:00
// refer to Struct types HasZero() method. It panics if s's kind is not struct.
2024-02-18 10:42:21 +00:00
func HasZero(s interface{}) bool {
2024-02-18 10:42:21 +00:00
return New(s).HasZero()
2024-02-18 10:42:21 +00:00
}
// IsStruct returns true if the given variable is a struct or a pointer to
2024-02-18 10:42:21 +00:00
// struct.
2024-02-18 10:42:21 +00:00
func IsStruct(s interface{}) bool {
2024-02-18 10:42:21 +00:00
v := reflect.ValueOf(s)
2024-02-18 10:42:21 +00:00
if v.Kind() == reflect.Ptr {
2024-02-18 10:42:21 +00:00
v = v.Elem()
2024-02-18 10:42:21 +00:00
}
// uninitialized zero value of a struct
2024-02-18 10:42:21 +00:00
if v.Kind() == reflect.Invalid {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
return v.Kind() == reflect.Struct
2024-02-18 10:42:21 +00:00
}
// Name returns the structs's type name within its package. It returns an
2024-02-18 10:42:21 +00:00
// empty string for unnamed types. It panics if s's kind is not struct.
2024-02-18 10:42:21 +00:00
func Name(s interface{}) string {
2024-02-18 10:42:21 +00:00
return New(s).Name()
2024-02-18 10:42:21 +00:00
}
// nested retrieves recursively all types for the given value and returns the
2024-02-18 10:42:21 +00:00
// nested value.
2024-02-18 10:42:21 +00:00
func (s *Struct) nested(val reflect.Value) interface{} {
2024-02-18 10:42:21 +00:00
var finalVal interface{}
v := reflect.ValueOf(val.Interface())
2024-02-18 10:42:21 +00:00
if v.Kind() == reflect.Ptr {
2024-02-18 10:42:21 +00:00
v = v.Elem()
2024-02-18 10:42:21 +00:00
}
switch v.Kind() {
2024-02-18 10:42:21 +00:00
case reflect.Struct:
2024-02-18 10:42:21 +00:00
n := New(val.Interface())
2024-02-18 10:42:21 +00:00
n.TagName = s.TagName
2024-02-18 10:42:21 +00:00
m := n.Map()
// do not add the converted value if there are no exported fields, ie:
2024-02-18 10:42:21 +00:00
// time.Time
2024-02-18 10:42:21 +00:00
if len(m) == 0 {
2024-02-18 10:42:21 +00:00
finalVal = val.Interface()
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
finalVal = m
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case reflect.Map:
2024-02-18 10:42:21 +00:00
// get the element type of the map
2024-02-18 10:42:21 +00:00
mapElem := val.Type()
2024-02-18 10:42:21 +00:00
switch val.Type().Kind() {
2024-02-18 10:42:21 +00:00
case reflect.Ptr, reflect.Array, reflect.Map,
2024-02-18 10:42:21 +00:00
reflect.Slice, reflect.Chan:
2024-02-18 10:42:21 +00:00
mapElem = val.Type().Elem()
2024-02-18 10:42:21 +00:00
if mapElem.Kind() == reflect.Ptr {
2024-02-18 10:42:21 +00:00
mapElem = mapElem.Elem()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// only iterate over struct types, ie: map[string]StructType,
2024-02-18 10:42:21 +00:00
// map[string][]StructType,
2024-02-18 10:42:21 +00:00
if mapElem.Kind() == reflect.Struct ||
2024-02-18 10:42:21 +00:00
(mapElem.Kind() == reflect.Slice &&
2024-02-18 10:42:21 +00:00
mapElem.Elem().Kind() == reflect.Struct) {
2024-02-18 10:42:21 +00:00
m := make(map[string]interface{}, val.Len())
2024-02-18 10:42:21 +00:00
for _, k := range val.MapKeys() {
2024-02-18 10:42:21 +00:00
m[k.String()] = s.nested(val.MapIndex(k))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
finalVal = m
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
// TODO(arslan): should this be optional?
2024-02-18 10:42:21 +00:00
finalVal = val.Interface()
2024-02-18 10:42:21 +00:00
case reflect.Slice, reflect.Array:
2024-02-18 10:42:21 +00:00
if val.Type().Kind() == reflect.Interface {
2024-02-18 10:42:21 +00:00
finalVal = val.Interface()
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
// TODO(arslan): should this be optional?
2024-02-18 10:42:21 +00:00
// do not iterate of non struct types, just pass the value. Ie: []int,
2024-02-18 10:42:21 +00:00
// []string, co... We only iterate further if it's a struct.
2024-02-18 10:42:21 +00:00
// i.e []foo or []*foo
2024-02-18 10:42:21 +00:00
if val.Type().Elem().Kind() != reflect.Struct &&
2024-02-18 10:42:21 +00:00
!(val.Type().Elem().Kind() == reflect.Ptr &&
2024-02-18 10:42:21 +00:00
val.Type().Elem().Elem().Kind() == reflect.Struct) {
2024-02-18 10:42:21 +00:00
finalVal = val.Interface()
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
slices := make([]interface{}, val.Len())
2024-02-18 10:42:21 +00:00
for x := 0; x < val.Len(); x++ {
2024-02-18 10:42:21 +00:00
slices[x] = s.nested(val.Index(x))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
finalVal = slices
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
finalVal = val.Interface()
2024-02-18 10:42:21 +00:00
}
return finalVal
2024-02-18 10:42:21 +00:00
}