Skip to content

Commit

Permalink
Merge pull request #559 from MUzairS15/MUzairS15/fixes
Browse files Browse the repository at this point in the history
Add standard `Unmarshal` for `yaml/json` data.
  • Loading branch information
MUzairS15 authored Aug 13, 2024
2 parents 9846521 + 74551a4 commit 8996f15
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 138 deletions.
51 changes: 51 additions & 0 deletions encoding/encoding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package encoding

import (
"encoding/json"

"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
}
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"})
}
4 changes: 2 additions & 2 deletions generators/artifacthub/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/layer5io/meshkit/utils"
"github.com/layer5io/meshkit/utils/component"
"github.com/layer5io/meshkit/utils/manifests"
_component "github.com/meshery/schemas/models/v1beta1/component"
"github.com/meshery/schemas/models/v1beta1/category"
_component "github.com/meshery/schemas/models/v1beta1/component"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -53,7 +53,7 @@ func (pkg AhPackage) GenerateComponents() ([]_component.ComponentDefinition, err
if err != nil {
continue
}

if comp.Model.Metadata.AdditionalProperties == nil {
comp.Model.Metadata.AdditionalProperties = make(map[string]interface{})
}
Expand Down
2 changes: 1 addition & 1 deletion generators/models/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package models

import "github.com/meshery/schemas/models/v1beta1/component"
import "github.com/meshery/schemas/models/v1beta1/component"

// anything that can be validated is a Validator
type Validator interface {
Expand Down
16 changes: 8 additions & 8 deletions models/meshmodel/registry/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ var (
ErrRegisteringEntityCode = "replace_me"
ErrUnknownHostInMapCode = "replace_me"
ErrCreatingUserDataDirectoryCode = "replace_me"
ErrGetByIdCode = "replace_me"
ErrGetByIdCode = "replace_me"
)

func ErrGetById(err error, id string) error {
return errors.New(
ErrUnknownHostCode,
errors.Alert,
[]string{"Failed to get the entity with the given ID: " + id},
[]string{err.Error()},
[]string{"Entity with the given ID may not be present in the registry", "Registry might be inaccessible at the moment"},
[]string{"Check if your ID is correct" , "If the registry is inaccesible, please try again after some time"},
)
ErrUnknownHostCode,
errors.Alert,
[]string{"Failed to get the entity with the given ID: " + id},
[]string{err.Error()},
[]string{"Entity with the given ID may not be present in the registry", "Registry might be inaccessible at the moment"},
[]string{"Check if your ID is correct", "If the registry is inaccesible, please try again after some time"},
)

}

Expand Down
2 changes: 1 addition & 1 deletion models/meshmodel/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (rm *RegistryManager) GetEntities(f entity.Filter) ([]entity.Entity, int64,
return f.Get(rm.db)
}

func (rm *RegistryManager) GetEntityById (f entity.Filter) (entity.Entity, error) {
func (rm *RegistryManager) GetEntityById(f entity.Filter) (entity.Entity, error) {
return f.GetById(rm.db)
}

Expand Down
8 changes: 4 additions & 4 deletions models/meshmodel/registry/v1beta1/policy_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type PolicyFilter struct {
Id string
Id string
Kind string
Greedy bool
SubType string
Expand All @@ -26,12 +26,12 @@ func (pf *PolicyFilter) Create(m map[string]interface{}) {
}

func (pf *PolicyFilter) GetById(db *database.Handler) (entity.Entity, error) {
p := &v1beta1.PolicyDefinition{}
err := db.First(p, "id = ?", pf.Id).Error
p := &v1beta1.PolicyDefinition{}
err := db.First(p, "id = ?", pf.Id).Error
if err != nil {
return nil, registry.ErrGetById(err, pf.Id)
}
return p, err
return p, err
}

func (pf *PolicyFilter) Get(db *database.Handler) ([]entity.Entity, int64, int, error) {
Expand Down
168 changes: 83 additions & 85 deletions models/registration/error.go
Original file line number Diff line number Diff line change
@@ -1,85 +1,83 @@
package registration

import (
"fmt"

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

const (
ErrDirPkgUnitParseFailCode = "replace_me"
ErrGetEntityCode = "replace_me"
ErrRegisterEntityCode = "replace_me"
ErrImportFailureCode = "replace_me"
ErrMissingRegistrantCode = "replace_me"
ErrSeedingComponentsCode = "replace-me"
)



func ErrSeedingComponents(err error) error {
return errors.New(
ErrSeedingComponentsCode,
errors.Alert,
[]string{"Failed to register the given models into meshery's registry"},
[]string{err.Error()},
[]string{"Given models may not be in accordance with Meshery's schema", "Internal(OS level) error while reading files" },
[]string{"Make sure the models being seeded are valid in accordance with Meshery's schema", "If it is an internal error, please try again after some time"},
)
}

func ErrMissingRegistrant(modelName string) error {
return errors.New(
ErrMissingRegistrantCode,
errors.Alert,
[]string{fmt.Sprintf("Model with name: %s does not have registrant information", modelName)},
[]string{"Meshery models are always registered in context of a registrant."},
// there is only one cause for this error
[]string{""},
[]string{"Make sure that the registrant information is present in the model definition"},
)
}

func ErrRegisterEntity(err error, name, entity string) error {
return errors.New(
ErrRegisterEntityCode,
errors.Alert,
[]string{fmt.Sprintf("Failed to register an entity of name: %s and type: %s into Meshery's registry", name, entity)},
[]string{err.Error()},
[]string{fmt.Sprintf("%s definition might be violating the definition schema", entity), fmt.Sprintf("%s might be missing model details", entity)},
[]string{fmt.Sprintf("ensure the %s definition follows the correct schema", entity), fmt.Sprintf("ensure %s definition belongs to correct model", entity)},
)
}

func ErrGetEntity(err error) error {
return errors.New(
ErrGetEntityCode,
errors.Alert,
[]string{"Could not parse the given data into any Meshery entity"},
[]string{err.Error()},
[]string{"Entity definition might not be in accordance with it's corresponding schema", "Might be an invalid/unsupported schemaVersion"},
[]string{"Ensure that the definition given is in accordance with it's schema", "Ensure that the schemaVersion used is valid and is supported by Meshery"},
)
}

func ErrDirPkgUnitParseFail(dirpath string, err error) error {
return errors.New(
ErrDirPkgUnitParseFailCode,
errors.Alert,
[]string{fmt.Sprintf("Directory at path: %s cannot be registered into Meshery", dirpath)},
[]string{fmt.Sprint(err.Error())},
[]string{"The directory might not have a valid model definition", "Might be some internal issues while walking the file tree"},
[]string{"Make sure that there is a valid model definition present in the directory. Meshery's registration pipeline currently does not support nested models, therefore the behaviour might be unexpected if it contains nested models.", "If there is an internal error, please try again after some time"},
)
}

func ErrImportFailure(hostname string, failedMsg string) error {
return errors.New(
ErrImportFailureCode,
errors.Alert,
[]string{fmt.Sprintf("Errors while registering entities for registrant: %s", hostname)},
[]string{failedMsg},
[]string{"Entity definition might not be in accordance with schema", "Entity version might not be supported by Meshery"},
[]string{"See the registration logs (found at $HOME/.meshery/logs/registry/registry-logs.log) to find out which Entity failed to be imported with more specific error information."},
)
}
package registration

import (
"fmt"

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

const (
ErrDirPkgUnitParseFailCode = "replace_me"
ErrGetEntityCode = "replace_me"
ErrRegisterEntityCode = "replace_me"
ErrImportFailureCode = "replace_me"
ErrMissingRegistrantCode = "replace_me"
ErrSeedingComponentsCode = "replace-me"
)

func ErrSeedingComponents(err error) error {
return errors.New(
ErrSeedingComponentsCode,
errors.Alert,
[]string{"Failed to register the given models into meshery's registry"},
[]string{err.Error()},
[]string{"Given models may not be in accordance with Meshery's schema", "Internal(OS level) error while reading files"},
[]string{"Make sure the models being seeded are valid in accordance with Meshery's schema", "If it is an internal error, please try again after some time"},
)
}

func ErrMissingRegistrant(modelName string) error {
return errors.New(
ErrMissingRegistrantCode,
errors.Alert,
[]string{fmt.Sprintf("Model with name: %s does not have registrant information", modelName)},
[]string{"Meshery models are always registered in context of a registrant."},
// there is only one cause for this error
[]string{""},
[]string{"Make sure that the registrant information is present in the model definition"},
)
}

func ErrRegisterEntity(err error, name, entity string) error {
return errors.New(
ErrRegisterEntityCode,
errors.Alert,
[]string{fmt.Sprintf("Failed to register an entity of name: %s and type: %s into Meshery's registry", name, entity)},
[]string{err.Error()},
[]string{fmt.Sprintf("%s definition might be violating the definition schema", entity), fmt.Sprintf("%s might be missing model details", entity)},
[]string{fmt.Sprintf("ensure the %s definition follows the correct schema", entity), fmt.Sprintf("ensure %s definition belongs to correct model", entity)},
)
}

func ErrGetEntity(err error) error {
return errors.New(
ErrGetEntityCode,
errors.Alert,
[]string{"Could not parse the given data into any Meshery entity"},
[]string{err.Error()},
[]string{"Entity definition might not be in accordance with it's corresponding schema", "Might be an invalid/unsupported schemaVersion"},
[]string{"Ensure that the definition given is in accordance with it's schema", "Ensure that the schemaVersion used is valid and is supported by Meshery"},
)
}

func ErrDirPkgUnitParseFail(dirpath string, err error) error {
return errors.New(
ErrDirPkgUnitParseFailCode,
errors.Alert,
[]string{fmt.Sprintf("Directory at path: %s cannot be registered into Meshery", dirpath)},
[]string{fmt.Sprint(err.Error())},
[]string{"The directory might not have a valid model definition", "Might be some internal issues while walking the file tree"},
[]string{"Make sure that there is a valid model definition present in the directory. Meshery's registration pipeline currently does not support nested models, therefore the behaviour might be unexpected if it contains nested models.", "If there is an internal error, please try again after some time"},
)
}

func ErrImportFailure(hostname string, failedMsg string) error {
return errors.New(
ErrImportFailureCode,
errors.Alert,
[]string{fmt.Sprintf("Errors while registering entities for registrant: %s", hostname)},
[]string{failedMsg},
[]string{"Entity definition might not be in accordance with schema", "Entity version might not be supported by Meshery"},
[]string{"See the registration logs (found at $HOME/.meshery/logs/registry/registry-logs.log) to find out which Entity failed to be imported with more specific error information."},
)
}
14 changes: 6 additions & 8 deletions models/registration/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ import (
"github.com/layer5io/meshkit/models/meshmodel/entity"
)

/*
RegistrationErrorStore stores all the errors that does not break the registration process, but have to be reported nevertheless.
/*
RegistrationErrorStore stores all the errors that does not break the registration process, but have to be reported nevertheless.
*/
type RegistrationErrorStore interface {
AddInvalidDefinition(string,error)
InsertEntityRegError(hostname string, modelName string, entityType entity.EntityType, entityName string, err error)
AddInvalidDefinition(string, error)
InsertEntityRegError(hostname string, modelName string, entityType entity.EntityType, entityName string, err error)
}

// Anything that can be parsed into a packagingUnit is a RegisterableEntity in Meshery server
type RegisterableEntity interface {
/*
1. `err` - this is a breaking error, which signifies that the given entity is invalid and cannot be registered
/*
1. `err` - this is a breaking error, which signifies that the given entity is invalid and cannot be registered
2. Errors encountered while parsing items into meshmodel entites are stored in the RegistrationErrorStore
*/
PkgUnit(RegistrationErrorStore) (packagingUnit, error)
}


Loading

0 comments on commit 8996f15

Please sign in to comment.