mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-26 11:01:07 +01:00
fix serialization, added benchmark tests, updated next release docs
This commit is contained in:
@@ -204,8 +204,13 @@ func Deserialize(src SerializedObject, dst any) E.Error {
|
||||
}
|
||||
if hasValidateTag {
|
||||
errs.Add(ValidateWithFieldTags(dstV.Interface()))
|
||||
} else if validator, ok := dstV.Addr().Interface().(CustomValidator); ok {
|
||||
errs.Add(validator.Validate())
|
||||
} else {
|
||||
if dstV.CanAddr() {
|
||||
dstV = dstV.Addr()
|
||||
}
|
||||
if validator, ok := dstV.Interface().(CustomValidator); ok {
|
||||
errs.Add(validator.Validate())
|
||||
}
|
||||
}
|
||||
return errs.Error()
|
||||
case reflect.Map:
|
||||
@@ -222,7 +227,10 @@ func Deserialize(src SerializedObject, dst any) E.Error {
|
||||
errs.Add(err.Subject(k))
|
||||
}
|
||||
}
|
||||
if validator, ok := dstV.Addr().Interface().(CustomValidator); ok {
|
||||
if dstV.CanAddr() {
|
||||
dstV = dstV.Addr()
|
||||
}
|
||||
if validator, ok := dstV.Interface().(CustomValidator); ok {
|
||||
errs.Add(validator.Validate())
|
||||
}
|
||||
return errs.Error()
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"testing"
|
||||
|
||||
. "github.com/yusing/go-proxy/internal/utils/testing"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func TestDeserialize(t *testing.T) {
|
||||
@@ -187,6 +188,20 @@ func TestStringToSlice(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkStringToSlice(b *testing.B) {
|
||||
for range b.N {
|
||||
dst := make([]int, 0)
|
||||
_, _ = ConvertString("- 1\n- 2\n- 3", reflect.ValueOf(&dst))
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStringToSliceYAML(b *testing.B) {
|
||||
for range b.N {
|
||||
dst := make([]int, 0)
|
||||
_ = yaml.Unmarshal([]byte("- 1\n- 2\n- 3"), &dst)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringToMap(t *testing.T) {
|
||||
t.Run("yaml-like", func(t *testing.T) {
|
||||
dst := make(map[string]string)
|
||||
@@ -197,6 +212,20 @@ func TestStringToMap(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkStringToMap(b *testing.B) {
|
||||
for range b.N {
|
||||
dst := make(map[string]string)
|
||||
_, _ = ConvertString(" a: b\n c: d", reflect.ValueOf(&dst))
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStringToMapYAML(b *testing.B) {
|
||||
for range b.N {
|
||||
dst := make(map[string]string)
|
||||
_ = yaml.Unmarshal([]byte(" a: b\n c: d"), &dst)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringToStruct(t *testing.T) {
|
||||
t.Run("yaml-like", func(t *testing.T) {
|
||||
dst := struct {
|
||||
@@ -212,3 +241,23 @@ func TestStringToStruct(t *testing.T) {
|
||||
}{"a", 123})
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkStringToStruct(b *testing.B) {
|
||||
for range b.N {
|
||||
dst := struct {
|
||||
A string `json:"a"`
|
||||
B int `json:"b"`
|
||||
}{}
|
||||
_, _ = ConvertString(" a: a\n b: 123", reflect.ValueOf(&dst))
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStringToStructYAML(b *testing.B) {
|
||||
for range b.N {
|
||||
dst := struct {
|
||||
A string `yaml:"a"`
|
||||
B int `yaml:"b"`
|
||||
}{}
|
||||
_ = yaml.Unmarshal([]byte(" a: a\n b: 123"), &dst)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,3 +36,27 @@ func TestSplit(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSplitRune(b *testing.B) {
|
||||
for range b.N {
|
||||
SplitRune(alphaNumeric, ',')
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSplitRuneStdlib(b *testing.B) {
|
||||
for range b.N {
|
||||
strings.Split(alphaNumeric, ",")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJoinRune(b *testing.B) {
|
||||
for range b.N {
|
||||
JoinRune(SplitRune(alphaNumeric, ','), ',')
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkJoinRuneStdlib(b *testing.B) {
|
||||
for range b.N {
|
||||
strings.Join(SplitRune(alphaNumeric, ','), ",")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user