1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-12-25 23:37:06 +02:00
Files
ferret/pkg/stdlib/objects/keep_keys.go
Tim Voronov 24752c0f48 Updated ANTLR and other dependencies. Upgraded Go version (#796)
* Updated ANTLR and other dependencies. Upgraded Go version

* Updated unit tests

* Updated CodeQL

* Fixed linting errors

* Updated build pipeline

* Reverted refactoring of build pipeline

* Removed outdated examples from E2E tests
2025-05-07 10:50:17 -04:00

65 lines
1.3 KiB
Go

package objects
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)
// KEEP_KEYS returns a new object with only given keys.
// @param {Object} obj - Source object.
// @param {String, repeated} keys - Keys that need to be kept.
// @return {Object} - New Object with only given keys.
func KeepKeys(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 2, core.MaxArgs)
if err != nil {
return values.None, err
}
err = core.ValidateType(args[0], types.Object)
if err != nil {
return values.None, err
}
keys := values.NewArrayWith(args[1:]...)
if len(args) == 2 && args[1].Type().Equals(types.Array) {
keys = args[1].(*values.Array)
}
err = validateArrayOf(types.String, keys)
if err != nil {
return values.None, err
}
obj := args[0].(*values.Object)
resultObj := values.NewObject()
var key values.String
var val core.Value
var exists values.Boolean
keys.ForEach(func(keyVal core.Value, _ int) bool {
key = keyVal.(values.String)
if val, exists = obj.Get(key); exists {
cloneable, ok := val.(core.Cloneable)
if ok {
val = cloneable.Clone()
}
resultObj.Set(key, val)
}
return true
})
return resultObj, nil
}