Skip to content

Commit

Permalink
Merge pull request #277 from Revolyssup/trim
Browse files Browse the repository at this point in the history
Add trim filter and add empty schema check
  • Loading branch information
Revolyssup authored Mar 13, 2023
2 parents 3a62e8b + c15ef19 commit edde0bd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
24 changes: 21 additions & 3 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,28 @@ func (c ComponentDefinition) Type() types.CapabilityType {
func (c ComponentDefinition) GetID() uuid.UUID {
return c.ID
}

func emptySchemaCheck(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 !emptySchemaCheck(c.Schema) {
c.Metadata["hasInvalidSchema"] = true
}
modelID := uuid.NewSHA1(uuid.UUID{}, byt)
var model Model
modelCreationLock.Lock()
Expand Down Expand Up @@ -138,6 +152,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 +164,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
2 changes: 1 addition & 1 deletion models/meshmodel/core/v1alpha1/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
var modelCreationLock sync.Mutex //Each component/relationship will perform a check and if the model already doesn't exist, it will create a model. This lock will make sure that there are no race conditions.
type ModelFilter struct {
Name string
DisplayName string //If Name is already passed, avoid passing Display name unless greedy=true, else the filter will translate to an AND returning only the models where name and display name match exactly. Ignore, if this behaviour is expected.
DisplayName string //If Name is already passed, avoid passing Display name unless greedy=true, else the filter will translate to an AND returning only the models where name and display name match exactly. Ignore, if this behavior is expected.
Greedy bool //when set to true - instead of an exact match, name will be prefix matched. Also an OR will be performed of name and display_name
Version string
Category string
Expand Down

0 comments on commit edde0bd

Please sign in to comment.