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

227 lines
3.8 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package structs
import (
"errors"
"fmt"
"reflect"
)
var (
errNotExported = errors.New("field is not exported")
2024-02-18 10:42:21 +00:00
errNotSettable = errors.New("field is not settable")
)
// Field represents a single struct field that encapsulates high level
2024-02-18 10:42:21 +00:00
// functions around the field.
2024-02-18 10:42:21 +00:00
type Field struct {
value reflect.Value
field reflect.StructField
2024-02-18 10:42:21 +00:00
defaultTag string
}
// Tag returns the value associated with key in the tag string. If there is no
2024-02-18 10:42:21 +00:00
// such key in the tag, Tag returns the empty string.
2024-02-18 10:42:21 +00:00
func (f *Field) Tag(key string) string {
2024-02-18 10:42:21 +00:00
return f.field.Tag.Get(key)
2024-02-18 10:42:21 +00:00
}
// Value returns the underlying value of the field. It panics if the field
2024-02-18 10:42:21 +00:00
// is not exported.
2024-02-18 10:42:21 +00:00
func (f *Field) Value() interface{} {
2024-02-18 10:42:21 +00:00
return f.value.Interface()
2024-02-18 10:42:21 +00:00
}
// IsEmbedded returns true if the given field is an anonymous field (embedded)
2024-02-18 10:42:21 +00:00
func (f *Field) IsEmbedded() bool {
2024-02-18 10:42:21 +00:00
return f.field.Anonymous
2024-02-18 10:42:21 +00:00
}
// IsExported returns true if the given field is exported.
2024-02-18 10:42:21 +00:00
func (f *Field) IsExported() bool {
2024-02-18 10:42:21 +00:00
return f.field.PkgPath == ""
2024-02-18 10:42:21 +00:00
}
// IsZero returns true if the given field is not initialized (has a zero value).
2024-02-18 10:42:21 +00:00
// It panics if the field is not exported.
2024-02-18 10:42:21 +00:00
func (f *Field) IsZero() bool {
2024-02-18 10:42:21 +00:00
zero := reflect.Zero(f.value.Type()).Interface()
2024-02-18 10:42:21 +00:00
current := f.Value()
return reflect.DeepEqual(current, zero)
2024-02-18 10:42:21 +00:00
}
// Name returns the name of the given field
2024-02-18 10:42:21 +00:00
func (f *Field) Name() string {
2024-02-18 10:42:21 +00:00
return f.field.Name
2024-02-18 10:42:21 +00:00
}
// Kind returns the fields kind, such as "string", "map", "bool", etc ..
2024-02-18 10:42:21 +00:00
func (f *Field) Kind() reflect.Kind {
2024-02-18 10:42:21 +00:00
return f.value.Kind()
2024-02-18 10:42:21 +00:00
}
// Set sets the field to given value v. It returns an error if the field is not
2024-02-18 10:42:21 +00:00
// settable (not addressable or not exported) or if the given value's type
2024-02-18 10:42:21 +00:00
// doesn't match the fields type.
2024-02-18 10:42:21 +00:00
func (f *Field) Set(val interface{}) error {
2024-02-18 10:42:21 +00:00
// we can't set unexported fields, so be sure this field is exported
2024-02-18 10:42:21 +00:00
if !f.IsExported() {
2024-02-18 10:42:21 +00:00
return errNotExported
2024-02-18 10:42:21 +00:00
}
// do we get here? not sure...
2024-02-18 10:42:21 +00:00
if !f.value.CanSet() {
2024-02-18 10:42:21 +00:00
return errNotSettable
2024-02-18 10:42:21 +00:00
}
given := reflect.ValueOf(val)
if f.value.Kind() != given.Kind() {
2024-02-18 10:42:21 +00:00
return fmt.Errorf("wrong kind. got: %s want: %s", given.Kind(), f.value.Kind())
2024-02-18 10:42:21 +00:00
}
f.value.Set(given)
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// Zero sets the field to its zero value. It returns an error if the field is not
2024-02-18 10:42:21 +00:00
// settable (not addressable or not exported).
2024-02-18 10:42:21 +00:00
func (f *Field) Zero() error {
2024-02-18 10:42:21 +00:00
zero := reflect.Zero(f.value.Type()).Interface()
2024-02-18 10:42:21 +00:00
return f.Set(zero)
2024-02-18 10:42:21 +00:00
}
// Fields returns a slice of Fields. This is particular handy to get the fields
2024-02-18 10:42:21 +00:00
// of a nested struct . A struct tag with the content of "-" ignores the
2024-02-18 10:42:21 +00:00
// 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 *http.Request `structs:"-"`
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// It panics if field is not exported or if field's kind is not struct
2024-02-18 10:42:21 +00:00
func (f *Field) Fields() []*Field {
2024-02-18 10:42:21 +00:00
return getFields(f.value, f.defaultTag)
2024-02-18 10:42:21 +00:00
}
// Field returns the field from a nested struct. It panics if the nested struct
2024-02-18 10:42:21 +00:00
// is not exported or if the field was not found.
2024-02-18 10:42:21 +00:00
func (f *Field) Field(name string) *Field {
2024-02-18 10:42:21 +00:00
field, ok := f.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 field
2024-02-18 10:42:21 +00:00
}
// FieldOk returns the field from a nested struct. The boolean returns whether
2024-02-18 10:42:21 +00:00
// the field was found (true) or not (false).
2024-02-18 10:42:21 +00:00
func (f *Field) FieldOk(name string) (*Field, bool) {
2024-02-18 10:42:21 +00:00
value := &f.value
2024-02-18 10:42:21 +00:00
// value must be settable so we need to make sure it holds the address of the
2024-02-18 10:42:21 +00:00
// variable and not a copy, so we can pass the pointer to strctVal instead of a
2024-02-18 10:42:21 +00:00
// copy (which is not assigned to any variable, hence not settable).
2024-02-18 10:42:21 +00:00
// see "https://blog.golang.org/laws-of-reflection#TOC_8."
2024-02-18 10:42:21 +00:00
if f.value.Kind() != reflect.Ptr {
2024-02-18 10:42:21 +00:00
a := f.value.Addr()
2024-02-18 10:42:21 +00:00
value = &a
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
v := strctVal(value.Interface())
2024-02-18 10:42:21 +00:00
t := v.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{
2024-02-18 10:42:21 +00:00
field: field,
2024-02-18 10:42:21 +00:00
value: v.FieldByName(name),
}, true
2024-02-18 10:42:21 +00:00
}