mirror of
https://github.com/yusing/godoxy.git
synced 2025-07-05 22:24:02 +02:00
fix fields not being validated (introduced in 577a536
), drop support of list string not starting with hyphen
This commit is contained in:
parent
7fe03be73f
commit
8bbb5d2e09
2 changed files with 10 additions and 15 deletions
|
@ -9,7 +9,6 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
|
||||||
|
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
E "github.com/yusing/go-proxy/internal/error"
|
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)
|
mapping[key] = dstV.FieldByName(field.Name)
|
||||||
fieldName[field.Name] = key
|
fieldName[field.Name] = key
|
||||||
|
|
||||||
_, needValidate = field.Tag.Lookup("validate")
|
if !needValidate {
|
||||||
|
_, needValidate = field.Tag.Lookup("validate")
|
||||||
|
}
|
||||||
|
|
||||||
aliases, ok := field.Tag.Lookup("aliases")
|
aliases, ok := field.Tag.Lookup("aliases")
|
||||||
if ok {
|
if ok {
|
||||||
|
@ -276,7 +277,7 @@ func Convert(src reflect.Value, dst reflect.Value) E.Error {
|
||||||
if dstT.Kind() != reflect.Slice {
|
if dstT.Kind() != reflect.Slice {
|
||||||
return ErrUnsupportedConversion.Subject(dstT.String() + " to " + srcT.String())
|
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
|
i := 0
|
||||||
for _, v := range src.Seq2() {
|
for _, v := range src.Seq2() {
|
||||||
tmp := New(dstT.Elem()).Elem()
|
tmp := New(dstT.Elem()).Elem()
|
||||||
|
@ -284,7 +285,7 @@ func Convert(src reflect.Value, dst reflect.Value) E.Error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Subjectf("[%d]", i)
|
return err.Subjectf("[%d]", i)
|
||||||
}
|
}
|
||||||
newSlice = reflect.Append(newSlice, tmp)
|
newSlice.Index(i).Set(tmp)
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
dst.Set(newSlice)
|
dst.Set(newSlice)
|
||||||
|
@ -365,16 +366,10 @@ func ConvertString(src string, dst reflect.Value) (convertible bool, convErr E.E
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
lines := strutils.SplitLine(src)
|
sl := make([]any, 0)
|
||||||
sl := make([]string, 0, len(lines))
|
err := yaml.Unmarshal([]byte(src), &sl)
|
||||||
for _, line := range lines {
|
if err != nil {
|
||||||
line = strings.TrimLeftFunc(line, func(r rune) bool {
|
return true, E.From(err)
|
||||||
return r == '-' || unicode.IsSpace(r)
|
|
||||||
})
|
|
||||||
if line == "" || line[0] == '#' {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
sl = append(sl, line)
|
|
||||||
}
|
}
|
||||||
tmp = sl
|
tmp = sl
|
||||||
case reflect.Map, reflect.Struct:
|
case reflect.Map, reflect.Struct:
|
||||||
|
|
|
@ -168,7 +168,7 @@ func TestStringToSlice(t *testing.T) {
|
||||||
})
|
})
|
||||||
t.Run("multiline", func(t *testing.T) {
|
t.Run("multiline", func(t *testing.T) {
|
||||||
dst := make([]string, 0)
|
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)
|
ExpectTrue(t, convertible)
|
||||||
ExpectNoError(t, err)
|
ExpectNoError(t, err)
|
||||||
ExpectDeepEqual(t, dst, []string{"a", "b", "c"})
|
ExpectDeepEqual(t, dst, []string{"a", "b", "c"})
|
||||||
|
|
Loading…
Add table
Reference in a new issue