diff --git a/utils/process.go b/utils/process.go index e0f05b12..0253f3e0 100644 --- a/utils/process.go +++ b/utils/process.go @@ -53,6 +53,7 @@ func init() { process.Alias("xiang.helper.HexToString", "utils.str.Hex") process.Register("utils.str.Join", str.ProcessJoin) process.Register("utils.str.JoinPath", str.ProcessJoinPath) + process.Register("utils.str.UUID", str.ProcessUUID) // Array process.Alias("xiang.helper.ArrayPluck", "utils.arr.Pluck") diff --git a/utils/str/str.go b/utils/str/str.go index 80515c71..478ba12c 100644 --- a/utils/str/str.go +++ b/utils/str/str.go @@ -5,6 +5,7 @@ import ( "path/filepath" "strings" + "github.com/google/uuid" "github.com/yaoapp/gou/process" ) @@ -29,3 +30,9 @@ func ProcessJoinPath(process *process.Process) interface{} { } return filepath.Join(paths...) } + +// ProcessUUID utils.str.uuid +func ProcessUUID(process *process.Process) interface{} { + uuid := uuid.New() + return uuid.String() +} diff --git a/utils/str_test.go b/utils/str_test.go index 124cbfe8..ebd78c69 100644 --- a/utils/str_test.go +++ b/utils/str_test.go @@ -5,6 +5,7 @@ import ( "os" "testing" + "github.com/google/uuid" "github.com/stretchr/testify/assert" "github.com/yaoapp/gou/process" _ "github.com/yaoapp/yao/helper" @@ -21,6 +22,16 @@ func TestProcessStrJoinPath(t *testing.T) { assert.Equal(t, shouldBe, res) } +func TestProcessUUID(t *testing.T) { + res := process.New("utils.str.UUID").Run().(string) + _, err := uuid.Parse(res) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, 36, len(res)) +} + func TestProcessStrHex(t *testing.T) { res, err := process.New("utils.str.Hex", []byte{0x0, 0x1}).Exec() assert.Nil(t, err)