-
Notifications
You must be signed in to change notification settings - Fork 0
/
goconfig_test.go
40 lines (35 loc) · 881 Bytes
/
goconfig_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
package goconfig
import (
"fmt"
"testing"
)
type Site struct {
Title string `mapstructure:"title"`
}
type DB struct {
Host string `mapstructure:"host"`
Name string `mapstructure:"name"`
Password string `mapstructure:"password"`
Port string `mapstructure:"port"`
Username string `mapstructure:"username"`
}
type MyConfig struct {
Db DB
Site Site
}
func TestGetConfig(t *testing.T) {
var conf MyConfig
runtimeConf, err := GetConfig(conf)
if err != nil {
t.Fatalf("cannot load config:%v", err)
}
fmt.Printf("runtimeConf: %T\n", runtimeConf)
fmt.Printf("% v\n", runtimeConf)
// see https://go.dev/doc/effective_go#interface_conversions
confStruct, ok := runtimeConf.(MyConfig)
if !ok {
t.Fatalf("runtimeConf cannot convert to MyConfig")
}
fmt.Printf("confStruct: % v\n", confStruct)
fmt.Printf("Db.Username:%v\n", confStruct.Db.Username)
}