|
@@ -12,6 +12,7 @@ import (
|
|
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
+
|
|
"github.com/zeromicro/go-zero/core/stringx"
|
|
"github.com/zeromicro/go-zero/core/stringx"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -4384,3 +4385,44 @@ func BenchmarkUnmarshal(b *testing.B) {
|
|
UnmarshalKey(data, &an)
|
|
UnmarshalKey(data, &an)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+func TestFillDefaultUnmarshal(t *testing.T) {
|
|
|
|
+ fillDefaultUnmarshal := NewUnmarshaler(jsonTagKey, WithDefault())
|
|
|
|
+ t.Run("nil", func(t *testing.T) {
|
|
|
|
+ type St struct{}
|
|
|
|
+ err := fillDefaultUnmarshal.Unmarshal(map[string]any{}, St{})
|
|
|
|
+ assert.Error(t, err)
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ t.Run("not nil", func(t *testing.T) {
|
|
|
|
+ type St struct{}
|
|
|
|
+ err := fillDefaultUnmarshal.Unmarshal(map[string]any{}, &St{})
|
|
|
|
+ assert.NoError(t, err)
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ t.Run("default", func(t *testing.T) {
|
|
|
|
+ type St struct {
|
|
|
|
+ A string `json:",default=a"`
|
|
|
|
+ B string
|
|
|
|
+ }
|
|
|
|
+ var st St
|
|
|
|
+ err := fillDefaultUnmarshal.Unmarshal(map[string]any{}, &st)
|
|
|
|
+ assert.NoError(t, err)
|
|
|
|
+ assert.Equal(t, st.A, "a")
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ t.Run("env", func(t *testing.T) {
|
|
|
|
+ type St struct {
|
|
|
|
+ A string `json:",default=a"`
|
|
|
|
+ B string
|
|
|
|
+ C string `json:",env=TEST_C"`
|
|
|
|
+ }
|
|
|
|
+ t.Setenv("TEST_C", "c")
|
|
|
|
+
|
|
|
|
+ var st St
|
|
|
|
+ err := fillDefaultUnmarshal.Unmarshal(map[string]any{}, &st)
|
|
|
|
+ assert.NoError(t, err)
|
|
|
|
+ assert.Equal(t, st.A, "a")
|
|
|
|
+ assert.Equal(t, st.C, "c")
|
|
|
|
+ })
|
|
|
|
+}
|