-
Notifications
You must be signed in to change notification settings - Fork 1
/
loader.go
171 lines (134 loc) · 5.8 KB
/
loader.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright (C) 2019 WuPeng <wup364@outlook.com>.
// Use of this source code is governed by an MIT-style.
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// 模块加载器
// 依赖包: utils.utypes.Object utils.strutil.strutil
package ipakku
import (
"reflect"
"github.com/wup364/pakku/utils/utypes"
)
const (
// DEFT_VAL_APPNAME 默认实例名字
DEFT_VAL_APPNAME = "app"
// PARAMS_KEY_APPNAME 实例名字KEY
PARAMS_KEY_APPNAME = "app.name"
// ERR_MSG_MODULE_NOT_FOUND 模块未找到
ERR_MSG_MODULE_NOT_FOUND = "the module was not found, model: %s"
)
// Updater 模块版本升级执行器
type Updater interface {
// Version 要升级到的版本号
Version() float64
// Execute 执行升级
Execute(app Application) error
}
// Updaters 升级器
type Updaters []Updater
// 实现sort.Interface接口取元素数量方法
func (sort Updaters) Len() int {
return len(sort)
}
// 实现sort.Interface接口比较元素方法
func (sort Updaters) Less(i, j int) bool {
return sort[i].Version() < sort[j].Version()
}
// 实现sort.Interface接口交换元素方法
func (sort Updaters) Swap(i, j int) {
sort[i], sort[j] = sort[j], sort[i]
}
// Opts 模块配置项
type Opts struct {
Name string // [可选] 模块ID, 不填则为结构体名称
Version float64 // [必填] 模块版本
Description string // [可选] 模块描述
Updaters func(app Application) Updaters // [可选] 模块升级执行器, 一个版本执行一次
OnReady func(app Application) // [可选] 每次加载模块开始之前执行
OnSetup func() // [可选] 模块安装, 一个模块只初始化一次
OnInit func() // [可选] 每次模块安装、升级后执行一次
}
// ModuleEvent 模块生命周期事件
type ModuleEvent string
var ModuleEventOnReady ModuleEvent = "OnReady"
var ModuleEventOnSetup ModuleEvent = "OnSetup"
var ModuleEventOnUpdate ModuleEvent = "OnUpdate"
var ModuleEventOnInit ModuleEvent = "OnInit"
var ModuleEventOnLoaded ModuleEvent = "OnLoaded"
var ModuleEventOnSetupSucced ModuleEvent = "OnSetupSucced"
var ModuleEventOnUpdateSucced ModuleEvent = "OnUpdateSucced"
// OnModuleEvent 模块生命周期事件回调函数
type OnModuleEvent func(module interface{}, app Application)
// ModuleInfoRecorder 用于记录模块信息
type ModuleInfoRecorder interface {
Init(appName string) error
GetValue(key string) string
SetValue(key string, value string) error
}
// Module 实现这个接口可被加载器识别, 用于初始化和模块自动注入功能
type Module interface {
AsModule() Opts
}
// Loader 模块加载器, 实例化后可实现统一管理模板
type Loader interface {
// GetInstanceID 获取实例的ID
GetInstanceID() string
// Load 装载&初始化模块, 初始化顺序: doReady -> doSetup -> doCheckVersion -> doInit -> doEnd
Load(mt Module)
// Loads 装载&初始化模块(自动分析模块依赖顺序), 初始化顺序: doReady -> doSetup -> doCheckVersion -> doInit -> doEnd
Loads(mts ...Module)
// SetModuleInfoRecorder 设置模块信息记录器
SetModuleInfoRecorder(moduleInfo ModuleInfoRecorder)
// Application 获取当前实例
GetApplication() Application
Params // Params 保存实例中的键值对数据
Modules // Modules 模块操作
}
// Application 当前运行中的实例
type Application interface {
// GetInstanceID 获取实例的ID
GetInstanceID() string
// Params 保存实例中的键值对数据
Params() Params
// Modules 模块操作
Modules() Modules
// Utils 工具
Utils() Utils
}
// Params 保存实例中的键值对数据
type Params interface {
ParamGetter
ParamSetter
}
// ParamGetter 只读 - 保存实例中的键值对数据
type ParamGetter interface {
// GetParam 获取变量, 当前实例上的变量
GetParam(key string) utypes.Object
}
// ParamSetter 只写 - 保存实例中的键值对数据
type ParamSetter interface {
// SetParam 设置变量, 保存在当前实例内部
SetParam(key string, val interface{})
}
// Modules 模块操作
type Modules interface {
// GetModuleByName 根据模块Name获取模块指针记录, 可以获取一个已经实例化的模块
GetModuleByName(name string, val interface{}) error
// GetModules 获取模块, 模块名字和接口名字一样才能正常获得
GetModules(val ...interface{}) error
// GetModuleVersion 获取模块版本号
GetModuleVersion(name string) string
// OnModuleEvent 监听模块生命周期事件
OnModuleEvent(name string, event ModuleEvent, val OnModuleEvent)
}
// Utils 工具
type Utils interface {
// AutoWired 自动注入依赖对象
AutoWired(structobj interface{}) error
// Invoke 模块调用, 返回 []reflect.Value, 返回值暂时无法处理
Invoke(name string, method string, params ...interface{}) ([]reflect.Value, error)
}