Skip to content

Commit

Permalink
feat: implement objectRemoveKey
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitjangid committed May 4, 2023
1 parent 868d9c6 commit aba32e3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
37 changes: 37 additions & 0 deletions builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1969,6 +1969,7 @@ func builtinSum(i *interpreter, arrv value) (value, error) {
return makeValueNumber(sum), nil
}

<<<<<<< HEAD

Check failure on line 1972 in builtins.go

View workflow job for this annotation

GitHub Actions / Test go1.x amd64

syntax error: non-declaration statement outside function body

Check failure on line 1972 in builtins.go

View workflow job for this annotation

GitHub Actions / Test go1.17.x amd64

syntax error: non-declaration statement outside function body

Check failure on line 1972 in builtins.go

View workflow job for this annotation

GitHub Actions / Test go1.18.x amd64

syntax error: non-declaration statement outside function body

Check failure on line 1972 in builtins.go

View workflow job for this annotation

GitHub Actions / Test go1.19.x amd64

syntax error: non-declaration statement outside function body
func builtinContains(i *interpreter, arrv value, ev value) (value, error) {
arr, err := i.getArray(arrv)
if err != nil {
Expand All @@ -1988,6 +1989,41 @@ func builtinContains(i *interpreter, arrv value, ev value) (value, error) {
}
}
return makeValueBoolean(false), nil
=======

Check failure on line 1992 in builtins.go

View workflow job for this annotation

GitHub Actions / Test go1.x amd64

syntax error: unexpected ==, expected }

Check failure on line 1992 in builtins.go

View workflow job for this annotation

GitHub Actions / Test go1.17.x amd64

syntax error: unexpected ==, expecting }

Check failure on line 1992 in builtins.go

View workflow job for this annotation

GitHub Actions / Test go1.18.x amd64

syntax error: unexpected ==, expecting }

Check failure on line 1992 in builtins.go

View workflow job for this annotation

GitHub Actions / Test go1.19.x amd64

syntax error: unexpected ==, expecting }
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
>>>>>>> e46833a (feat: implement objectRemoveKey)

Check failure on line 2026 in builtins.go

View workflow job for this annotation

GitHub Actions / Test go1.x amd64

syntax error: unexpected >>, expected }

Check failure on line 2026 in builtins.go

View workflow job for this annotation

GitHub Actions / Test go1.17.x amd64

syntax error: unexpected >>, expecting }

Check failure on line 2026 in builtins.go

View workflow job for this annotation

GitHub Actions / Test go1.18.x amd64

syntax error: unexpected >>, expecting }

Check failure on line 2026 in builtins.go

View workflow job for this annotation

GitHub Actions / Test go1.19.x amd64

syntax error: unexpected >>, expecting }
}

// Utils for builtins - TODO(sbarzowski) move to a separate file in another commit
Expand Down Expand Up @@ -2256,6 +2292,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 aba32e3

Please sign in to comment.