mirror of
https://github.com/yusing/godoxy.git
synced 2025-06-05 19:22:34 +02:00
fix: slice deserialization should return all errors
This commit is contained in:
parent
3dfa2d08fd
commit
8ad7bb9e0c
2 changed files with 9 additions and 9 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -166,16 +166,9 @@ func TestStringToSlice(t *testing.T) {
|
|||
ExpectNoError(t, err)
|
||||
ExpectDeepEqual(t, dst, []string{"a", "b", "c"})
|
||||
})
|
||||
t.Run("multiline", func(t *testing.T) {
|
||||
dst := make([]string, 0)
|
||||
convertible, err := ConvertString("- a\n- b\n- c", reflect.ValueOf(&dst))
|
||||
ExpectTrue(t, convertible)
|
||||
ExpectNoError(t, err)
|
||||
ExpectDeepEqual(t, dst, []string{"a", "b", "c"})
|
||||
})
|
||||
t.Run("yaml-like", 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"})
|
||||
|
|
Loading…
Add table
Reference in a new issue