Skip to content

Commit

Permalink
feat: implement objectRemoveKey
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitjangid committed Apr 17, 2023
1 parent c484aec commit e46833a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
36 changes: 36 additions & 0 deletions builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1935,6 +1935,41 @@ func builtinSum(i *interpreter, arrv value) (value, error) {
return makeValueNumber(sum), 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 @@ -2245,6 +2280,7 @@ var funcBuiltins = buildBuiltinMap([]builtin{
&generalBuiltin{name: "sort", function: builtinSort, params: []generalBuiltinParameter{{name: "arr"}, {name: "keyF", defaultValue: functionID}}},
&unaryBuiltin{name: "native", function: builtinNative, params: ast.Identifiers{"x"}},
&unaryBuiltin{name: "sum", function: builtinSum, params: ast.Identifiers{"arr"}},
&binaryBuiltin{name: "objectRemoveKey", function: builtInObjectRemoveKey, params: ast.Identifiers{"obj", "key"}},

// internal
&unaryBuiltin{name: "$objectFlatMerge", function: builtinUglyObjectFlatMerge, params: ast.Identifiers{"x"}},
Expand Down
8 changes: 6 additions & 2 deletions linter/internal/types/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,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 Expand Up @@ -176,8 +180,8 @@ func prepareStdlib(g *typeGraph) {

// Boolean

"xor": g.newSimpleFuncType(boolType, "x", "y"),
"xnor": g.newSimpleFuncType(boolType, "x", "y"),
"xor": g.newSimpleFuncType(boolType, "x", "y"),
"xnor": g.newSimpleFuncType(boolType, "x", "y"),
}

fieldContains := map[string][]placeholderID{}
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 e46833a

Please sign in to comment.