Newer
Older
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
package routebuildermdl
import (
"encoding/json"
"strconv"
"strings"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/dalmdl/dao"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/dalmdl/mongodb"
"github.com/tidwall/gjson"
)
// Master - struct for master Service
type Master struct {
ServiceName string
IsCach bool
IsRestricted bool
IsRoleBased bool
IsMongo bool
}
// MongoQuery - for mongo service
type MongoQuery struct {
Collection string
Host string
Query string
Args []string
}
// FDBQuery - for fdb services
type FDBQuery struct {
FilePath string
Query []string
}
// Runable - helps to run
type Runable struct {
Master
MongoQuery
FDBQuery
}
// MongoService - return mongo query object
func (m *Master) MongoService(host, collection, query string) *Runable {
mongo := MongoQuery{
Host: host,
Collection: collection,
Query: query,
}
m.IsMongo = true
runable := &Runable{
Master: *m,
MongoQuery: mongo,
}
return runable
}
// FDBService - return mongo query object
func (m *Master) FDBService(filPath string, query ...string) *Runable {
FDB := FDBQuery{
FilePath: filPath,
Query: query,
}
runable := &Runable{
Master: *m,
FDBQuery: FDB,
}
return runable
}
// IsCachable for both fdb and mongo
func (m *Master) IsCachable(status bool) *Master {
m.IsCach = status
return m
}
// SetArgs set argument for query string
func (m *Runable) SetArgs(args ...string) *Runable {
if m.Master.IsMongo {
m.MongoQuery.Args = args
}
return m
}
// Register - register it in cacahe
func (m *Runable) Register() {
service := ServiceCache{
MasterService: m,
IsMasterService: true,
}
commonServiceRegistration(m.Master.ServiceName, service, m.Master.IsRestricted, m.Master.IsRestricted)
}
// Run - execute and return output and error
func (m *Runable) Run(data []byte) (interface{}, error) {
if m.Master.IsMongo {
return m.runMongoService(data)
}
return m.runFDBService()
}
func (m *Runable) runMongoService(data []byte) (interface{}, error) {
rs := gjson.ParseBytes(data)
for i, arg := range m.MongoQuery.Args {
result := rs.Get(arg).String()
argNotation := "~" + strconv.Itoa(i+1)
m.MongoQuery.Query = strings.Replace(m.MongoQuery.Query, argNotation, result, 1)
}
var v map[string]interface{}
unmarshalError := json.Unmarshal([]byte(m.MongoQuery.Query), &v)
if errormdl.CheckErr(unmarshalError) != nil {
loggermdl.LogError(unmarshalError)
return nil, errormdl.CheckErr(unmarshalError)
}
query, getError := mongodb.GetMongoDAO(m.MongoQuery.Collection).GetData(v)
if errormdl.CheckErr(getError) != nil {
loggermdl.LogError(getError)
return nil, errormdl.CheckErr(getError)
}
}
func (m *Runable) runFDBService() (interface{}, error) {
rs, getError := dalmdl.GetDAO().
FilePath(m.FDBQuery.FilePath).
Query(m.FDBQuery.Query...).
IsCacheable(m.Master.IsCach).
Run()
if errormdl.CheckErr(getError) != nil {
loggermdl.LogError(getError)
return nil, errormdl.CheckErr(getError)
}
return rs.Value(), nil
}