mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-12-15 23:26:36 +08:00
实现WAF
This commit is contained in:
77
internal/utils/get.go
Normal file
77
internal/utils/get.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"reflect"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var RegexpDigitNumber = regexp.MustCompile("^\\d+$")
|
||||
|
||||
func Get(object interface{}, keys []string) interface{} {
|
||||
if len(keys) == 0 {
|
||||
return object
|
||||
}
|
||||
|
||||
if object == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
firstKey := keys[0]
|
||||
keys = keys[1:]
|
||||
|
||||
value := reflect.ValueOf(object)
|
||||
|
||||
if !value.IsValid() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if value.Kind() == reflect.Ptr {
|
||||
value = value.Elem()
|
||||
}
|
||||
|
||||
if value.Kind() == reflect.Struct {
|
||||
field := value.FieldByName(firstKey)
|
||||
if !field.IsValid() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(keys) == 0 {
|
||||
return field.Interface()
|
||||
}
|
||||
|
||||
return Get(field.Interface(), keys)
|
||||
}
|
||||
|
||||
if value.Kind() == reflect.Map {
|
||||
mapKey := reflect.ValueOf(firstKey)
|
||||
mapValue := value.MapIndex(mapKey)
|
||||
if !mapValue.IsValid() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(keys) == 0 {
|
||||
return mapValue.Interface()
|
||||
}
|
||||
|
||||
return Get(mapValue.Interface(), keys)
|
||||
}
|
||||
|
||||
if value.Kind() == reflect.Slice {
|
||||
if RegexpDigitNumber.MatchString(firstKey) {
|
||||
firstKeyInt := types.Int(firstKey)
|
||||
if value.Len() > firstKeyInt {
|
||||
result := value.Index(firstKeyInt).Interface()
|
||||
if len(keys) == 0 {
|
||||
return result
|
||||
}
|
||||
|
||||
return Get(result, keys)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user