Skip to content

Commit

Permalink
Add trim filter and add empty schema check
Browse files Browse the repository at this point in the history
Signed-off-by: Ashish Tiwari <[email protected]>
  • Loading branch information
Revolyssup committed Mar 10, 2023
1 parent 3a62e8b commit 5668222
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions models/meshmodel/core/v1alpha1/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type ComponentDefinition struct {
Format ComponentFormat `json:"format" yaml:"format"`
Metadata map[string]interface{} `json:"metadata" yaml:"metadata"`
Model Model `json:"model"`
Schema string `json:"schema" yaml:"schema"`
Schema string `json:"schema,omitempty" yaml:"schema"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}
Expand All @@ -44,7 +44,7 @@ type ComponentDefinitionDB struct {
DisplayName string `json:"displayName" gorm:"displayName"`
Format ComponentFormat `json:"format" yaml:"format"`
Metadata []byte `json:"metadata" yaml:"metadata"`
Schema string `json:"schema" yaml:"schema"`
Schema string `json:"schema,omitempty" yaml:"schema"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}
Expand All @@ -55,14 +55,29 @@ func (c ComponentDefinition) Type() types.CapabilityType {
func (c ComponentDefinition) GetID() uuid.UUID {
return c.ID
}
func schemaValidationCheck(schema string) (valid bool) {
if schema == "" {
return
}
m := make(map[string]interface{})
_ = json.Unmarshal([]byte(schema), &m)
if m["properties"] == nil {
return
}
valid = true
return

}
func CreateComponent(db *database.Handler, c ComponentDefinition) (uuid.UUID, error) {
c.ID = uuid.New()
tempModelID := uuid.New()
byt, err := json.Marshal(c.Model)
if err != nil {
return uuid.UUID{}, err
}
if !schemaValidationCheck(c.Schema) {
c.Metadata["hasInvalidSchema"] = true
}
modelID := uuid.NewSHA1(uuid.UUID{}, byt)
var model Model
modelCreationLock.Lock()
Expand Down Expand Up @@ -138,6 +153,9 @@ func GetMeshModelComponents(db *database.Handler, f ComponentFilter) (c []Compon
fmt.Println(err.Error()) //for debugging
}
for _, cm := range componentDefinitionsWithModel {
if f.Trim {
cm.Schema = ""
}
c = append(c, cm.ComponentDefinitionDB.GetComponentDefinition(cm.Model))
}
return c
Expand All @@ -147,6 +165,7 @@ type ComponentFilter struct {
Name string
APIVersion string
Greedy bool //when set to true - instead of an exact match, name will be prefix matched
Trim bool //when set to true - the schema is not returned
DisplayName string
ModelName string
Version string
Expand Down

0 comments on commit 5668222

Please sign in to comment.