From 5330cece5d0c7a32c2977ce69f242bdc680fa43e Mon Sep 17 00:00:00 2001 From: revolyssup Date: Sat, 29 Oct 2022 00:43:52 +0530 Subject: [PATCH 1/3] Add Database compliant Meshmodel structs Signed-off-by: revolyssup --- models/meshmodel/core/v1alpha1/component.go | 20 ++++++-- .../v1alpha1/component_capabilities_db.go | 51 +++++++++++++++++++ 2 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 models/meshmodel/core/v1alpha1/component_capabilities_db.go 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 +} From d2cba67908efb2dca1538088a03eac34dc3b828e Mon Sep 17 00:00:00 2001 From: Ashish Tiwari Date: Mon, 31 Oct 2022 13:50:44 +0530 Subject: [PATCH 2/3] minor changes Signed-off-by: Ashish Tiwari --- models/meshmodel/core/v1alpha1/component.go | 23 +++++++++---------- .../v1alpha1/component_capabilities_db.go | 12 ++++------ 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/models/meshmodel/core/v1alpha1/component.go b/models/meshmodel/core/v1alpha1/component.go index aa82fa35..2f4f5836 100644 --- a/models/meshmodel/core/v1alpha1/component.go +++ b/models/meshmodel/core/v1alpha1/component.go @@ -3,25 +3,24 @@ package v1alpha1 const ComponentDefinitionKindKey = "ComponentDefinition" type TypeMeta struct { - Kind string `json:"kind,omitempty"` - APIVersion string `json:"apiVersion,omitempty"` + Kind string `json:"kind,omitempty" yaml:"kind"` + APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion"` } // use NewComponent function for instantiating type Component struct { - TypeMeta `gorm:"embedded"` - ComponentSpec `gorm:"embedded"` - Metadata map[string]interface{} `json:"metadata,omitempty" gorm:"type:JSONB"` + TypeMeta `gorm:"embedded" yaml:"typemeta"` + ComponentSpec `gorm:"embedded" yaml:"componentspec"` + Metadata map[string]interface{} `json:"metadata,omitempty" yaml:"metadata"` // for backward compatibility - Spec string `json:"spec,omitempty"` + Spec string `json:"spec,omitempty" yaml:"spec"` } -type capability struct { - ID string `json:"id,omitempty"` +type Capability struct { // Host is the address of the service registering the capability - Host string `json:"host,omitempty"` + Host string `json:"host,omitempty" yaml:"host"` } type ComponentSpec struct { - Schematic map[string]interface{} `json:"schematic,omitempty" gorm:"type:JSONB"` + Schematic map[string]interface{} `json:"schematic,omitempty" yaml:"schematic"` } func NewComponent() Component { @@ -33,6 +32,6 @@ func NewComponent() Component { } type ComponentCapability struct { - Component - capability + Component `yaml:"component"` + Capability `yaml:"capability"` } diff --git a/models/meshmodel/core/v1alpha1/component_capabilities_db.go b/models/meshmodel/core/v1alpha1/component_capabilities_db.go index 1ae2543c..b00f8683 100644 --- a/models/meshmodel/core/v1alpha1/component_capabilities_db.go +++ b/models/meshmodel/core/v1alpha1/component_capabilities_db.go @@ -6,7 +6,7 @@ import ( "github.com/google/uuid" ) -// This file consists of methods and structs that database(gorm) will use to interact with meshmodel components +// This file consists of helper methods and structs that database(gorm) will use to interact with meshmodel components type ComponentDB struct { TypeMeta ComponentSpecDB @@ -22,15 +22,11 @@ type ComponentSpecDB struct { 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"` + Capability } func ComponentCapabilityFromCCDB(cdb ComponentCapabilityDB) (c ComponentCapability) { - c.capability = cdb.capability + c.Capability = cdb.Capability c.TypeMeta = cdb.TypeMeta c.Spec = cdb.Spec m := make(map[string]interface{}) @@ -42,7 +38,7 @@ func ComponentCapabilityFromCCDB(cdb ComponentCapabilityDB) (c ComponentCapabili return } func ComponentCapabilityDBFromCC(c ComponentCapability) (cdb ComponentCapabilityDB) { - cdb.capability = c.capability + cdb.Capability = c.Capability cdb.TypeMeta = c.TypeMeta cdb.Spec = c.Spec cdb.Metadata, _ = json.Marshal(c.Metadata) From 32150d377bc822436db58b01e98eebb705fc68a9 Mon Sep 17 00:00:00 2001 From: Ashish Tiwari Date: Mon, 31 Oct 2022 13:58:49 +0530 Subject: [PATCH 3/3] lint checks Signed-off-by: Ashish Tiwari --- .../meshmodel/core/v1alpha1/component_capabilities_db.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/models/meshmodel/core/v1alpha1/component_capabilities_db.go b/models/meshmodel/core/v1alpha1/component_capabilities_db.go index b00f8683..fb407cd6 100644 --- a/models/meshmodel/core/v1alpha1/component_capabilities_db.go +++ b/models/meshmodel/core/v1alpha1/component_capabilities_db.go @@ -25,18 +25,23 @@ type ComponentCapabilityDB struct { Capability } +// ComponentCapabilityFromCCDB produces a client facing instance of ComponentCapability from a database representation of ComponentCapability. +// Use this function to interconvert any time the ComponentCapability is fetched from the database and is to be returned to client. 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) + _ = json.Unmarshal(cdb.Metadata, &m) c.Metadata = m schematic := make(map[string]interface{}) - json.Unmarshal(cdb.Schematic, &schematic) + _ = json.Unmarshal(cdb.Schematic, &schematic) c.Schematic = schematic return } + +// ComponentCapabilityDBFromCC produces a database compatible instance of ComponentCapability from a client representation of ComponentCapability. +// Use this function to interconvert any time the ComponentCapability is created by some client and is to be saved to the database. func ComponentCapabilityDBFromCC(c ComponentCapability) (cdb ComponentCapabilityDB) { cdb.Capability = c.Capability cdb.TypeMeta = c.TypeMeta