fix: slice deserialization should return all errors

This commit is contained in:
yusing
2025-01-11 01:39:03 +08:00
parent e7bb6bc798
commit 28b5d44e11
2 changed files with 9 additions and 9 deletions

View File

@@ -1,5 +1,7 @@
package utils
// FIXME: some times [%d] is not in correct order
import (
"encoding/json"
"errors"
@@ -277,17 +279,22 @@ func Convert(src reflect.Value, dst reflect.Value) E.Error {
if dstT.Kind() != reflect.Slice {
return ErrUnsupportedConversion.Subject(dstT.String() + " to " + srcT.String())
}
sliceErrs := E.NewBuilder("slice conversion errors")
newSlice := reflect.MakeSlice(dstT, src.Len(), src.Len())
i := 0
for _, v := range src.Seq2() {
tmp := New(dstT.Elem()).Elem()
err := Convert(v, tmp)
if err != nil {
return err.Subjectf("[%d]", i)
sliceErrs.Add(err.Subjectf("[%d]", i))
continue
}
newSlice.Index(i).Set(tmp)
i++
}
if err := sliceErrs.Error(); err != nil {
return err
}
dst.Set(newSlice)
return nil
}