Skip to content

Commit 311a48a

Browse files
committed
feat: add test case for default value
1 parent affcbe0 commit 311a48a

9 files changed

+191
-17
lines changed

registry.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,7 @@ func (k *Kod) get(ctx context.Context, reg *Registration) (any, error) {
9393
// Fill global config.
9494
if c, ok := obj.(interface{ getGlobalConfig() any }); ok {
9595
if cfg := c.getGlobalConfig(); cfg != nil {
96-
err := defaults.Set(cfg)
97-
if err != nil {
98-
return nil, err
99-
}
100-
101-
err = k.viper.Unmarshal(cfg)
96+
err := k.setConfig("", cfg)
10297
if err != nil {
10398
return nil, err
10499
}
@@ -108,12 +103,7 @@ func (k *Kod) get(ctx context.Context, reg *Registration) (any, error) {
108103
// Fill config.
109104
if c, ok := obj.(interface{ getConfig() any }); ok {
110105
if cfg := c.getConfig(); cfg != nil {
111-
err := defaults.Set(cfg)
112-
if err != nil {
113-
return nil, err
114-
}
115-
116-
err = k.viper.UnmarshalKey(reg.Name, cfg)
106+
err := k.setConfig(reg.Name, cfg)
117107
if err != nil {
118108
return nil, err
119109
}
@@ -155,6 +145,15 @@ func (k *Kod) get(ctx context.Context, reg *Registration) (any, error) {
155145
return obj, nil
156146
}
157147

148+
func (k *Kod) setConfig(name string, cfg any) error {
149+
err := defaults.Set(cfg)
150+
if err != nil {
151+
return err
152+
}
153+
154+
return k.viper.UnmarshalKey(name, cfg)
155+
}
156+
158157
func fillLog(name string, obj any, log *slog.Logger) error {
159158
x, ok := obj.(interface{ setLogger(string, *slog.Logger) })
160159
if !ok {

tests/case1/case.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import (
2424
)
2525

2626
type test1Config struct {
27-
A string
27+
A string `default:"a"`
2828
Redis struct {
29-
Addr string
30-
Timeout time.Duration
29+
Addr string `default:"localhost:6379"`
30+
Timeout time.Duration `default:"1s"`
3131
}
3232
}
3333

tests/case1/case_default_config.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package case1
2+
3+
import (
4+
"github.com/go-kod/kod"
5+
)
6+
7+
type errorConfig struct {
8+
A int `default:"sss"`
9+
}
10+
11+
type test1ComponentDefaultErrorImpl struct {
12+
kod.Implements[test1ComponentDefaultError]
13+
kod.WithConfig[*errorConfig]
14+
}
15+
16+
type test1ComponentGlobalDefaultErrorImpl struct {
17+
kod.Implements[test1ComponentGlobalDefaultError]
18+
kod.WithGlobalConfig[*errorConfig]
19+
}
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package case1
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/go-kod/kod"
8+
"github.com/go-kod/kod/internal/mock"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestDefaultConfig(t *testing.T) {
13+
log, observer := kod.NewTestLogger()
14+
15+
kod.RunTest(t, func(ctx context.Context, k *test1Component) {
16+
observer.Clean()
17+
18+
k.L(ctx).Info("hello", "config", k.Config())
19+
20+
assert.Equal(t, 1, observer.Len())
21+
assert.Equal(t, "{\"component\":\"github.com/go-kod/kod/tests/case1/Test1Component\",\"config\":{\"A\":\"B\",\"Redis\":{\"Addr\":\"localhost:6379\",\"Timeout\":2000000000}},\"level\":\"INFO\",\"msg\":\"hello\"}\n",
22+
observer.RemoveKeys("time").String())
23+
}, kod.WithLogger(log))
24+
}
25+
26+
func TestDefaultConfig2(t *testing.T) {
27+
log, observer := kod.NewTestLogger()
28+
29+
kod.RunTest(t, func(ctx context.Context, k *test1Component) {
30+
observer.Clean()
31+
32+
k.L(ctx).Info("hello", "config", k.Config())
33+
34+
assert.Equal(t, 1, observer.Len())
35+
assert.Equal(t, "{\"component\":\"github.com/go-kod/kod/tests/case1/Test1Component\",\"config\":{\"A\":\"B2\",\"Redis\":{\"Addr\":\"localhost:6379\",\"Timeout\":1000000000}},\"level\":\"INFO\",\"msg\":\"hello\"}\n",
36+
observer.RemoveKeys("time").String())
37+
}, kod.WithLogger(log), kod.WithConfigFile("./kod2.toml"))
38+
}
39+
40+
func TestDefaultConfigError(t *testing.T) {
41+
mock.ExpectFailure(t, func(tb testing.TB) {
42+
kod.RunTest(tb, func(ctx context.Context, k *test1ComponentDefaultErrorImpl) {
43+
k.L(ctx).Info("hello", "config", k.Config())
44+
}, kod.WithConfigFile("./kod2.toml"))
45+
})
46+
}
47+
48+
func TestDefaultGlobalConfigError(t *testing.T) {
49+
mock.ExpectFailure(t, func(tb testing.TB) {
50+
kod.RunTest(tb, func(ctx context.Context, k *test1ComponentGlobalDefaultErrorImpl) {
51+
k.L(ctx).Info("hello", "config", k.Config())
52+
}, kod.WithConfigFile("./kod2.toml"))
53+
})
54+
}

tests/case1/case_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func TestConfigNotFound(t *testing.T) {
159159
kod.RunTest(t, func(ctx context.Context, k *test1Component) {
160160
_, err := k.Foo(ctx, &FooReq{})
161161
fmt.Println(err)
162-
require.Equal(t, "test1:", err.Error())
162+
require.Equal(t, "test1:a", err.Error())
163163
}, kod.WithConfigFile("kod-notfound.toml"))
164164
}
165165

tests/case1/kod2.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ debug = true
55

66
["github.com/go-kod/kod/tests/case1/Test1Component"]
77
A="B2"
8-
redis = { addr = "localhost:6379", timeout = "2s" }
8+
redis = { addr = "localhost:6379" }

tests/case1/kod_gen.go

+44
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/case1/kod_gen_interface.go

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/case1/kod_gen_mock.go

+48
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)