2022-06-29 10:49:42 +00:00
|
|
|
package dynamic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StructFieldIterator func(tag string, ft reflect.StructField, fv reflect.Value) error
|
|
|
|
|
|
|
|
var ErrCanNotIterateNilPointer = errors.New("can not iterate struct on a nil pointer")
|
|
|
|
|
2022-07-06 13:58:26 +00:00
|
|
|
func IterateFields(obj interface{}, cb func(ft reflect.StructField, fv reflect.Value) error) error {
|
2022-07-06 14:01:35 +00:00
|
|
|
if obj == nil {
|
|
|
|
return errors.New("can not iterate field, given object is nil")
|
|
|
|
}
|
|
|
|
|
2022-07-06 13:58:26 +00:00
|
|
|
sv := reflect.ValueOf(obj)
|
|
|
|
st := reflect.TypeOf(obj)
|
|
|
|
|
|
|
|
if st.Kind() != reflect.Ptr {
|
|
|
|
return fmt.Errorf("f should be a pointer of a struct, %s given", st)
|
|
|
|
}
|
|
|
|
|
|
|
|
// for pointer, check if it's nil
|
|
|
|
if sv.IsNil() {
|
|
|
|
return ErrCanNotIterateNilPointer
|
|
|
|
}
|
|
|
|
|
|
|
|
// solve the reference
|
|
|
|
st = st.Elem()
|
|
|
|
sv = sv.Elem()
|
|
|
|
|
|
|
|
if st.Kind() != reflect.Struct {
|
|
|
|
return fmt.Errorf("f should be a struct, %s given", st)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < sv.NumField(); i++ {
|
|
|
|
fv := sv.Field(i)
|
|
|
|
ft := st.Field(i)
|
|
|
|
|
|
|
|
// skip unexported fields
|
|
|
|
if !st.Field(i).IsExported() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:35:57 +00:00
|
|
|
if err := cb(ft, fv); err != nil {
|
2022-07-06 13:58:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-09 07:11:09 +00:00
|
|
|
func isStructPtr(tpe reflect.Type) bool {
|
|
|
|
return tpe.Kind() == reflect.Ptr && tpe.Elem().Kind() == reflect.Struct
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:49:42 +00:00
|
|
|
func IterateFieldsByTag(obj interface{}, tagName string, cb StructFieldIterator) error {
|
|
|
|
sv := reflect.ValueOf(obj)
|
|
|
|
st := reflect.TypeOf(obj)
|
|
|
|
|
|
|
|
if st.Kind() != reflect.Ptr {
|
|
|
|
return fmt.Errorf("f should be a pointer of a struct, %s given", st)
|
|
|
|
}
|
|
|
|
|
|
|
|
// for pointer, check if it's nil
|
|
|
|
if sv.IsNil() {
|
|
|
|
return ErrCanNotIterateNilPointer
|
|
|
|
}
|
|
|
|
|
|
|
|
// solve the reference
|
|
|
|
st = st.Elem()
|
|
|
|
sv = sv.Elem()
|
|
|
|
|
|
|
|
if st.Kind() != reflect.Struct {
|
|
|
|
return fmt.Errorf("f should be a struct, %s given", st)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < sv.NumField(); i++ {
|
|
|
|
fv := sv.Field(i)
|
|
|
|
ft := st.Field(i)
|
|
|
|
|
|
|
|
// skip unexported fields
|
|
|
|
if !st.Field(i).IsExported() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-07-09 07:11:09 +00:00
|
|
|
if isStructPtr(ft.Type) && !fv.IsNil() {
|
|
|
|
// recursive iterate the struct field
|
|
|
|
if err := IterateFieldsByTag(fv.Interface(), tagName, cb); err != nil {
|
|
|
|
return fmt.Errorf("unable to iterate struct fields over the type %v: %v", ft, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:49:42 +00:00
|
|
|
tag, ok := ft.Tag.Lookup(tagName)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-07-09 07:11:09 +00:00
|
|
|
// call the iterator
|
2022-06-29 10:49:42 +00:00
|
|
|
if err := cb(tag, ft, fv); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|