structure: add constructor to provide a more advanced functions around
structs
This commit is contained in:
parent
50c639fd56
commit
fe137c012f
20
field.go
Normal file
20
field.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package structure
|
||||||
|
|
||||||
|
import "reflect"
|
||||||
|
|
||||||
|
// Field represents a single struct field that encapsulates many high level
|
||||||
|
// function around a singel struct field
|
||||||
|
type Field struct {
|
||||||
|
value reflect.Value
|
||||||
|
field reflect.StructField
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tag returns the value associated with key in the tag string. If there is no
|
||||||
|
// such key in the tag, Tag returns the empty string
|
||||||
|
func (f *Field) Tag(key string) string {
|
||||||
|
return f.field.Tag.Get(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Field) Value() interface{} {
|
||||||
|
return f.value.Interface()
|
||||||
|
}
|
||||||
89
structs.go
Normal file
89
structs.go
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
package structure
|
||||||
|
|
||||||
|
// Struct encapsulates a struct type to provide several high level functions
|
||||||
|
// around the structure.
|
||||||
|
type Struct struct {
|
||||||
|
raw interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns a new *Struct with the struct s.
|
||||||
|
func New(s interface{}) *Struct {
|
||||||
|
return &Struct{
|
||||||
|
raw: s,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map converts the given struct to a map[string]interface{}. For more info
|
||||||
|
// refer to Map() function.
|
||||||
|
func (s *Struct) Map() map[string]interface{} {
|
||||||
|
return Map(s.raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Values converts the given struct to a []interface{}. For more info refer to
|
||||||
|
// Values() function.
|
||||||
|
func (s *Struct) Values() []interface{} {
|
||||||
|
return Values(s.raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fields returns a slice of field names For more info refer to Fields()
|
||||||
|
// function.
|
||||||
|
func (s *Struct) Fields() []string {
|
||||||
|
return Fields(s.raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Field returns a new Field struct that provides several high level functions
|
||||||
|
// around a single struct field entitiy. It panics if the field is not found or
|
||||||
|
// is unexported.
|
||||||
|
func (s *Struct) Field(name string) *Field {
|
||||||
|
f, ok := s.FieldOk(name)
|
||||||
|
if !ok {
|
||||||
|
panic("field not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
// Field returns a new Field struct that provides several high level functions
|
||||||
|
// around a single struct field entitiy and a boolean indicating if the field
|
||||||
|
// was found. It panics if the or is unexported.
|
||||||
|
func (s *Struct) FieldOk(name string) (*Field, bool) {
|
||||||
|
v := strctVal(s.raw)
|
||||||
|
t := v.Type()
|
||||||
|
|
||||||
|
field, ok := t.FieldByName(name)
|
||||||
|
if !ok {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
if field.PkgPath != "" {
|
||||||
|
panic("unexported field access is not allowed")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Field{
|
||||||
|
field: field,
|
||||||
|
value: v.FieldByName(name),
|
||||||
|
}, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsZero returns true if all fields is equal to a zero value. For more info
|
||||||
|
// refer to IsZero() function.
|
||||||
|
func (s *Struct) IsZero() bool {
|
||||||
|
return IsZero(s.raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
// HasZero returns true if any field is equal to a zero value. For more info
|
||||||
|
// refer to HasZero() function.
|
||||||
|
func (s *Struct) HasZero() bool {
|
||||||
|
return HasZero(s.raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name returns the structs's type name within its package. For more info refer
|
||||||
|
// to Name() function.
|
||||||
|
func (s *Struct) Name() string {
|
||||||
|
return Name(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsStruct returns true if its a struct or a pointer to struct.
|
||||||
|
func (s *Struct) IsStruct() bool {
|
||||||
|
return IsStruct(s.raw)
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user