mirror of
https://github.com/hanzoai/expr.git
synced 2026-09-24 16:58:58 +00:00
Refactor getting field name
This commit is contained in:
+2
-2
@@ -216,7 +216,7 @@ func fetchType(t reflect.Type, name string) (reflect.Type, bool) {
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
f := t.Field(i)
|
||||
// Search all fields, even embedded structs.
|
||||
if fieldName(f) == name {
|
||||
if FieldName(f) == name {
|
||||
return f.Type, true
|
||||
}
|
||||
}
|
||||
@@ -290,7 +290,7 @@ func setTypeForIntegers(node ast.Node, t reflect.Type) {
|
||||
}
|
||||
}
|
||||
|
||||
func fieldName(field reflect.StructField) string {
|
||||
func FieldName(field reflect.StructField) string {
|
||||
if taggedName := field.Tag.Get("expr"); taggedName != "" {
|
||||
return taggedName
|
||||
}
|
||||
|
||||
+2
-9
@@ -1,6 +1,7 @@
|
||||
package conf
|
||||
|
||||
import (
|
||||
"github.com/antonmedv/expr/checker"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
@@ -84,7 +85,7 @@ func FieldsFromStruct(t reflect.Type) TypesTable {
|
||||
}
|
||||
}
|
||||
|
||||
types[actualFieldName(f)] = Tag{Type: f.Type}
|
||||
types[checker.FieldName(f)] = Tag{Type: f.Type}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,11 +101,3 @@ func dereference(t reflect.Type) reflect.Type {
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func actualFieldName(f reflect.StructField) string {
|
||||
if taggedName := f.Tag.Get("expr"); taggedName != "" {
|
||||
return taggedName
|
||||
}
|
||||
|
||||
return f.Name
|
||||
}
|
||||
|
||||
@@ -63,8 +63,14 @@ func Fetch(from, i interface{}) interface{} {
|
||||
}
|
||||
|
||||
case reflect.Struct:
|
||||
fieldName := reflect.ValueOf(i).String()
|
||||
value := v.FieldByName(fieldName)
|
||||
fieldName := i.(string)
|
||||
value := v.FieldByNameFunc(func(name string) bool {
|
||||
field, _ := v.Type().FieldByName(name)
|
||||
if field.Tag.Get("expr") == fieldName {
|
||||
return true
|
||||
}
|
||||
return name == fieldName
|
||||
})
|
||||
if value.IsValid() && value.CanInterface() {
|
||||
return value.Interface()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user