-
Notifications
You must be signed in to change notification settings - Fork 157
/
serviceContainer.go
87 lines (70 loc) · 1.92 KB
/
serviceContainer.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
package kernel
import (
"crypto/md5"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/power"
)
type ApplicationInterface interface {
GetContainer() *ServiceContainer
GetAccessToken() *AccessToken
GetConfig() *Config
GetComponent(name string) interface{}
}
type ExtraInfo struct {
Prepends *power.HashMap
ID string
}
func NewExtraInfo() (*ExtraInfo, error) {
return &ExtraInfo{
Prepends: &power.HashMap{},
ID: "",
}, nil
}
type ServiceContainer struct {
ID string
Prepends *object.Attribute
DefaultConfig *object.HashMap
UserConfig *object.HashMap
Config *object.HashMap
}
func NewServiceContainer(config *object.HashMap, extraInfos ...*ExtraInfo) (*ServiceContainer, error) {
var extraInfo = &ExtraInfo{}
if len(extraInfos) > 0 {
extraInfo = extraInfos[0]
}
prepends, err := power.PowerHashMapToObjectHashMap(extraInfo.Prepends)
if err != nil {
return nil, err
}
container := &ServiceContainer{
ID: extraInfo.ID,
UserConfig: config,
Prepends: object.NewAttribute(prepends),
}
return container, nil
}
func (container *ServiceContainer) GetID() string {
if container.ID == "" {
data, _ := object.JsonEncode(container.UserConfig)
encodeData := md5.Sum([]byte(data))
container.ID = string(encodeData[:])
}
return container.ID
}
func (container *ServiceContainer) getBaseConfig() *object.HashMap {
return &object.HashMap{
// http://docs.guzzlephp.org/en/stable/request-options.html
"http": &object.HashMap{
"timeout": 30.0,
"base_uri": "https://api.weixin.qq.com/",
},
}
}
func (container *ServiceContainer) GetConfig() *object.HashMap {
// init container config
basicConfig := container.getBaseConfig()
// merge config
container.Config = object.ReplaceHashMapRecursive(container.Config, basicConfig, container.DefaultConfig, container.UserConfig)
//fmt.Dump(container.Config)
return container.Config
}