Skip to content

Commit

Permalink
feat: implement objectRemoveKey (#686)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitjangid authored May 25, 2023
1 parent 868d9c6 commit 3c7c0cb
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
36 changes: 36 additions & 0 deletions builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"}},
Expand Down
4 changes: 4 additions & 0 deletions linter/internal/types/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
4 changes: 4 additions & 0 deletions testdata/builtinObjectRemoveKey.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"bar": 2,
"baz": 3
}
1 change: 1 addition & 0 deletions testdata/builtinObjectRemoveKey.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.objectRemoveKey({foo: 1, bar: 2, baz: 3}, "foo")
Empty file.

0 comments on commit 3c7c0cb

Please sign in to comment.