From 0ccdb0503953fed1b67d8724073becd4a1bd12aa Mon Sep 17 00:00:00 2001 From: Cyril Tovena Date: Thu, 26 Sep 2019 23:26:36 -0400 Subject: [PATCH] implement simple del function on object --- compliance/functions.json | 15 +++++++++++++++ functions.go | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/compliance/functions.json b/compliance/functions.json index 8b8db36..c4334d5 100644 --- a/compliance/functions.json +++ b/compliance/functions.json @@ -821,5 +821,20 @@ "result": [[1, 2, 3, 4], [5, 6, 7, 8, 9]] } ] +}, +{ + "given": { + "array": {}, + "deleteme": "ok", + "foo": [1,2,3,4] + }, + "cases": [ + { + "expression": "del(@,'deleteme','foo')", + "result": { + "array": {} + } + } + ] } ] diff --git a/functions.go b/functions.go index 9b7cd89..943ebbe 100644 --- a/functions.go +++ b/functions.go @@ -319,6 +319,14 @@ func newFunctionCaller() *functionCaller { }, handler: jpfNotNull, }, + "del": { + name: "del", + arguments: []argSpec{ + {types: []jpType{jpObject}}, + {types: []jpType{jpString}, variadic: true}, + }, + handler: jpfDel, + }, } return caller } @@ -482,6 +490,16 @@ func jpfMap(arguments []interface{}) (interface{}, error) { } return mapped, nil } + +func jpfDel(arguments []interface{}) (interface{}, error) { + obj := arguments[0].(map[string]interface{}) + for i := 1; i < len(arguments); i++ { + key := arguments[i].(string) + delete(obj, key) + } + return obj, nil +} + func jpfMax(arguments []interface{}) (interface{}, error) { if items, ok := toArrayNum(arguments[0]); ok { if len(items) == 0 {