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 branch 'master' into ctcarrier/bug/fix-artifacthub-created-format
Signed-off-by: Mohd Hamza <[email protected]>
- Loading branch information
Showing
51 changed files
with
869 additions
and
1,217 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
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,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 | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package artifacthub | ||
|
||
const ( | ||
ArtifactHub = "artifacthub" | ||
) |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package github | ||
|
||
const ( | ||
GitHub = "github" | ||
) |
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
Oops, something went wrong.