Skip to content

Commit

Permalink
Merge pull request #422 from meshery/leecalcote/models/annotations
Browse files Browse the repository at this point in the history
feat: annotations property for models
  • Loading branch information
leecalcote authored Dec 2, 2023
2 parents 0133432 + 803ac87 commit 5c52cbb
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
5 changes: 2 additions & 3 deletions models/meshmodel/core/v1alpha1/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ func GetMeshModelComponents(db *database.Handler, f ComponentFilter) (c []Compon
Joins("JOIN model_dbs ON component_definition_dbs.model_id = model_dbs.id").
Joins("JOIN category_dbs ON model_dbs.category_id = category_dbs.id") //


if f.Greedy {
if f.Name != "" && f.DisplayName != "" {
finder = finder.Where("component_definition_dbs.kind LIKE ? OR display_name LIKE ?", "%"+f.Name+"%", f.DisplayName+"%")
Expand All @@ -128,7 +127,7 @@ func GetMeshModelComponents(db *database.Handler, f ComponentFilter) (c []Compon
if f.ModelName != "" && f.ModelName != "all" {
finder = finder.Where("model_dbs.name = ?", f.ModelName)
}

if f.Annotations == "true" {
finder = finder.Where("component_definition_dbs.metadata->>'isAnnotation' = true")
} else if f.Annotations == "false" {
Expand Down Expand Up @@ -188,7 +187,7 @@ type ComponentFilter struct {
OrderOn string
Limit int //If 0 or unspecified then all records are returned and limit is not used
Offset int
Annotations string
Annotations string //When this query parameter is "true", only components with the "isAnnotation" property set to true are returned. When this query parameter is "false", all components except those considered to be annotation components are returned. Any other value of the query parameter results in both annotations as well as non-annotation models being returned.
}

// Create the filter from map[string]interface{}
Expand Down
5 changes: 3 additions & 2 deletions models/meshmodel/core/v1alpha1/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ 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
Registrant string //name of the registrant for a given model
Registrant string //name of the registrant for a given model
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
OrderOn string
Sort string //asc or desc. Default behavior is asc
Limit int //If 0 or unspecified then all records are returned and limit is not used
Limit int //If 0 or unspecified then all records are returned and limit is not used
Offset int
Annotations string //When this query parameter is "true", only models with the "isAnnotation" property set to true are returned. When this query parameter is "false", all models except those considered to be annotation models are returned. Any other value of the query parameter results in both annoations as well as non-annotation models being returned.
}

// Create the filter from map[string]interface{}
Expand Down
10 changes: 10 additions & 0 deletions models/meshmodel/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,11 @@ func (rm *RegistryManager) GetModels(db *database.Handler, f types.Filter) ([]v1
finder = finder.Where("model_dbs.display_name = ?", mf.DisplayName)
}
}
if mf.Annotations == "true" {
finder = finder.Where("model_dbs.metadata->>'isAnnotation' = true")
} else if mf.Annotations == "false" {
finder = finder.Where("model_dbs.metadata->>'isAnnotation' = false")
}
if mf.Version != "" {
finder = finder.Where("model_dbs.version = ?", mf.Version)
}
Expand All @@ -343,6 +348,11 @@ func (rm *RegistryManager) GetModels(db *database.Handler, f types.Filter) ([]v1
if mf.Registrant != "" {
finder = finder.Where("hosts.hostname = ?", mf.Registrant)
}
if mf.Annotations == "true" {
finder = finder.Where("model_dbs.metadata->>'isAnnotation' = true")
} else if mf.Annotations == "false" {
finder = finder.Where("model_dbs.metadata->>'isAnnotation' = false")
}
if mf.OrderOn != "" {
if mf.Sort == "desc" {
finder = finder.Order(clause.OrderByColumn{Column: clause.Column{Name: mf.OrderOn}, Desc: true})
Expand Down
1 change: 0 additions & 1 deletion utils/manifests/getComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
k8s "github.com/layer5io/meshkit/utils/kubernetes"
)


func GetFromManifest(ctx context.Context, url string, resource int, cfg Config) (*Component, error) {
manifest, err := utils.ReadFileSource(url)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ func Contains[G []K, K comparable](slice G, ele K) bool {
return false
}


func Cast[K any](val interface{}) (K, error) {
assertedValue, ok := val.(K)
if !ok {
Expand All @@ -256,7 +255,7 @@ func Cast[K any](val interface{}) (K, error) {
return assertedValue, nil
}

func MarshalAndUnmarshal[fromType any, toType any](val fromType) (unmarshalledvalue toType, err error){
func MarshalAndUnmarshal[fromType any, toType any](val fromType) (unmarshalledvalue toType, err error) {
data, err := Marshal(val)
if err != nil {
return
Expand Down

0 comments on commit 5c52cbb

Please sign in to comment.