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