Fixed to_entries and del bug #1886

This commit is contained in:
Mike Farah 2023-11-30 14:04:54 +11:00
parent c11a53322b
commit 730f240d24
5 changed files with 28 additions and 7 deletions

View File

@ -205,6 +205,7 @@ func (n *CandidateNode) AddKeyValueChild(rawKey *CandidateNode, rawValue *Candid
value := rawValue.Copy()
value.SetParent(n)
value.IsMapKey = false // force this, incase we are creating a value from a key
value.Key = key
n.Content = append(n.Content, key, value)

View File

@ -147,3 +147,16 @@ func TestGetParsedKeyForArrayValue(t *testing.T) {
n := CandidateNode{Key: key, Value: "meow", document: 3}
test.AssertResult(t, 4, n.getParsedKey())
}
func TestCandidateNodeAddKeyValueChild(t *testing.T) {
key := CandidateNode{Value: "cool", IsMapKey: true}
node := CandidateNode{}
// if we use a key in a new node as a value, it should no longer be marked as a key
_, keyIsValueNow := node.AddKeyValueChild(&CandidateNode{Value: "newKey"}, &key)
test.AssertResult(t, keyIsValueNow.IsMapKey, false)
test.AssertResult(t, key.IsMapKey, true)
}

View File

@ -61,7 +61,7 @@ func deleteFromMap(node *CandidateNode, childPath interface{}) {
shouldDelete := key.Value == childPath
log.Debugf("shouldDelete %v ? %v", NodeToString(value), shouldDelete)
log.Debugf("shouldDelete %v? %v == %v = %v", NodeToString(value), key.Value, childPath, shouldDelete)
if !shouldDelete {
newContents = append(newContents, key, value)

View File

@ -8,12 +8,10 @@ import (
func entrySeqFor(key *CandidateNode, value *CandidateNode) *CandidateNode {
var keyKey = &CandidateNode{Kind: ScalarNode, Tag: "!!str", Value: "key"}
var valueKey = &CandidateNode{Kind: ScalarNode, Tag: "!!str", Value: "value"}
return &CandidateNode{
Kind: MappingNode,
Tag: "!!map",
Content: []*CandidateNode{keyKey, key, valueKey, value},
}
candidate := &CandidateNode{Kind: MappingNode, Tag: "!!map"}
candidate.AddKeyValueChild(keyKey, key)
candidate.AddKeyValueChild(valueKey, value)
return candidate
}
func toEntriesFromMap(candidateNode *CandidateNode) *CandidateNode {

View File

@ -5,6 +5,15 @@ import (
)
var entriesOperatorScenarios = []expressionScenario{
{
description: "to_entries, delete key",
skipDoc: true,
document: `{a: 1, b: 2}`,
expression: `to_entries | map(del(.key))`,
expected: []string{
"D0, P[], (!!seq)::- value: 1\n- value: 2\n",
},
},
{
description: "to_entries Map",
document: `{a: 1, b: 2}`,