From 3c7c0cbac801b5e3b9b3286b8321eaa824d33027 Mon Sep 17 00:00:00 2001 From: Rohit Jangid Date: Thu, 25 May 2023 17:25:30 +0530 Subject: [PATCH] feat: implement objectRemoveKey (#686) --- builtins.go | 36 +++++++++++++++++++ linter/internal/types/stdlib.go | 4 +++ testdata/builtinObjectRemoveKey.golden | 4 +++ testdata/builtinObjectRemoveKey.jsonnet | 1 + testdata/builtinObjectRemoveKey.linter.golden | 0 5 files changed, 45 insertions(+) create mode 100644 testdata/builtinObjectRemoveKey.golden create mode 100644 testdata/builtinObjectRemoveKey.jsonnet create mode 100644 testdata/builtinObjectRemoveKey.linter.golden diff --git a/builtins.go b/builtins.go index 875fd5f24..10b180f5d 100644 --- a/builtins.go +++ b/builtins.go @@ -1990,6 +1990,41 @@ func builtinContains(i *interpreter, arrv value, ev value) (value, error) { return makeValueBoolean(false), nil } +func builtInObjectRemoveKey(i *interpreter, objv value, keyv value) (value, error) { + obj, err := i.getObject(objv) + if err != nil { + return nil, err + } + key, err := i.getString(keyv) + if err != nil { + return nil, err + } + + newFields := make(simpleObjectFieldMap) + simpleObj := obj.uncached.(*simpleObject) + for fieldName, fieldVal := range simpleObj.fields { + if fieldName == key.getGoString() { + // skip the field which needs to be deleted + continue + } + + newFields[fieldName] = simpleObjectField{ + hide: fieldVal.hide, + field: &bindingsUnboundField{ + inner: fieldVal.field, + bindings: simpleObj.upValues, + }, + } + } + + return makeValueSimpleObject( + nil, + newFields, + []unboundField{}, // No asserts allowed + nil, + ), nil +} + // Utils for builtins - TODO(sbarzowski) move to a separate file in another commit type builtin interface { @@ -2256,6 +2291,7 @@ var funcBuiltins = buildBuiltinMap([]builtin{ &binaryBuiltin{name: "equals", function: builtinEquals, params: ast.Identifiers{"x", "y"}}, &binaryBuiltin{name: "objectFieldsEx", function: builtinObjectFieldsEx, params: ast.Identifiers{"obj", "hidden"}}, &ternaryBuiltin{name: "objectHasEx", function: builtinObjectHasEx, params: ast.Identifiers{"obj", "fname", "hidden"}}, + &binaryBuiltin{name: "objectRemoveKey", function: builtInObjectRemoveKey, params: ast.Identifiers{"obj", "key"}}, &unaryBuiltin{name: "type", function: builtinType, params: ast.Identifiers{"x"}}, &unaryBuiltin{name: "char", function: builtinChar, params: ast.Identifiers{"n"}}, &unaryBuiltin{name: "codepoint", function: builtinCodepoint, params: ast.Identifiers{"str"}}, diff --git a/linter/internal/types/stdlib.go b/linter/internal/types/stdlib.go index dee12b27c..6d007985c 100644 --- a/linter/internal/types/stdlib.go +++ b/linter/internal/types/stdlib.go @@ -151,6 +151,10 @@ func prepareStdlib(g *typeGraph) { "setDiff": g.newFuncType(anyArrayType, []ast.Parameter{required("a"), required("b"), optional("keyF")}), "setMember": g.newFuncType(boolType, []ast.Parameter{required("x"), required("arr"), optional("keyF")}), + // Objects + + "objectRemoveKey": g.newSimpleFuncType(anyObjectType, "obj", "key"), + // Encoding "base64": g.newSimpleFuncType(stringType, "input"), diff --git a/testdata/builtinObjectRemoveKey.golden b/testdata/builtinObjectRemoveKey.golden new file mode 100644 index 000000000..6eb329c5e --- /dev/null +++ b/testdata/builtinObjectRemoveKey.golden @@ -0,0 +1,4 @@ +{ + "bar": 2, + "baz": 3 +} diff --git a/testdata/builtinObjectRemoveKey.jsonnet b/testdata/builtinObjectRemoveKey.jsonnet new file mode 100644 index 000000000..c4ac248e6 --- /dev/null +++ b/testdata/builtinObjectRemoveKey.jsonnet @@ -0,0 +1 @@ +std.objectRemoveKey({foo: 1, bar: 2, baz: 3}, "foo") \ No newline at end of file diff --git a/testdata/builtinObjectRemoveKey.linter.golden b/testdata/builtinObjectRemoveKey.linter.golden new file mode 100644 index 000000000..e69de29bb