Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement objectRemoveKey #686

Merged
merged 1 commit into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.