mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-19 23:11:25 +02:00
fix deserialization panics on empty map
This commit is contained in:
@@ -277,6 +277,18 @@ func isIntFloat(t reflect.Kind) bool {
|
||||
// Returns:
|
||||
// - error: the error occurred during conversion, or nil if no error occurred.
|
||||
func Convert(src reflect.Value, dst reflect.Value) E.Error {
|
||||
if !dst.IsValid() {
|
||||
return E.Errorf("convert: dst is %w", ErrNilValue)
|
||||
}
|
||||
|
||||
if !src.IsValid() {
|
||||
if dst.CanSet() {
|
||||
dst.Set(reflect.Zero(dst.Type()))
|
||||
return nil
|
||||
}
|
||||
return E.Errorf("convert: src is %w", ErrNilValue)
|
||||
}
|
||||
|
||||
srcT := src.Type()
|
||||
dstT := dst.Type()
|
||||
|
||||
@@ -339,7 +351,7 @@ func Convert(src reflect.Value, dst reflect.Value) E.Error {
|
||||
return nil
|
||||
}
|
||||
if dstT.Kind() != reflect.Slice {
|
||||
return ErrUnsupportedConversion.Subject(dstT.String() + " to slice")
|
||||
return ErrUnsupportedConversion.Subject(dstT.String() + " to " + srcT.String())
|
||||
}
|
||||
newSlice := reflect.MakeSlice(dstT, 0, src.Len())
|
||||
i := 0
|
||||
@@ -469,11 +481,10 @@ func ConvertString(src string, dst reflect.Value) (convertible bool, convErr E.E
|
||||
return true, errs.Error()
|
||||
}
|
||||
tmp = m
|
||||
default:
|
||||
return false, nil
|
||||
}
|
||||
if tmp != nil {
|
||||
return true, Convert(reflect.ValueOf(tmp), dst)
|
||||
}
|
||||
return false, nil
|
||||
return true, Convert(reflect.ValueOf(tmp), dst)
|
||||
}
|
||||
|
||||
func DeserializeYAML[T any](data []byte, target T) E.Error {
|
||||
|
||||
@@ -23,7 +23,7 @@ func IgnoreError[Result any](r Result, _ error) Result {
|
||||
func ExpectNoError(t *testing.T, err error) {
|
||||
t.Helper()
|
||||
if err != nil && !reflect.ValueOf(err).IsNil() {
|
||||
t.Errorf("expected err=nil, got %v", err)
|
||||
t.Errorf("expected err=nil, got %s", ansi.StripANSI(err.Error()))
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,7 @@ func ExpectNoError(t *testing.T, err error) {
|
||||
func ExpectError(t *testing.T, expected error, err error) {
|
||||
t.Helper()
|
||||
if !errors.Is(err, expected) {
|
||||
t.Errorf("expected err %s, got %v", expected, err)
|
||||
t.Errorf("expected err %s, got %s", expected, ansi.StripANSI(err.Error()))
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,7 @@ func ExpectError(t *testing.T, expected error, err error) {
|
||||
func ExpectError2(t *testing.T, input any, expected error, err error) {
|
||||
t.Helper()
|
||||
if !errors.Is(err, expected) {
|
||||
t.Errorf("%v: expected err %s, got %v", input, expected, err)
|
||||
t.Errorf("%v: expected err %s, got %s", input, expected, ansi.StripANSI(err.Error()))
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
@@ -48,22 +48,17 @@ func ExpectErrorT[T error](t *testing.T, err error) {
|
||||
t.Helper()
|
||||
var errAs T
|
||||
if !errors.As(err, &errAs) {
|
||||
t.Errorf("expected err %T, got %v", errAs, err)
|
||||
t.Errorf("expected err %T, got %s", errAs, ansi.StripANSI(err.Error()))
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func ExpectEqual[T comparable](t *testing.T, got T, want T) {
|
||||
t.Helper()
|
||||
if got != want {
|
||||
t.Errorf("expected:\n%v, got\n%v", want, got)
|
||||
t.FailNow()
|
||||
if gotStr, ok := any(got).(string); ok {
|
||||
ExpectDeepEqual(t, ansi.StripANSI(gotStr), any(want).(string))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func ExpectStrEqual(t *testing.T, got string, want string) {
|
||||
t.Helper()
|
||||
got = ansi.StripANSI(got)
|
||||
if got != want {
|
||||
t.Errorf("expected:\n%v, got\n%v", want, got)
|
||||
t.FailNow()
|
||||
|
||||
Reference in New Issue
Block a user