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 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= diff --git a/models/catalog/v1alpha1/catalog.go b/models/catalog/v1alpha1/catalog.go index f5a78410..3886dd81 100644 --- a/models/catalog/v1alpha1/catalog.go +++ b/models/catalog/v1alpha1/catalog.go @@ -10,7 +10,7 @@ import ( // 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 +23,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"` @@ -70,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"` } @@ -80,33 +80,54 @@ 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" +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, err + } + + classProperty, err := utils.Cast[map[string]interface{}](properties["class"]) + if err != nil { + return nil, err } + + oneOf, err := utils.Cast[[]interface{}](classProperty["oneOf"]) + if err != nil { + return nil, err + } + + return oneOf, nil } -// Ref to catalog schema - https://github.com/meshery/schemas/blob/master/schemas/constructs/v1alpha1/catalog_data.json +// GetCatalogClasses gets class and 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.", - }, + oneOf, err := getClasses() + if err != nil { + utils.ErrGettingClassDescription(err) + return nil } + + 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 +} + +// String method for ContentClass +func (c ContentClass) String() string { + return string(c) } 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"}, + ) +} 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 +}