A question refers to the comment ! And if not Ooopsy

This commit is contained in:
mr
2024-08-30 14:50:48 +02:00
parent db78c70dc3
commit 8180fe5e99
39 changed files with 737 additions and 404 deletions

View File

@@ -13,21 +13,30 @@ import (
"github.com/rs/zerolog"
)
// single instance of the validator used in every model Struct to validate the fields
var validate = validator.New(validator.WithRequiredStructEnabled())
/*
* AbstractObject is a struct that represents the basic fields of an object
* it defines the object id and name
* every data in base root model should inherit from this struct (only exception is the ResourceModel)
*/
type AbstractObject struct {
UUID string `json:"id,omitempty" bson:"id,omitempty" validate:"required"`
Name string `json:"name,omitempty" bson:"name,omitempty" validate:"required"`
}
// GetID returns the id of the object (abstract)
func (ao *AbstractObject) GetID() string {
return ao.UUID
}
// GetName returns the name of the object (abstract)
func (ao *AbstractObject) GetName() string {
return ao.Name
}
// GetAccessor returns the accessor of the object (abstract)
func (dma *AbstractObject) GetAccessor(caller *tools.HTTPCaller) Accessor {
return nil
}
@@ -58,9 +67,9 @@ func (r *AbstractObject) GenerateID() {
}
type AbstractAccessor struct {
Logger zerolog.Logger
Type string
Caller *tools.HTTPCaller
Logger zerolog.Logger // Logger is the logger of the accessor, it's a specilized logger for the accessor
Type string // Type is the data type of the accessor
Caller *tools.HTTPCaller // Caller is the http caller of the accessor (optionnal) only need in a peer connection
}
func (dma *AbstractAccessor) GetType() string {
@@ -71,12 +80,14 @@ func (dma *AbstractAccessor) GetCaller() *tools.HTTPCaller {
return dma.Caller
}
// Init initializes the accessor with the data type and the http caller
func (dma *AbstractAccessor) Init(t DataType, caller *tools.HTTPCaller) {
dma.Logger = logs.CreateLogger(t.String(), "")
dma.Caller = caller
dma.Type = t.String()
dma.Logger = logs.CreateLogger(t.String(), "") // Create a logger with the data type
dma.Caller = caller // Set the caller
dma.Type = t.String() // Set the data type
}
// GenericLoadOne loads one object from the database (generic)
func (wfa *AbstractAccessor) GenericStoreOne(data DBObject, accessor Accessor) (DBObject, int, error) {
data.GenerateID()
f := dbs.Filters{
@@ -106,6 +117,7 @@ func (wfa *AbstractAccessor) GenericStoreOne(data DBObject, accessor Accessor) (
return accessor.LoadOne(id)
}
// GenericLoadOne loads one object from the database (generic)
func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBObject, int, error) {
res, code, err := accessor.LoadOne(id)
if err != nil {
@@ -120,15 +132,17 @@ func (dma *AbstractAccessor) GenericDeleteOne(id string, accessor Accessor) (DBO
return res, 200, nil
}
// GenericLoadOne loads one object from the database (generic)
// json expected in entry is a flatted object no need to respect the inheritance hierarchy
func (dma *AbstractAccessor) GenericUpdateOne(set DBObject, id string, accessor Accessor, new DBObject) (DBObject, int, error) {
r, c, err := accessor.LoadOne(id)
if err != nil {
return nil, c, err
}
change := set.Serialize()
loaded := r.Serialize()
change := set.Serialize() // get the changes
loaded := r.Serialize() // get the loaded object
for k, v := range change {
for k, v := range change { // apply the changes, with a flatten method
loaded[k] = v
}
id, code, err := mongo.MONGOService.UpdateOne(new.Deserialize(loaded), id, accessor.GetType())

View File

@@ -2,6 +2,7 @@ package utils
type DataType int
// DataType - Enum for the different types of resources in db accessible from the outside
const (
INVALID DataType = iota
DATA_RESOURCE
@@ -19,6 +20,7 @@ const (
BOOKING
)
// Bind the standard API name to the data type
var DefaultAPI = [...]string{
"",
"oc-catalog",
@@ -36,6 +38,7 @@ var DefaultAPI = [...]string{
"oc-datacenter",
}
// Bind the standard data name to the data type
var Str = [...]string{
"invalid",
"data_resource",
@@ -57,11 +60,11 @@ func FromInt(i int) string {
return Str[i]
}
func (d DataType) API() string {
func (d DataType) API() string { // API - Returns the API name of the data type
return DefaultAPI[d]
}
func (d DataType) String() string {
func (d DataType) String() string { // String - Returns the string name of the data type
return Str[d]
}

View File

@@ -5,6 +5,7 @@ import (
"cloud.o-forge.io/core/oc-lib/tools"
)
// ShallowDBObject is an interface that defines the basic methods shallowed version of a DBObject
type ShallowDBObject interface {
GenerateID()
GetID() string
@@ -13,6 +14,7 @@ type ShallowDBObject interface {
Serialize() map[string]interface{}
}
// DBObject is an interface that defines the basic methods for a DBObject
type DBObject interface {
GenerateID()
GetID() string
@@ -22,6 +24,7 @@ type DBObject interface {
GetAccessor(caller *tools.HTTPCaller) Accessor
}
// Accessor is an interface that defines the basic methods for an Accessor
type Accessor interface {
Init(t DataType, caller *tools.HTTPCaller)
GetType() string