Newer
Older
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(collection, query string) *Runable {
mongo := MongoQuery{
Collection: collection,
Query: query,
}
m.IsMongo = true
runable := &Runable{
Master: *m,
MongoQuery: mongo,
}
return runable
}
// MongoServiceWithHost - return mongo query object
func (m *Master) MongoServiceWithHost(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() *Master {
m.IsCach = true
return m
}
// IsCachableWithExpiration for both fdb and mongo
func (m *Master) IsCachableWithExpiration(cacheExpirationTime time.Duration) *Master {
m.IsCach = true
m.CacheTime = cacheExpirationTime
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
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.GetMongoDAOWithHost(m.MongoQuery.Host, 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...).
Run()
if errormdl.CheckErr(getError) != nil {
loggermdl.LogError(getError)
return nil, errormdl.CheckErr(getError)
}
return rs.Value(), nil
}