update:simplify slice lookup by using slices.Contains() (#5084)

Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
me-cs
2025-08-14 23:33:25 +08:00
committed by GitHub
parent 94562ded74
commit f0a3d213dc

View File

@@ -3,6 +3,7 @@ package mapping
import (
"fmt"
"reflect"
"slices"
"strings"
)
@@ -152,18 +153,10 @@ func validateOptional(field reflect.StructField, value reflect.Value) error {
}
func validateOptions(value reflect.Value, opt *fieldOptions) error {
var found bool
val := fmt.Sprint(value.Interface())
for i := range opt.Options {
if opt.Options[i] == val {
found = true
break
}
}
if !found {
if !slices.Contains(opt.Options, val) {
return fmt.Errorf("field %q not in options", val)
}
return nil
}