This commit is contained in:
mr 2024-07-18 15:15:01 +02:00
parent 33c284cd4c
commit 6d104288b7
3 changed files with 40 additions and 25 deletions

View File

@ -6,32 +6,38 @@ import (
"cloud.o-forge.io/core/oc-lib/models/resources"
)
type LibData int
const (
INVALID = resources.INVALID
DATA = resources.DATA
PROCESSING = resources.PROCESSING
STORAGE = resources.STORAGE
DATACENTER = resources.DATACENTER
WORKFLOW = resources.WORKFLOW
INVALID LibData = iota
DATA = resources.DATA
PROCESSING = resources.PROCESSING
STORAGE = resources.STORAGE
DATACENTER = resources.DATACENTER
WORKFLOW = resources.WORKFLOW
)
func (d LibData) EnumIndex() int {
return int(d)
}
func Init() {
mongo.MONGOService.Init(models.GetModelsNames(), GetConfig())
}
func LoadOne(name string, id string) interface{} {
return models.Model(name).GetAccessor().LoadOne(id)
func LoadOne(col LibData, id string) interface{} {
return models.Model(col.EnumIndex()).GetAccessor().LoadOne(id)
}
func UpdateOne(name string, set map[string]interface{}, id string) interface{} {
return models.Model(name).GetAccessor().UpdateOne(set, id)
func UpdateOne(col LibData, set map[string]interface{}, id string) interface{} {
return models.Model(col.EnumIndex()).GetAccessor().UpdateOne(set, id)
}
func DeleteOne(name string, id string) interface{} {
return models.Model(name).GetAccessor().DeleteOne(id)
func DeleteOne(col LibData, id string) interface{} {
return models.Model(col.EnumIndex()).GetAccessor().DeleteOne(id)
}
func StoreOne(name string, object map[string]interface{}) interface{} {
model := models.Model(name)
func StoreOne(col LibData, object map[string]interface{}) interface{} {
model := models.Model(col.EnumIndex())
return model.GetAccessor().StoreOne(model.Deserialize(object))
}

View File

@ -12,19 +12,19 @@ import (
)
var models = map[string]func() utils.DBObject{
w.WORKFLOW: func() utils.DBObject { return &w.Workflow{} },
r.ToString(r.DATA): func() utils.DBObject { return &d.Data{} },
r.ToString(r.DATACENTER): func() utils.DBObject { return &dc.Datacenter{} },
r.ToString(r.STORAGE): func() utils.DBObject { return &s.Storage{} },
r.ToString(r.PROCESSING): func() utils.DBObject { return &p.Processing{} },
w.WORKFLOW: func() utils.DBObject { return &w.Workflow{} },
r.DATA.String(): func() utils.DBObject { return &d.Data{} },
r.DATACENTER.String(): func() utils.DBObject { return &dc.Datacenter{} },
r.STORAGE.String(): func() utils.DBObject { return &s.Storage{} },
r.PROCESSING.String(): func() utils.DBObject { return &p.Processing{} },
}
func Model(model string) utils.DBObject {
func Model(model int) utils.DBObject {
log := logs.CreateLogger("oclib", "")
if _, ok := models[model]; ok {
return models[model]()
if _, ok := models[r.FromInt(model)]; ok {
return models[r.FromInt(model)]()
}
log.Error().Msg("Can't find model " + model + ".")
log.Error().Msg("Can't find model " + r.FromInt(model) + ".")
return nil
}

View File

@ -26,8 +26,17 @@ var str = [...]string{
"workflow",
}
func ToString(r ResourceType) string {
return str[r]
func FromInt(i int) string {
return str[i]
}
func (d ResourceType) String() string {
return str[d]
}
// EnumIndex - Creating common behavior-give the type a EnumIndex functio
func (d ResourceType) EnumIndex() int {
return int(d)
}
type Resource interface {