From 0eba848abcbd90a24f169fd469f04d9f57fab723 Mon Sep 17 00:00:00 2001 From: Shubham Upadhyay <58693304+shubham251972@users.noreply.github.com> Date: Fri, 19 Jul 2024 22:44:27 +0000 Subject: [PATCH 1/6] Source class description from schema Signed-off-by: Shubham Upadhyay <58693304+shubham251972@users.noreply.github.com> --- models/catalog/v1alpha1/catalog.go | 79 ++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 26 deletions(-) diff --git a/models/catalog/v1alpha1/catalog.go b/models/catalog/v1alpha1/catalog.go index f5a78410..57c375eb 100644 --- a/models/catalog/v1alpha1/catalog.go +++ b/models/catalog/v1alpha1/catalog.go @@ -5,12 +5,13 @@ import ( "encoding/json" "github.com/layer5io/meshkit/utils" + "github.com/meshery/schemas" ) // CatalogData defines model for catalog_data. type CatalogData struct { ContentClass ContentClass `json:"content_class,omitempty"` - //Tracks the specific content version that has been made available in the Catalog + // Tracks the specific content version that has been made available in the Catalog PublishedVersion string `json:"published_version"` // Compatibility A list of technologies included in or implicated by this design; a list of relevant technology tags. @@ -23,7 +24,7 @@ type CatalogData struct { PatternInfo string `json:"pattern_info"` // Contains reference to the dark and light mode snapshots of the catalog. - SnapshotURL []string `json:"imageURL,omitempty"` // this will require updating exisitng catalog data as well. updated the json tag to match previous key name, so changes will not be required in exisitng catgalogs + SnapshotURL []string `json:"imageURL,omitempty"` // this will require updating existing catalog data as well. updated the json tag to match previous key name, so changes will not be required in existing catalogs // Type Categorization of the type of design or operational flow depicted in this design. Type CatalogDataType `json:"type"` @@ -80,33 +81,59 @@ const ( Community ContentClass = "community" ) -func (c ContentClass) String() string { - switch c { - case Official: - return "official" - case Verified: - return "verified" - case Community: - fallthrough - default: - return "community" +// Load and extract descriptions and enum values from schema +func loadSchemaDescriptions() (map[string]ContentClassObj, error) { + // Load the schema file + schema, err := schemas.LoadSchema("schemas/constructs/v1alpha1/catalog_data.json") + if err != nil { + return nil, utils.ErrUnmarshal(err) } + + properties, propErr := utils.Cast[map[string]interface{}](schema["properties"]) + if propErr != nil { + return nil, propErr + } + + classProperty, classErr := utils.Cast[map[string]interface{}](properties["class"]) + if classErr != nil { + return nil, classErr + } + + enumDescriptions, _ := utils.Cast[[]interface{}](classProperty["enumDescriptions"]) + enumValues, _ := utils.Cast[[]interface{}](classProperty["enum"]) + + descriptions := make(map[string]ContentClassObj) + for i, value := range enumValues { + if i < len(enumDescriptions) { + class := value.(string) + description := enumDescriptions[i].(string) + descriptions[class] = ContentClassObj{ + Class: ContentClass(class), + Description: description, + } + } + } + + return descriptions, nil } -// Ref to catalog schema - https://github.com/meshery/schemas/blob/master/schemas/constructs/v1alpha1/catalog_data.json +// GetCatalogClasses gets class descriptions from the schema func GetCatalogClasses() []ContentClassObj { - return []ContentClassObj{ - { - Class: Official, - Description: "Content produced and fully supported by Meshery maintainers. This represents the highest level of support and is considered the most reliable.", - }, - { - Class: Verified, - Description: "Content produced by partners and verified by Meshery maintainers. While not directly maintained by Meshery, it has undergone a verification process to ensure quality and compatibility.", - }, - { - Class: Community, - Description: "Content produced and shared by Meshery users. This includes a wide range of content, such as performance profiles, test results, filters, patterns, and applications. Community content may have varying levels of support and reliability.", - }, + descriptions, err := loadSchemaDescriptions() + if err != nil { + utils.ErrGettingClassDescription(err) + return nil + } + + var classObjects []ContentClassObj + for _, obj := range descriptions { + classObjects = append(classObjects, obj) } + + return classObjects +} + +// String method for ContentClass +func (c ContentClass) String() string { + return string(c) } From 562effaf787db02754bd6c70610a7fd13393829d Mon Sep 17 00:00:00 2001 From: Shubham Upadhyay <58693304+shubham251972@users.noreply.github.com> Date: Thu, 18 Jul 2024 22:44:59 +0000 Subject: [PATCH 2/6] Added meshkit error Signed-off-by: Shubham Upadhyay <58693304+shubham251972@users.noreply.github.com> --- utils/error.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/utils/error.go b/utils/error.go index 8e9a42cf..7c1d3201 100644 --- a/utils/error.go +++ b/utils/error.go @@ -46,6 +46,7 @@ var ( ErrCopyFileCode = "replace_me" ErrCloseFileCode = "replace_me" ErrCompressToTarGZCode = "meshkit-11248" + ErrGettingClassDescriptionCode = "meshkit-11249" ) var ( ErrExtractType = errors.New( @@ -230,3 +231,14 @@ func ErrCloseFile(err error) error { []string{"Check for issues with file permissions or disk space and try again."}, ) } + +func ErrGettingClassDescription(err error) error { + return errors.New( + ErrGettingClassDescriptionCode, + errors.Alert, + []string{"Error getting class description"}, + []string{err.Error()}, + []string{"Schema version might have changed.", "Schema location might have change"}, + []string{"Make sure ref schema version is correct.", "Make sure location of schema is correct"}, + ) +} From 84dbc7d2f60bf0d98437c9955cf27980d9fae1f2 Mon Sep 17 00:00:00 2001 From: Shubham Upadhyay <58693304+shubham251972@users.noreply.github.com> Date: Thu, 1 Aug 2024 06:13:02 +0530 Subject: [PATCH 3/6] Update catalog.go with schema change for oneOf Signed-off-by: Shubham Upadhyay <58693304+shubham251972@users.noreply.github.com> --- models/catalog/v1alpha1/catalog.go | 64 ++++++++++++++---------------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/models/catalog/v1alpha1/catalog.go b/models/catalog/v1alpha1/catalog.go index 57c375eb..3886dd81 100644 --- a/models/catalog/v1alpha1/catalog.go +++ b/models/catalog/v1alpha1/catalog.go @@ -5,7 +5,6 @@ import ( "encoding/json" "github.com/layer5io/meshkit/utils" - "github.com/meshery/schemas" ) // CatalogData defines model for catalog_data. @@ -71,7 +70,7 @@ func (cd *CatalogData) IsNil() bool { type ContentClass string type ContentClassObj struct { - Class ContentClass `json:"class"` + Class ContentClass `json:"class"` Description string `json:"description"` } @@ -81,54 +80,49 @@ const ( Community ContentClass = "community" ) -// Load and extract descriptions and enum values from schema -func loadSchemaDescriptions() (map[string]ContentClassObj, error) { - // Load the schema file - schema, err := schemas.LoadSchema("schemas/constructs/v1alpha1/catalog_data.json") +func getClasses() ([]interface{}, error) { + schema, err := utils.LoadJSONSchema("schemas/constructs/v1alpha1/catalog_data.json") + if err != nil { + return nil, utils.ErrUnmarshal(err) + } + + properties, err := utils.Cast[map[string]interface{}](schema["properties"]) if err != nil { - return nil, utils.ErrUnmarshal(err) + return nil, err } - properties, propErr := utils.Cast[map[string]interface{}](schema["properties"]) - if propErr != nil { - return nil, propErr - } - - classProperty, classErr := utils.Cast[map[string]interface{}](properties["class"]) - if classErr != nil { - return nil, classErr + classProperty, err := utils.Cast[map[string]interface{}](properties["class"]) + if err != nil { + return nil, err } - enumDescriptions, _ := utils.Cast[[]interface{}](classProperty["enumDescriptions"]) - enumValues, _ := utils.Cast[[]interface{}](classProperty["enum"]) - - descriptions := make(map[string]ContentClassObj) - for i, value := range enumValues { - if i < len(enumDescriptions) { - class := value.(string) - description := enumDescriptions[i].(string) - descriptions[class] = ContentClassObj{ - Class: ContentClass(class), - Description: description, - } - } + oneOf, err := utils.Cast[[]interface{}](classProperty["oneOf"]) + if err != nil { + return nil, err } - return descriptions, nil + return oneOf, nil } -// GetCatalogClasses gets class descriptions from the schema +// GetCatalogClasses gets class and descriptions from the schema func GetCatalogClasses() []ContentClassObj { - descriptions, err := loadSchemaDescriptions() + oneOf, err := getClasses() if err != nil { utils.ErrGettingClassDescription(err) return nil } - var classObjects []ContentClassObj - for _, obj := range descriptions { - classObjects = append(classObjects, obj) - } + var classObjects []ContentClassObj + + for _, item := range oneOf { + itemMap, _ := utils.Cast[map[string]interface{}](item) + class, _ := utils.Cast[string](itemMap["const"]) + description, _ := utils.Cast[string](itemMap["description"]) + classObjects = append(classObjects, ContentClassObj{ + Class: ContentClass(class), + Description: description, + }) + } return classObjects } From b7ac28a3c7d8aa7c3da103b466d4d8e41d93b517 Mon Sep 17 00:00:00 2001 From: Shubham Upadhyay <58693304+shubham251972@users.noreply.github.com> Date: Thu, 1 Aug 2024 06:15:36 +0530 Subject: [PATCH 4/6] Add load json schema as util function Signed-off-by: Shubham Upadhyay <58693304+shubham251972@users.noreply.github.com> --- utils/utils.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/utils/utils.go b/utils/utils.go index e037a6ec..3df33804 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -19,6 +19,7 @@ import ( "strings" "github.com/layer5io/meshkit/models/meshmodel/entity" + "github.com/meshery/schemas" log "github.com/sirupsen/logrus" "gopkg.in/yaml.v3" ) @@ -456,3 +457,20 @@ func FindEntityType(content []byte) (entity.EntityType, error) { } return "", ErrInvalidSchemaVersion } + +// Load JSON schema from Schema package +func LoadJSONSchema(filePath string) (map[string]interface{}, error) { + // Read the file from the embedded filesystem + data, err := schemas.Schemas.ReadFile(filePath) + if err != nil { + return nil, fmt.Errorf("error reading file: %w", err) + } + + // Unmarshal JSON data into a map + var schema map[string]interface{} + if err := json.Unmarshal(data, &schema); err != nil { + return nil, fmt.Errorf("error unmarshalling JSON: %w", err) + } + + return schema, nil +} From 8535afb225bcdac15ea6a2d9d3a7087321a53051 Mon Sep 17 00:00:00 2001 From: Shubham Upadhyay <58693304+shubham251972@users.noreply.github.com> Date: Thu, 1 Aug 2024 07:40:24 +0530 Subject: [PATCH 5/6] Bump schema Signed-off-by: Shubham Upadhyay <58693304+shubham251972@users.noreply.github.com> --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index d413cafa..3e3779c8 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +24,7 @@ require ( github.com/google/uuid v1.5.0 github.com/kubernetes/kompose v1.31.1 github.com/layer5io/meshery-operator v0.7.0 - github.com/meshery/schemas v0.7.9 + github.com/meshery/schemas v0.7.16 github.com/nats-io/nats.go v1.31.0 github.com/open-policy-agent/opa v0.57.1 github.com/opencontainers/image-spec v1.1.0-rc6 From 7b4e54956ad0170f8b522d6141dfdbe123db093c Mon Sep 17 00:00:00 2001 From: Shubham Upadhyay <58693304+shubham251972@users.noreply.github.com> Date: Thu, 1 Aug 2024 23:46:47 +0530 Subject: [PATCH 6/6] Update go.sum Do a proper bump of schema Signed-off-by: Shubham Upadhyay <58693304+shubham251972@users.noreply.github.com> --- go.sum | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go.sum b/go.sum index d2feebea..e1f47348 100644 --- a/go.sum +++ b/go.sum @@ -607,8 +607,8 @@ github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvls github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k= github.com/meshery/kompose v1.0.1 h1:lg8B/pkLh6762jeFsQATD8UJZZwXZf/aviC3/dzw78A= github.com/meshery/kompose v1.0.1/go.mod h1:TWhWTEMbJBUzENf4JTEtBmZRFm/r8n0nS6v4/nSD2vA= -github.com/meshery/schemas v0.7.9 h1:7rA9RfRfbYRGONYMUbfgen2j+jsKgfjqHPuUYOhjwyA= -github.com/meshery/schemas v0.7.9/go.mod h1:ZsfoE5HvlqJvUvBlqS2rHoNQoDJ+eYuly5w5m+qIQSM= +github.com/meshery/schemas v0.7.16 h1:ognecYwlbkbXGuNwmj3btRJhq4ozuAI/0pr37GzUDq4= +github.com/meshery/schemas v0.7.16/go.mod h1:mSg/yFQPiTf5HC9zkdpML5X9AimnOKjneCuNaJtOEMs= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/miekg/dns v1.1.43 h1:JKfpVSCB84vrAmHzyrsxB5NAr5kLoMXZArPSw7Qlgyg= github.com/miekg/dns v1.1.43/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0V4=