diff --git a/helper/map.go b/helper/map.go index 95c50c4d..121c9ee3 100644 --- a/helper/map.go +++ b/helper/map.go @@ -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 +} diff --git a/helper/map.process.go b/helper/map.process.go index 9ca0e5e7..fde5acb1 100644 --- a/helper/map.process.go +++ b/helper/map.process.go @@ -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) +} diff --git a/helper/map_test.go b/helper/map_test.go new file mode 100644 index 00000000..908c6926 --- /dev/null +++ b/helper/map_test.go @@ -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") +} diff --git a/helper/process.go b/helper/process.go index e84cb220..5068a9a2 100644 --- a/helper/process.go +++ b/helper/process.go @@ -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) } diff --git a/helper/throw.process.go b/helper/throw.process.go new file mode 100644 index 00000000..18740a57 --- /dev/null +++ b/helper/throw.process.go @@ -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 +} diff --git a/helper/throw_test.go b/helper/throw_test.go new file mode 100644 index 00000000..74eb4e30 --- /dev/null +++ b/helper/throw_test.go @@ -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() + }) +}