-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexample_test.go
157 lines (141 loc) · 3.38 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package core_test
import (
"context"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
"github.com/DoNewsCode/core"
"github.com/DoNewsCode/core/config"
"github.com/DoNewsCode/core/contract"
"github.com/DoNewsCode/core/di"
"github.com/go-kit/log"
"github.com/gorilla/mux"
"github.com/knadh/koanf/parsers/json"
"github.com/knadh/koanf/providers/basicflag"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
)
func ExampleC_AddModuleFunc() {
type Foo struct{}
c := core.New()
c.AddModuleFunc(func() (Foo, func(), error) {
return Foo{}, func() {}, nil
})
fmt.Printf("%T %T\n", c.Modules()...)
// Output:
// core.cleanup core_test.Foo
}
func ExampleC_AddModule_simple() {
type Foo struct{}
c := core.New()
c.AddModule(Foo{})
fmt.Printf("%T\n", c.Modules()...)
// Output:
// core_test.Foo
}
func ExampleC_AddModule_inject() {
type Foo struct {
di.In
Logger log.Logger
}
var f Foo
c := core.New()
c.Provide(di.Deps{func() log.Logger {
return log.NewLogfmtLogger(os.Stdout)
}})
// If the module embeds di.In (directly or indirectly via pointer), then DI will
// populate its fields.
c.AddModule(&f)
f.Logger.Log("msg", "hello")
// Output:
// msg=hello
}
func ExampleC_Provide() {
type Foo struct {
Value string
}
type Bar struct {
foo Foo
}
c := core.New()
c.Provide(di.Deps{
func() (foo Foo, cleanup func(), err error) {
return Foo{
Value: "test",
}, func() {}, nil
},
func(foo Foo) Bar {
return Bar{foo: foo}
},
})
c.Invoke(func(bar Bar) {
fmt.Println(bar.foo.Value)
})
// Output:
// test
}
func Example_minimal() {
// Spin up a real server
c := core.Default(core.WithInline("log.level", "none"))
c.AddModule(core.HttpFunc(func(router *mux.Router) {
router.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
writer.Write([]byte("hello world"))
})
}))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go c.Serve(ctx)
// Giver server sometime to be ready.
time.Sleep(time.Second)
// Let's try if the server works.
resp, err := http.Get("http://localhost:8080/")
if err != nil {
return
}
defer resp.Body.Close()
bytes, _ := ioutil.ReadAll(resp.Body)
cancel()
fmt.Println(string(bytes))
// Output:
// hello world
}
func ExampleC_stack() {
fs := flag.NewFlagSet("example", flag.ContinueOnError)
fs.String("log.level", "error", "the log level")
// Spin up a real server
c := core.New(
core.WithConfigStack(basicflag.Provider(fs, "."), nil),
core.WithConfigStack(env.Provider("APP_", ".", func(s string) string {
return strings.ToLower(strings.Replace(s, "APP_", "", 1))
}), nil),
core.WithConfigStack(file.Provider("./config/testdata/mock.json"), json.Parser()),
)
c.AddModule(core.HttpFunc(func(router *mux.Router) {
router.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
writer.Write([]byte("hello world"))
})
}))
c.Serve(context.Background())
}
func ExampleC_decorate() {
c := core.Default(core.WithInline("log.level", "none"), core.WithInline("env", "development"))
type MyClient struct {
env contract.Env
}
provideMyClient := func(env2 contract.Env) MyClient {
return MyClient{env: env2}
}
c.Provide(di.Deps{provideMyClient})
c.Decorate(func(_ contract.Env) contract.Env {
return config.NewEnv("testing")
})
var m MyClient
c.Populate(&m)
fmt.Println(m.env)
// Output:
// testing
}