generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #559 from MUzairS15/MUzairS15/fixes
Add standard `Unmarshal` for `yaml/json` data.
- Loading branch information
Showing
14 changed files
with
228 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.