-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_script.go
85 lines (79 loc) · 1.9 KB
/
service_script.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
package redis
import (
"context"
"encoding/json"
"errors"
"github.com/team-ide/go-tool/util"
"go.uber.org/zap"
"reflect"
)
func NewServiceScript(config interface{}) (res map[string]interface{}, err error) {
if config == nil {
err = errors.New("config is null")
util.Logger.Error("NewServiceScript error", zap.Error(err))
return
}
var service IService
if c1, ok := config.(*Config); ok {
service, err = New(c1)
if err != nil {
util.Logger.Error("NewServiceScript error", zap.Error(err))
return
}
} else {
if c2, ok := config.(Config); ok {
service, err = New(&c2)
if err != nil {
util.Logger.Error("NewServiceScript error", zap.Error(err))
return
}
} else {
var bs []byte
bs, err = json.Marshal(config)
if err != nil {
err = errors.New("config to json error:" + err.Error())
util.Logger.Error("NewServiceScript error", zap.Error(err))
return
}
var c3 = &Config{}
err = json.Unmarshal(bs, c3)
if err != nil {
err = errors.New("config to json error:" + err.Error())
util.Logger.Error("NewServiceScript error", zap.Error(err))
return
}
service, err = New(c3)
if err != nil {
util.Logger.Error("NewServiceScript error", zap.Error(err))
return
}
}
}
res = map[string]interface{}{}
res["service"] = service
t := reflect.TypeOf(service)
v := reflect.ValueOf(service)
size := t.NumMethod()
for i := 0; i < size; i++ {
method := t.Method(i)
name := method.Name
res[name] = v.Method(i).Interface()
res[util.FirstToLower(name)] = v.Method(i).Interface()
}
return
}
func NewParam(args ...interface{}) *Param {
res := &Param{}
for _, arg := range args {
if v, ok := arg.(int); ok {
res.Database = v
} else if v, ok := arg.(int64); ok {
res.Database = int(v)
} else if v, ok := arg.(float64); ok {
res.Database = int(v)
} else if v, ok := arg.(context.Context); ok {
res.Ctx = v
}
}
return res
}