Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source class description from schema #542

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 53 additions & 26 deletions models/catalog/v1alpha1/catalog.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package v1alpha1

Check failure on line 1 in models/catalog/v1alpha1/catalog.go

View workflow job for this annotation

GitHub Actions / lint

: # github.com/layer5io/meshkit/models/catalog/v1alpha1

import (
"database/sql/driver"
"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.
Expand All @@ -23,7 +24,7 @@
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"`
Expand Down Expand Up @@ -80,33 +81,59 @@
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) {
shubham251972 marked this conversation as resolved.
Show resolved Hide resolved
// Load the schema file
schema, err := schemas.LoadSchema("schemas/constructs/v1alpha1/catalog_data.json")

Check failure on line 87 in models/catalog/v1alpha1/catalog.go

View workflow job for this annotation

GitHub Actions / codecov

undefined: schemas.LoadSchema

Check failure on line 87 in models/catalog/v1alpha1/catalog.go

View workflow job for this annotation

GitHub Actions / lint

undefined: schemas.LoadSchema (typecheck)

Check failure on line 87 in models/catalog/v1alpha1/catalog.go

View workflow job for this annotation

GitHub Actions / lint

undefined: schemas.LoadSchema) (typecheck)

Check failure on line 87 in models/catalog/v1alpha1/catalog.go

View workflow job for this annotation

GitHub Actions / lint

undefined: schemas.LoadSchema) (typecheck)
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)
}
12 changes: 12 additions & 0 deletions utils/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var (
ErrCopyFileCode = "replace_me"
ErrCloseFileCode = "replace_me"
ErrCompressToTarGZCode = "meshkit-11248"
ErrGettingClassDescriptionCode = "meshkit-11249"
)
var (
ErrExtractType = errors.New(
Expand Down Expand Up @@ -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"},
)
}
Loading