+ Helper: MapDel, Throw

This commit is contained in:
Max 2022-01-08 08:58:20 +08:00
parent 53b63557f8
commit 747b395e3d
6 changed files with 98 additions and 0 deletions

View file

@ -31,3 +31,9 @@ func MapSet(record map[string]interface{}, key string, value interface{}) map[st
record[key] = value
return record
}
// MapDel xiang.helper.MapDel 删除数值并返回新映射表
func MapDel(record map[string]interface{}, key string) map[string]interface{} {
delete(record, key)
return record
}

View file

@ -32,3 +32,11 @@ func ProcessMapSet(process *gou.Process) interface{} {
value := process.Args[2]
return MapSet(record, key, value)
}
// ProcessMapDel xiang.helper.MapDel 返回映射表给定键的值
func ProcessMapDel(process *gou.Process) interface{} {
process.ValidateArgNums(2)
record := process.ArgsMap(0)
key := process.ArgsString(1)
return MapDel(record, key)
}

51
helper/map_test.go Normal file
View file

@ -0,0 +1,51 @@
package helper
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/yaoapp/gou"
)
func TestProcessMapDel(t *testing.T) {
args := []interface{}{
map[string]interface{}{"foo": "Value1", "bar": "Value2"},
"bar",
}
new := gou.NewProcess("xiang.helper.MapDel", args...).Run().(map[string]interface{})
_, has := new["bar"]
assert.False(t, has)
assert.Equal(t, "Value1", new["foo"])
}
func TestProcessGetSet(t *testing.T) {
args := []interface{}{
map[string]interface{}{"foo": "Value1"},
"bar",
"Value2",
}
new := gou.NewProcess("xiang.helper.MapSet", args...).Run().(map[string]interface{})
assert.Equal(t, "Value1", new["foo"])
assert.Equal(t, "Value2", new["bar"])
bar := gou.NewProcess("xiang.helper.MapGet", new, "bar").Run().(string)
assert.Equal(t, "Value2", bar)
}
func TestProcessMapKeys(t *testing.T) {
args := []interface{}{
map[string]interface{}{"foo": "Value1", "bar": "Value2"},
}
keys := gou.NewProcess("xiang.helper.MapKeys", args...).Run().([]string)
assert.Contains(t, keys, "foo")
assert.Contains(t, keys, "bar")
}
func TestProcessMapValues(t *testing.T) {
args := []interface{}{
map[string]interface{}{"foo": "Value1", "bar": "Value2"},
}
values := gou.NewProcess("xiang.helper.MapValues", args...).Run().([]interface{})
assert.Contains(t, values, "Value1")
assert.Contains(t, values, "Value2")
}

View file

@ -19,6 +19,7 @@ func init() {
gou.RegisterProcessHandler("xiang.helper.MapValues", ProcessMapValues)
gou.RegisterProcessHandler("xiang.helper.MapGet", ProcessMapGet)
gou.RegisterProcessHandler("xiang.helper.MapSet", ProcessMapSet)
gou.RegisterProcessHandler("xiang.helper.MapDel", ProcessMapDel)
gou.RegisterProcessHandler("xiang.helper.StrConcat", ProcessStrConcat)
@ -41,6 +42,7 @@ func init() {
gou.RegisterProcessHandler("xiang.helper.EnvMultiGet", ProcessEnvMultiGet)
gou.RegisterProcessHandler("xiang.helper.Print", ProcessPrint)
gou.RegisterProcessHandler("xiang.helper.Throw", ProcessThrow)
}

15
helper/throw.process.go Normal file
View file

@ -0,0 +1,15 @@
package helper
import (
"github.com/yaoapp/gou"
"github.com/yaoapp/kun/exception"
)
// ProcessThrow xiang.helper.Throw 抛出异常
func ProcessThrow(process *gou.Process) interface{} {
process.ValidateArgNums(2)
message := process.ArgsString(0)
code := process.ArgsInt(1)
exception.New(message, code).Throw()
return nil
}

16
helper/throw_test.go Normal file
View file

@ -0,0 +1,16 @@
package helper
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/yaoapp/gou"
"github.com/yaoapp/kun/exception"
)
func TestProcessThrow(t *testing.T) {
e := exception.New("Someting error", 500)
assert.PanicsWithValue(t, *e, func() {
gou.NewProcess("xiang.helper.Throw", "Someting error", 500).Run()
})
}