Skip to content

Commit

Permalink
Merge branch 'master' into ctcarrier/bug/fix-artifacthub-created-format
Browse files Browse the repository at this point in the history
Signed-off-by: Mohd Hamza <[email protected]>
  • Loading branch information
hamza-mohd authored Aug 21, 2024
2 parents 010e2b5 + 305c646 commit 6bdfb1d
Show file tree
Hide file tree
Showing 51 changed files with 869 additions and 1,217 deletions.
2 changes: 1 addition & 1 deletion cmd/syncmodutil/internal/modsync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func getRequiredVersionsFromString(s string) (p []Package) {
if pkg == "" {
continue
}

pkgName, pkgVersion := getPackageAndVersionFromPackageVersion(pkg)
if strings.HasPrefix(pkgName, "//") { //Has been commented out
continue
Expand Down
64 changes: 64 additions & 0 deletions encoding/encoding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package encoding

import (
"encoding/json"

"github.com/layer5io/meshkit/utils"
"gopkg.in/yaml.v2"
)

// Unmarshal parses the JSON/YAML data and stores the result in the value pointed to by out
func Unmarshal(data []byte, out interface{}) error {
err := unmarshalJSON(data, out)
if err != nil {
err = unmarshalYAML(data, out)
if err != nil {
return err
}
}
return nil
}

func unmarshalYAML(data []byte, result interface{}) error {
err := yaml.Unmarshal(data, result)
if err != nil {
return ErrDecodeYaml(err)
}
return nil
}

func unmarshalJSON(data []byte, result interface{}) error {

err := json.Unmarshal(data, result)
if err != nil {
if e, ok := err.(*json.SyntaxError); ok {
return ErrUnmarshalSyntax(err, e.Offset)
}
if e, ok := err.(*json.UnmarshalTypeError); ok {
return ErrUnmarshalType(err, e.Value)
}
if e, ok := err.(*json.UnsupportedTypeError); ok {
return ErrUnmarshalUnsupportedType(err, e.Type)
}
if e, ok := err.(*json.UnsupportedValueError); ok {
return ErrUnmarshalUnsupportedValue(err, e.Value)
}
if e, ok := err.(*json.InvalidUnmarshalError); ok {
return ErrUnmarshalInvalid(err, e.Type)
}
return ErrUnmarshal(err)
}
return nil
}

func Marshal(in interface{}) ([]byte, error) {
result, err := json.Marshal(in)
if err != nil {
result, err = yaml.Marshal(in)
if err != nil {
return nil, utils.ErrMarshal(err)
}
}

return result, nil
}
47 changes: 47 additions & 0 deletions encoding/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package encoding

import (
"reflect"
"strconv"

"github.com/layer5io/meshkit/errors"
)

const (
ErrDecodeYamlCode = ""
ErrUnmarshalCode = ""
ErrUnmarshalInvalidCode = ""
ErrUnmarshalSyntaxCode = ""
ErrUnmarshalTypeCode = ""
ErrUnmarshalUnsupportedTypeCode = ""
ErrUnmarshalUnsupportedValueCode = ""
)

// ErrDecodeYaml is the error when the yaml unmarshal fails
func ErrDecodeYaml(err error) error {
return errors.New(ErrDecodeYamlCode, errors.Alert, []string{"Error occurred while decoding YAML"}, []string{err.Error()}, []string{"Invalid object format"}, []string{"Make sure to input a valid YAML object"})
}

func ErrUnmarshal(err error) error {
return errors.New(ErrUnmarshalCode, errors.Alert, []string{"Unmarshal unknown error: "}, []string{err.Error()}, []string{"Invalid object format"}, []string{"Make sure to input a valid JSON object"})
}

func ErrUnmarshalInvalid(err error, typ reflect.Type) error {
return errors.New(ErrUnmarshalInvalidCode, errors.Alert, []string{"Unmarshal invalid error for type: ", typ.String()}, []string{err.Error()}, []string{"Invalid object format"}, []string{"Make sure to input a valid JSON object"})
}

func ErrUnmarshalSyntax(err error, offset int64) error {
return errors.New(ErrUnmarshalSyntaxCode, errors.Alert, []string{"Unmarshal syntax error at offest: ", strconv.Itoa(int(offset))}, []string{err.Error()}, []string{"Invalid object format"}, []string{"Make sure to input a valid JSON object"})
}

func ErrUnmarshalType(err error, value string) error {
return errors.New(ErrUnmarshalTypeCode, errors.Alert, []string{"Unmarshal type error at key: %s. Error: %s", value}, []string{err.Error()}, []string{"Invalid object format"}, []string{"Make sure to input a valid JSON object"})
}

func ErrUnmarshalUnsupportedType(err error, typ reflect.Type) error {
return errors.New(ErrUnmarshalUnsupportedTypeCode, errors.Alert, []string{"Unmarshal unsupported type error at key: ", typ.String()}, []string{err.Error()}, []string{"Invalid object format"}, []string{"Make sure to input a valid JSON object"})
}

func ErrUnmarshalUnsupportedValue(err error, value reflect.Value) error {
return errors.New(ErrUnmarshalUnsupportedValueCode, errors.Alert, []string{"Unmarshal unsupported value error at key: ", value.String()}, []string{err.Error()}, []string{"Invalid object format"}, []string{"Make sure to input a valid JSON object"})
}
5 changes: 5 additions & 0 deletions generators/artifacthub/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package artifacthub

const (
ArtifactHub = "artifacthub"
)
21 changes: 12 additions & 9 deletions generators/artifacthub/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"strings"
"time"

"github.com/layer5io/meshkit/models/meshmodel/core/v1beta1"
"github.com/layer5io/meshkit/utils"
"github.com/layer5io/meshkit/utils/component"
"github.com/layer5io/meshkit/utils/manifests"
"github.com/meshery/schemas/models/v1beta1/category"
_component "github.com/meshery/schemas/models/v1beta1/component"
"github.com/meshery/schemas/models/v1beta1/model"
"gopkg.in/yaml.v2"
)

Expand All @@ -36,8 +38,8 @@ func (pkg AhPackage) GetVersion() string {
return pkg.Version
}

func (pkg AhPackage) GenerateComponents() ([]v1beta1.ComponentDefinition, error) {
components := make([]v1beta1.ComponentDefinition, 0)
func (pkg AhPackage) GenerateComponents() ([]_component.ComponentDefinition, error) {
components := make([]_component.ComponentDefinition, 0)
// TODO: Move this to the configuration

if pkg.ChartUrl == "" {
Expand All @@ -52,16 +54,17 @@ func (pkg AhPackage) GenerateComponents() ([]v1beta1.ComponentDefinition, error)
if err != nil {
continue
}
if comp.Metadata == nil {
comp.Metadata = make(map[string]interface{})
}
if comp.Model.Metadata == nil {
comp.Model.Metadata = make(map[string]interface{})
comp.Model.Metadata = &model.ModelDefinition_Metadata{}
}

if comp.Model.Metadata.AdditionalProperties == nil {
comp.Model.Metadata.AdditionalProperties = make(map[string]interface{})
}
comp.Model.Metadata["source_uri"] = pkg.ChartUrl
comp.Model.Metadata.AdditionalProperties["source_uri"] = pkg.ChartUrl
comp.Model.Version = pkg.Version
comp.Model.Name = pkg.Name
comp.Model.Category = v1beta1.Category{
comp.Model.Category = category.CategoryDefinition{
Name: "",
}
comp.Model.DisplayName = manifests.FormatToReadableString(comp.Model.Name)
Expand Down
5 changes: 5 additions & 0 deletions generators/github/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package github

const (
GitHub = "github"
)
2 changes: 1 addition & 1 deletion generators/github/git_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func fileInterceptor(br *bufio.Writer) walker.FileInterceptor {
// Add more calrifying commment and entry inside docs.
func dirInterceptor(br *bufio.Writer) walker.DirInterceptor {
return func(d walker.Directory) error {
err := helm.ConvertToK8sManifest(d.Path, br)
err := helm.ConvertToK8sManifest(d.Path, "", br)
if err != nil {
return err
}
Expand Down
20 changes: 11 additions & 9 deletions generators/github/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"bytes"
"os"

"github.com/layer5io/meshkit/models/meshmodel/core/v1beta1"
"github.com/layer5io/meshkit/utils"
"github.com/layer5io/meshkit/utils/component"
"github.com/layer5io/meshkit/utils/manifests"
"github.com/meshery/schemas/models/v1beta1/category"
_component "github.com/meshery/schemas/models/v1beta1/component"
"github.com/meshery/schemas/models/v1beta1/model"
)

type GitHubPackage struct {
Expand All @@ -23,8 +25,8 @@ func (gp GitHubPackage) GetVersion() string {
return gp.version
}

func (gp GitHubPackage) GenerateComponents() ([]v1beta1.ComponentDefinition, error) {
components := make([]v1beta1.ComponentDefinition, 0)
func (gp GitHubPackage) GenerateComponents() ([]_component.ComponentDefinition, error) {
components := make([]_component.ComponentDefinition, 0)

data, err := os.ReadFile(gp.filePath)
if err != nil {
Expand All @@ -39,17 +41,17 @@ func (gp GitHubPackage) GenerateComponents() ([]v1beta1.ComponentDefinition, err
if err != nil {
continue
}
if comp.Metadata == nil {
comp.Metadata = make(map[string]interface{})
}
if comp.Model.Metadata == nil {
comp.Model.Metadata = make(map[string]interface{})
comp.Model.Metadata = &model.ModelDefinition_Metadata{}
}
if comp.Model.Metadata.AdditionalProperties == nil {
comp.Model.Metadata.AdditionalProperties = make(map[string]interface{})
}

comp.Model.Metadata["source_uri"] = gp.SourceURL
comp.Model.Metadata.AdditionalProperties["source_uri"] = gp.SourceURL
comp.Model.Version = gp.version
comp.Model.Name = gp.Name
comp.Model.Category = v1beta1.Category{
comp.Model.Category = category.CategoryDefinition{
Name: "",
}
comp.Model.DisplayName = manifests.FormatToReadableString(comp.Model.Name)
Expand Down
2 changes: 1 addition & 1 deletion generators/github/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func ProcessContent(w io.Writer, downloadDirPath, downloadfilePath string) error
}

err = utils.ProcessContent(downloadDirPath, func(path string) error {
err = helm.ConvertToK8sManifest(path, w)
err = helm.ConvertToK8sManifest(path, "", w)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions generators/models/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package models

import "github.com/layer5io/meshkit/models/meshmodel/core/v1beta1"
import "github.com/meshery/schemas/models/v1beta1/component"

// anything that can be validated is a Validator
type Validator interface {
Expand All @@ -11,7 +11,7 @@ type Validator interface {
// system's capabilities in Meshery
// A Package should have all the information that we need to generate the components
type Package interface {
GenerateComponents() ([]v1beta1.ComponentDefinition, error)
GenerateComponents() ([]component.ComponentDefinition, error)
GetVersion() string
}

Expand Down
Loading

0 comments on commit 6bdfb1d

Please sign in to comment.