mirror of
https://github.com/kataras/iris.git
synced 2025-12-20 03:17:04 +00:00
add a new x/reflex sub-package for reflection common functions - may be improved in the near future
This commit is contained in:
56
x/reflex/struct.go
Normal file
56
x/reflex/struct.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package reflex
|
||||
|
||||
import "reflect"
|
||||
|
||||
// LookupFields returns a slice of all fields containg a struct field
|
||||
// of the given "fieldTag" of the "typ" struct. The fields returned
|
||||
// are flatted and reclusive over fields with value of struct.
|
||||
// Panics if "typ" is not a type of Struct.
|
||||
func LookupFields(typ reflect.Type, fieldTag string) []reflect.StructField {
|
||||
fields := lookupFields(typ, fieldTag, nil)
|
||||
return fields[0:len(fields):len(fields)]
|
||||
}
|
||||
|
||||
func lookupFields(typ reflect.Type, fieldTag string, parentIndex []int) []reflect.StructField {
|
||||
n := typ.NumField()
|
||||
fields := make([]reflect.StructField, 0, n)
|
||||
checkTag := fieldTag != ""
|
||||
for i := 0; i < n; i++ {
|
||||
field := typ.Field(i)
|
||||
if field.PkgPath != "" { // skip unexported fields.
|
||||
continue
|
||||
}
|
||||
|
||||
if checkTag {
|
||||
if v := field.Tag.Get(fieldTag); v == "" || v == "-" {
|
||||
// Skip fields that don't contain the 'fieldTag' tag or has '-'.
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
fieldType := IndirectType(field.Type)
|
||||
|
||||
if fieldType.Kind() == reflect.Struct { // It's a struct inside a struct and it's not time, flat it.
|
||||
if fieldType != TimeType {
|
||||
structFields := lookupFields(fieldType, fieldTag, append(parentIndex, i))
|
||||
if nn := len(structFields); nn > 0 {
|
||||
fields = append(fields, structFields...)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
index := []int{i}
|
||||
if len(parentIndex) > 0 {
|
||||
index = append(parentIndex, i)
|
||||
}
|
||||
|
||||
tmp := make([]int, len(index))
|
||||
copy(tmp, index)
|
||||
field.Index = tmp
|
||||
|
||||
fields = append(fields, field)
|
||||
}
|
||||
|
||||
return fields
|
||||
}
|
||||
Reference in New Issue
Block a user