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:
Diego Augusto Molina
2025-06-30 20:14:08 +02:00
committed by GitHub
co-authored by Anton Medvedev
parent fefc347053
commit 13992b0526
4 changed files with 130 additions and 13 deletions
+11 -3
View File
@@ -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
}