-
Notifications
You must be signed in to change notification settings - Fork 666
/
Copy pathcase.go
49 lines (44 loc) · 1.11 KB
/
case.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
package helper
import (
jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/gou/process"
"github.com/yaoapp/kun/exception"
)
// CaseParam 条件参数
type CaseParam struct {
When []Condition `json:"when"`
Name string `json:"name"`
Process string `json:"process"`
Args []interface{} `json:"args"`
}
// Case 条件判断
func Case(params ...CaseParam) interface{} {
for _, param := range params {
if When(param.When) {
return process.New(param.Process, param.Args...).Run()
}
}
return nil
}
// CaseParamOf 读取参数
func CaseParamOf(v interface{}) CaseParam {
data, err := jsoniter.Marshal(v)
if err != nil {
exception.New("参数错误: %s", 400, err).Throw()
}
res := CaseParam{}
err = jsoniter.Unmarshal(data, &res)
if err != nil {
exception.New("参数错误: %s", 400, err).Throw()
}
return res
}
// ProcessCase xiang.helper.Case Case条件判断
func ProcessCase(process *process.Process) interface{} {
process.ValidateArgNums(1)
params := []CaseParam{}
for _, v := range process.Args {
params = append(params, CaseParamOf(v))
}
return Case(params...)
}