mirror of
https://github.com/hanzoai/expr.git
synced 2026-09-24 16:58:58 +00:00
Ignoring fields with struct tag expr:"-" and make "in" return false for unexported fields (#806)
* allow ignoring struct member using a - in expr struct tag * fix in operator * simplify logic * add regression test for #807 * fixes #807: make in operator return false for unexported struct fields --------- Co-authored-by: Anton Medvedev <[email protected]>
This commit is contained in:
committed by
GitHub
co-authored by
Anton Medvedev
parent
fefc347053
commit
13992b0526
+11
-3
@@ -65,10 +65,14 @@ func Fetch(from, i any) any {
|
||||
fieldName := i.(string)
|
||||
value := v.FieldByNameFunc(func(name string) bool {
|
||||
field, _ := v.Type().FieldByName(name)
|
||||
if field.Tag.Get("expr") == fieldName {
|
||||
switch field.Tag.Get("expr") {
|
||||
case "-":
|
||||
return false
|
||||
case fieldName:
|
||||
return true
|
||||
default:
|
||||
return name == fieldName
|
||||
}
|
||||
return name == fieldName
|
||||
})
|
||||
if value.IsValid() {
|
||||
return value.Interface()
|
||||
@@ -213,7 +217,11 @@ func In(needle any, array any) bool {
|
||||
if !n.IsValid() || n.Kind() != reflect.String {
|
||||
panic(fmt.Sprintf("cannot use %T as field name of %T", needle, array))
|
||||
}
|
||||
value := v.FieldByName(n.String())
|
||||
field, ok := v.Type().FieldByName(n.String())
|
||||
if !ok || !field.IsExported() || field.Tag.Get("expr") == "-" {
|
||||
return false
|
||||
}
|
||||
value := v.FieldByIndex(field.Index)
|
||||
if value.IsValid() {
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user