From 8bbb5d2e091a675d91808100393da6e0a75cd633 Mon Sep 17 00:00:00 2001 From: yusing Date: Thu, 9 Jan 2025 04:21:32 +0800 Subject: [PATCH] fix fields not being validated (introduced in 577a536), drop support of list string not starting with hyphen --- internal/utils/serialization.go | 23 +++++++++-------------- internal/utils/serialization_test.go | 2 +- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/internal/utils/serialization.go b/internal/utils/serialization.go index 2e1c9b4..5c09eff 100644 --- a/internal/utils/serialization.go +++ b/internal/utils/serialization.go @@ -9,7 +9,6 @@ import ( "strconv" "strings" "time" - "unicode" "github.com/go-playground/validator/v10" E "github.com/yusing/go-proxy/internal/error" @@ -127,7 +126,9 @@ func Deserialize(src SerializedObject, dst any) E.Error { mapping[key] = dstV.FieldByName(field.Name) fieldName[field.Name] = key - _, needValidate = field.Tag.Lookup("validate") + if !needValidate { + _, needValidate = field.Tag.Lookup("validate") + } aliases, ok := field.Tag.Lookup("aliases") if ok { @@ -276,7 +277,7 @@ func Convert(src reflect.Value, dst reflect.Value) E.Error { if dstT.Kind() != reflect.Slice { return ErrUnsupportedConversion.Subject(dstT.String() + " to " + srcT.String()) } - newSlice := reflect.MakeSlice(dstT, 0, src.Len()) + newSlice := reflect.MakeSlice(dstT, src.Len(), src.Len()) i := 0 for _, v := range src.Seq2() { tmp := New(dstT.Elem()).Elem() @@ -284,7 +285,7 @@ func Convert(src reflect.Value, dst reflect.Value) E.Error { if err != nil { return err.Subjectf("[%d]", i) } - newSlice = reflect.Append(newSlice, tmp) + newSlice.Index(i).Set(tmp) i++ } dst.Set(newSlice) @@ -365,16 +366,10 @@ func ConvertString(src string, dst reflect.Value) (convertible bool, convErr E.E } return } - lines := strutils.SplitLine(src) - sl := make([]string, 0, len(lines)) - for _, line := range lines { - line = strings.TrimLeftFunc(line, func(r rune) bool { - return r == '-' || unicode.IsSpace(r) - }) - if line == "" || line[0] == '#' { - continue - } - sl = append(sl, line) + sl := make([]any, 0) + err := yaml.Unmarshal([]byte(src), &sl) + if err != nil { + return true, E.From(err) } tmp = sl case reflect.Map, reflect.Struct: diff --git a/internal/utils/serialization_test.go b/internal/utils/serialization_test.go index 2b31f11..b45d95c 100644 --- a/internal/utils/serialization_test.go +++ b/internal/utils/serialization_test.go @@ -168,7 +168,7 @@ func TestStringToSlice(t *testing.T) { }) t.Run("multiline", func(t *testing.T) { dst := make([]string, 0) - convertible, err := ConvertString(" a\n b\n c", reflect.ValueOf(&dst)) + convertible, err := ConvertString("- a\n- b\n- c", reflect.ValueOf(&dst)) ExpectTrue(t, convertible) ExpectNoError(t, err) ExpectDeepEqual(t, dst, []string{"a", "b", "c"})