Skip to content

Commit

Permalink
feat: Add support for YAML encoding and decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Jun 19, 2024
1 parent 60bb273 commit c58e9a3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
3 changes: 3 additions & 0 deletions encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 5,7 @@ import (
"github.com/yaoapp/gou/encoding/hex"
"github.com/yaoapp/gou/encoding/json"
"github.com/yaoapp/gou/encoding/xml"
"github.com/yaoapp/gou/encoding/yaml"
"github.com/yaoapp/gou/process"
)

Expand All @@ -15,6 16,8 @@ func init() {
process.Register("encoding.hex.Decode", hex.ProcessDecode)
process.Register("encoding.json.Encode", json.ProcessEncode)
process.Register("encoding.json.Decode", json.ProcessDecode)
process.Register("encoding.yaml.Encode", yaml.ProcessEncode)
process.Register("encoding.yaml.Decode", yaml.ProcessDecode)
process.Register("encoding.xml.Encode", xml.ProcessEncode)
process.Register("encoding.xml.Decode", xml.ProcessDecode)
}
2 changes: 1 addition & 1 deletion encoding/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 23,7 @@ func ProcessDecode(process *process.Process) interface{} {
var res interface{}
err := jsoniter.Unmarshal(data, &res)
if err != nil {
exception.New("Base64 decode error: %s", 500, err).Throw()
exception.New("JSON decode error: %s", 500, err).Throw()
}
return res
}
29 changes: 29 additions & 0 deletions encoding/yaml/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 1,29 @@
package yaml

import (
"github.com/yaoapp/gou/process"
"github.com/yaoapp/kun/exception"
"gopkg.in/yaml.v3"
)

// ProcessEncode json Encode
func ProcessEncode(process *process.Process) interface{} {
process.ValidateArgNums(1)
res, err := yaml.Marshal(process.Args[0])
if err != nil {
exception.New("JSON decode error: %s", 500, err).Throw()
}
return string(res)
}

// ProcessDecode json Decode
func ProcessDecode(process *process.Process) interface{} {
process.ValidateArgNums(1)
data := []byte(process.ArgsString(0))
var res interface{}
err := yaml.Unmarshal(data, &res)
if err != nil {
exception.New("YAML decode error: %s", 500, err).Throw()
}
return res
}
34 changes: 34 additions & 0 deletions encoding/yaml_test.go
Original file line number Diff line number Diff line change
@@ -0,0 1,34 @@
package encoding

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/yaoapp/gou/process"
"gopkg.in/yaml.v3"
)

func TestYAMLEncode(t *testing.T) {
data := []string{"foo", "bar"}
res, err := process.New("encoding.yaml.Encode", data).Exec()
if err != nil {
t.Fatal(err)
}

new := []string{}
err = yaml.Unmarshal([]byte(res.(string)), &new)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, new, data)
}

func TestYAMLDecode(t *testing.T) {
data := fmt.Sprintf("\n- foo\n- bar\n")
res, err := process.New("encoding.yaml.Decode", data).Exec()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, []interface{}{"foo", "bar"}, res)
}

0 comments on commit c58e9a3

Please sign in to comment.