diff --git a/models/meshmodel/core/v1alpha1/component.go b/models/meshmodel/core/v1alpha1/component.go index 7bcea0a1..aa82fa35 100644 --- a/models/meshmodel/core/v1alpha1/component.go +++ b/models/meshmodel/core/v1alpha1/component.go @@ -9,20 +9,30 @@ type TypeMeta struct { // use NewComponent function for instantiating type Component struct { - TypeMeta - ComponentSpec - Metadata map[string]interface{} `json:"metadata,omitempty"` + TypeMeta `gorm:"embedded"` + ComponentSpec `gorm:"embedded"` + Metadata map[string]interface{} `json:"metadata,omitempty" gorm:"type:JSONB"` // for backward compatibility Spec string `json:"spec,omitempty"` } - +type capability struct { + ID string `json:"id,omitempty"` + // Host is the address of the service registering the capability + Host string `json:"host,omitempty"` +} type ComponentSpec struct { - Schematic map[string]interface{} `json:"schematic,omitempty"` + Schematic map[string]interface{} `json:"schematic,omitempty" gorm:"type:JSONB"` } func NewComponent() Component { comp := Component{} comp.APIVersion = "core.meshery.io/v1alpha1" comp.Kind = ComponentDefinitionKindKey + comp.Metadata = make(map[string]interface{}, 1) return comp } + +type ComponentCapability struct { + Component + capability +} diff --git a/models/meshmodel/core/v1alpha1/component_capabilities_db.go b/models/meshmodel/core/v1alpha1/component_capabilities_db.go new file mode 100644 index 00000000..1ae2543c --- /dev/null +++ b/models/meshmodel/core/v1alpha1/component_capabilities_db.go @@ -0,0 +1,51 @@ +package v1alpha1 + +import ( + "encoding/json" + + "github.com/google/uuid" +) + +// This file consists of methods and structs that database(gorm) will use to interact with meshmodel components +type ComponentDB struct { + TypeMeta + ComponentSpecDB + Metadata []byte `json:"metadata"` + // for backward compatibility + Spec string `json:"spec,omitempty"` +} + +type ComponentSpecDB struct { + Schematic []byte `json:"schematic,omitempty"` +} + +type ComponentCapabilityDB struct { + ID uuid.UUID `json:"id,omitempty"` + ComponentDB + capability +} +type capabilityDB struct { + // Host is the address of the service registering the capability + Host string `json:"host,omitempty"` +} + +func ComponentCapabilityFromCCDB(cdb ComponentCapabilityDB) (c ComponentCapability) { + c.capability = cdb.capability + c.TypeMeta = cdb.TypeMeta + c.Spec = cdb.Spec + m := make(map[string]interface{}) + json.Unmarshal(cdb.Metadata, &m) + c.Metadata = m + schematic := make(map[string]interface{}) + json.Unmarshal(cdb.Schematic, &schematic) + c.Schematic = schematic + return +} +func ComponentCapabilityDBFromCC(c ComponentCapability) (cdb ComponentCapabilityDB) { + cdb.capability = c.capability + cdb.TypeMeta = c.TypeMeta + cdb.Spec = c.Spec + cdb.Metadata, _ = json.Marshal(c.Metadata) + cdb.Schematic, _ = json.Marshal(c.Schematic) + return +}