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 all 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
75 changes: 48 additions & 27 deletions models/catalog/v1alpha1/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"`
Expand Down Expand Up @@ -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"`
}

Expand All @@ -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)
}
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"},
)
}
18 changes: 18 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Loading