Newer
Older
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
"github.com/oleksandr/conditions"
"github.com/tidwall/gjson"
var ruleCache map[string]conditions.Expr
func init() {
ruleCache = make(map[string]conditions.Expr, 0)
}
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
// LoadData is a method sign for loader methods
type LoadData = func(ab *AbstractBusinessLogicHolder)
// FinalStepProcessOutput is a method sign for loader methods
type FinalStepProcessOutput = func(ab *AbstractBusinessLogicHolder) *interface{}
// AbstractBusinessLogicHolder use this type to inheritance
type AbstractBusinessLogicHolder struct {
ServiceData map[string]interface{}
}
// GetDataString will give you string
func (ab *AbstractBusinessLogicHolder) GetDataString(key string) (string, bool) {
//check in map
temp, found := ab.ServiceData[key]
if errormdl.CheckBool(!found) {
return "", false
}
// cast it
value, ok := temp.(string)
if errormdl.CheckBool1(!ok) {
return "", false
}
return value, true
}
// GetDataInt will give you int
func (ab *AbstractBusinessLogicHolder) GetDataInt(key string) (int, bool) {
//check in map
temp, found := ab.ServiceData[key]
if errormdl.CheckBool(!found) {
return 0, false
}
// cast it
value, ok := temp.(int)
if errormdl.CheckBool1(!ok) {
return 0, false
}
return value, true
}
func (ab *AbstractBusinessLogicHolder) GetResultset(key string) (*gjson.Result, bool) {
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
//check in map
temp, found := ab.ServiceData[key]
if errormdl.CheckBool(!found) {
loggermdl.LogWarn("Key not found -", key)
return &gjson.Result{}, false
}
// cast it
value, ok := temp.(*gjson.Result)
if errormdl.CheckBool1(!ok) {
return &gjson.Result{}, false
}
return value, true
}
// GetBool will give you int
func (ab *AbstractBusinessLogicHolder) GetBool(key string) (bool, bool) {
//check in map
temp, found := ab.ServiceData[key]
if errormdl.CheckBool(!found) {
return false, false
}
// cast it
value, ok := temp.(bool)
if errormdl.CheckBool1(!ok) {
return false, false
}
return value, true
}
// GetInterface will give you string
func (ab *AbstractBusinessLogicHolder) GetInterface(key string) (interface{}, bool) {
//check in map
temp, found := ab.ServiceData[key]
if errormdl.CheckBool(!found) {
return 0, false
}
// cast it
return temp, true
}
// Build will create memory for your data
func (ab *AbstractBusinessLogicHolder) Build() {
ab.ServiceData = make(map[string]interface{})
}
// SetResultset will return map data with finaldata key
func (ab *AbstractBusinessLogicHolder) SetResultset(key string, obj *gjson.Result) {
ab.ServiceData[key] = obj
}
// GetFinalData will return map data with finaldata key
func (ab *AbstractBusinessLogicHolder) GetFinalData() *interface{} {
a := ab.ServiceData["finaldata"]
return &a
}
// SetFinalData will return map data with finaldata key
func (ab *AbstractBusinessLogicHolder) SetFinalData(data interface{}) {
ab.ServiceData["finaldata"] = data
}
// EchoBL sample EchoBL logic handler
func (ab *AbstractBusinessLogicHolder) EchoBL() map[string]interface{} {
return map[string]interface{}{
"ok": int64(1),
}
}
// Step help to maintain steps
type Step struct {
Stepname string
RawYQL string
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
processDataFunc LoadData
RunFunc func() map[string]interface{}
ErrorFunc func() map[string]interface{}
}
// ServiceBuilder will help you to run steps
type ServiceBuilder struct {
ServiceName string
steps []Step
businessLogicHolder *AbstractBusinessLogicHolder
}
// GetSB Gives you service builder from where you can run steps
func GetSB(name string, ab *AbstractBusinessLogicHolder) *ServiceBuilder {
newsb := ServiceBuilder{}
newsb.ServiceName = name
newsb.businessLogicHolder = ab
return &newsb
}
// AddStep will add func step with rule
// Stepname : Give Name to step. It will appear in log.
// Rule : Give Ybl rule
// blfunc : Give Business Logic function pointer
// errorfunc : Give Error function pointer
func (sb *ServiceBuilder) AddStep(stepname, rule string, ld LoadData, blfunc, errorfunc func() map[string]interface{}) *ServiceBuilder {
step := Step{}
step.RawYQL = rule
//Check rule in cache
cachedRule, found := ruleCache[rule]
if !found {
step.expr = cachedRule
} else {
// Parse the condition language and get expression
p := conditions.NewParser(strings.NewReader(rule))
expr, err := p.Parse()
if err != nil {
step.expr = expr
} else {
loggermdl.LogError("Error in step expr", err)
}
ruleCache[rule] = expr
}
step.RunFunc = blfunc
step.ErrorFunc = errorfunc
step.Stepname = stepname
step.processDataFunc = ld
sb.steps = append(sb.steps, step)
return sb
}
// Run all Steps one by one
func (sb *ServiceBuilder) Run() *ServiceBuilder {
for _, step := range sb.steps {
//Load Data
if step.processDataFunc != nil {
step.processDataFunc(sb.businessLogicHolder)
}
//Run step func
tmp := step.RunFunc()
// result, _ := yql.Match(step.RawYQL, tmp)
result, _ := conditions.Evaluate(step.expr, tmp)
step.ErrorFunc()
break
}
}
return sb
}
// FinalOutput Step
func (sb *ServiceBuilder) FinalOutput(fn FinalStepProcessOutput) *interface{} {
if fn == nil {
return sb.businessLogicHolder.GetFinalData()
}
return fn(sb.businessLogicHolder)
}