[add] xiang.helper.HexToString process

This commit is contained in:
Max 2022-03-01 20:47:14 +08:00
parent 8566db96b0
commit 80965c78e3
6 changed files with 118 additions and 1 deletions

View file

@ -20,5 +20,5 @@ func check(t *testing.T) {
for key := range gou.Flows {
keys = append(keys, key)
}
assert.Equal(t, 22, len(keys))
assert.Equal(t, 23, len(keys))
}

22
helper/hex.process.go Normal file
View file

@ -0,0 +1,22 @@
package helper
import (
"encoding/hex"
"github.com/yaoapp/gou"
"github.com/yaoapp/kun/log"
)
// ProcessHexToString xiang.helper.HexToString
func ProcessHexToString(process *gou.Process) interface{} {
process.ValidateArgNums(1)
switch process.Args[0].(type) {
case string:
return hex.EncodeToString([]byte(process.Args[0].(string)))
case []byte:
return hex.EncodeToString(process.Args[0].([]byte))
}
log.With(log.F{"input": process.Args[0]}).Error("HexToString: type does not support")
return nil
}

61
helper/hex_test.go Normal file
View file

@ -0,0 +1,61 @@
package helper
import (
"os"
"path"
"testing"
"github.com/stretchr/testify/assert"
"github.com/yaoapp/gou"
"github.com/yaoapp/yao/flow"
"github.com/yaoapp/yao/share"
)
func TestProcessHexToString(t *testing.T) {
res, err := gou.NewProcess("xiang.helper.HexToString", []byte{0x0, 0x1}).Exec()
assert.Nil(t, err)
assert.Equal(t, "0001", res)
res, err = gou.NewProcess("xiang.helper.HexToString", string([]byte{0x0, 0x1})).Exec()
assert.Nil(t, err)
assert.Equal(t, "0001", res)
res, err = gou.NewProcess("xiang.helper.HexToString", 1024).Exec()
assert.Nil(t, err)
assert.Nil(t, res)
}
func TestProcessHexToStringInFlow(t *testing.T) {
testflow := path.Join(os.Getenv("YAO_DEV"), "tests", "flows", "helper")
flow.LoadFrom(testflow, "helper.")
res, err := gou.NewProcess("flows.helper.HexToString").Exec()
assert.Nil(t, err)
assert.Equal(t, "6162", res) // ab
}
func TestProcessHexToStringInScript(t *testing.T) {
testscirpt := path.Join(os.Getenv("YAO_DEV"), "tests", "scripts")
share.LoadFrom(testscirpt)
res, err := gou.NewProcess("scripts.helper.HexToStringString").Exec()
assert.Nil(t, err)
assert.Equal(t, "6162", res) // ab
res, err = gou.NewProcess("scripts.helper.HexToStringBytes", []byte{0x0, 0x1}).Exec()
assert.Nil(t, err)
assert.Equal(t, "0001", res) // []byte{0x0, 0x1}
}
func TestProcessBufferInScript(t *testing.T) {
testscirpt := path.Join(os.Getenv("YAO_DEV"), "tests", "scripts")
share.LoadFrom(testscirpt)
res, err := gou.NewProcess("scripts.helper.Buffer").Exec()
assert.Nil(t, err)
str, ok := res.(string)
assert.True(t, ok)
assert.Equal(t, []byte{0x0, 0x1}, []byte(str))
}

View file

@ -27,6 +27,8 @@ func init() {
gou.RegisterProcessHandler("xiang.helper.MapDel", ProcessMapDel)
gou.RegisterProcessHandler("xiang.helper.MapMultiDel", ProcessMapMultiDel)
gou.RegisterProcessHandler("xiang.helper.HexToString", ProcessHexToString)
gou.RegisterProcessHandler("xiang.helper.StrConcat", ProcessStrConcat)
gou.RegisterProcessHandler("xiang.helper.Captcha", ProcessCaptcha)
@ -64,6 +66,7 @@ func init() {
gou.RegisterProcessHandler("xiang.flow.Sleep", ProcessSleep)
gou.AliasProcess("xiang.flow.Sleep", "xiang.sys.Sleep")
}
// ProcessPrint xiang.helper.Print 打印语句

View file

@ -0,0 +1,13 @@
{
"label": "HexToString",
"version": "1.0.0",
"description": "HexToString",
"nodes": [
{
"name": "Value",
"process": "xiang.helper.HexToString",
"args": ["ab"]
}
],
"output": "{{$res.Value}}"
}

18
tests/scripts/helper.js Normal file
View file

@ -0,0 +1,18 @@
function HexToStringString() {
return Process("xiang.helper.HexToString", "ab");
}
function HexToStringBytes(data) {
return Process("xiang.helper.HexToString", data);
}
function Buffer() {
var buffer = new Uint8Array(2);
buffer[0] = 0x0;
buffer[1] = 0x1;
return BufferToString(buffer);
}
function BufferToString(buffer) {
return String.fromCharCode.apply(null, new Uint8Array(buffer));
}