fixed slice deserialization

This commit is contained in:
yusing 2024-12-17 10:32:20 +08:00
parent 0aa00ab226
commit 0995c8b839

View file

@ -244,8 +244,12 @@ func Deserialize(src SerializedObject, dst any) E.Error {
if e.Param() != "" { if e.Param() != "" {
detail += ":" + e.Param() detail += ":" + e.Param()
} }
fieldName, ok := fieldName[e.Field()]
if !ok {
fieldName = e.Field()
}
errs.Add(ErrValidationError. errs.Add(ErrValidationError.
Subject(fieldName[e.Field()]). Subject(fieldName).
Withf("require %q", detail)) Withf("require %q", detail))
} }
} }
@ -424,8 +428,19 @@ func ConvertString(src string, dst reflect.Value) (convertible bool, convErr E.E
switch dst.Kind() { switch dst.Kind() {
case reflect.Slice: case reflect.Slice:
// one liner is comma separated list // one liner is comma separated list
if len(lines) == 0 { if len(lines) == 1 {
dst.Set(reflect.ValueOf(strutils.CommaSeperatedList(src))) values := strutils.CommaSeperatedList(src)
dst.Set(reflect.MakeSlice(dst.Type(), len(values), len(values)))
errs := E.NewBuilder("invalid slice values")
for i, v := range values {
err := Convert(reflect.ValueOf(v), dst.Index(i))
if err != nil {
errs.Add(err.Subjectf("[%d]", i))
}
}
if errs.HasError() {
return true, errs.Error()
}
return return
} }
sl := make([]string, 0, len(lines)) sl := make([]string, 0, len(lines))