mirror of
https://github.com/yusing/godoxy.git
synced 2025-06-07 03:52: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
|
package utils
|
||||||
|
|
||||||
|
// FIXME: some times [%d] is not in correct order
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
@ -277,17 +279,22 @@ 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())
|
||||||
}
|
}
|
||||||
|
sliceErrs := E.NewBuilder("slice conversion errors")
|
||||||
newSlice := reflect.MakeSlice(dstT, src.Len(), 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()
|
||||||
err := Convert(v, tmp)
|
err := Convert(v, tmp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Subjectf("[%d]", i)
|
sliceErrs.Add(err.Subjectf("[%d]", i))
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
newSlice.Index(i).Set(tmp)
|
newSlice.Index(i).Set(tmp)
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
if err := sliceErrs.Error(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
dst.Set(newSlice)
|
dst.Set(newSlice)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -166,16 +166,9 @@ func TestStringToSlice(t *testing.T) {
|
||||||
ExpectNoError(t, err)
|
ExpectNoError(t, err)
|
||||||
ExpectDeepEqual(t, dst, []string{"a", "b", "c"})
|
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) {
|
t.Run("yaml-like", 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