Skip to content

Commit 730f240

Browse files
committed
Fixed to_entries and del bug #1886
1 parent c11a533 commit 730f240

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

pkg/yqlib/candidate_node.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ func (n *CandidateNode) AddKeyValueChild(rawKey *CandidateNode, rawValue *Candid
205205

206206
value := rawValue.Copy()
207207
value.SetParent(n)
208+
value.IsMapKey = false // force this, incase we are creating a value from a key
208209
value.Key = key
209210

210211
n.Content = append(n.Content, key, value)

pkg/yqlib/candidate_node_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,16 @@ func TestGetParsedKeyForArrayValue(t *testing.T) {
147147
n := CandidateNode{Key: key, Value: "meow", document: 3}
148148
test.AssertResult(t, 4, n.getParsedKey())
149149
}
150+
151+
func TestCandidateNodeAddKeyValueChild(t *testing.T) {
152+
key := CandidateNode{Value: "cool", IsMapKey: true}
153+
node := CandidateNode{}
154+
155+
// if we use a key in a new node as a value, it should no longer be marked as a key
156+
157+
_, keyIsValueNow := node.AddKeyValueChild(&CandidateNode{Value: "newKey"}, &key)
158+
159+
test.AssertResult(t, keyIsValueNow.IsMapKey, false)
160+
test.AssertResult(t, key.IsMapKey, true)
161+
162+
}

pkg/yqlib/operator_delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func deleteFromMap(node *CandidateNode, childPath interface{}) {
6161

6262
shouldDelete := key.Value == childPath
6363

64-
log.Debugf("shouldDelete %v ? %v", NodeToString(value), shouldDelete)
64+
log.Debugf("shouldDelete %v? %v == %v = %v", NodeToString(value), key.Value, childPath, shouldDelete)
6565

6666
if !shouldDelete {
6767
newContents = append(newContents, key, value)

pkg/yqlib/operator_entries.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ import (
88
func entrySeqFor(key *CandidateNode, value *CandidateNode) *CandidateNode {
99
var keyKey = &CandidateNode{Kind: ScalarNode, Tag: "!!str", Value: "key"}
1010
var valueKey = &CandidateNode{Kind: ScalarNode, Tag: "!!str", Value: "value"}
11-
12-
return &CandidateNode{
13-
Kind: MappingNode,
14-
Tag: "!!map",
15-
Content: []*CandidateNode{keyKey, key, valueKey, value},
16-
}
11+
candidate := &CandidateNode{Kind: MappingNode, Tag: "!!map"}
12+
candidate.AddKeyValueChild(keyKey, key)
13+
candidate.AddKeyValueChild(valueKey, value)
14+
return candidate
1715
}
1816

1917
func toEntriesFromMap(candidateNode *CandidateNode) *CandidateNode {

pkg/yqlib/operator_entries_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import (
55
)
66

77
var entriesOperatorScenarios = []expressionScenario{
8+
{
9+
description: "to_entries, delete key",
10+
skipDoc: true,
11+
document: `{a: 1, b: 2}`,
12+
expression: `to_entries | map(del(.key))`,
13+
expected: []string{
14+
"D0, P[], (!!seq)::- value: 1\n- value: 2\n",
15+
},
16+
},
817
{
918
description: "to_entries Map",
1019
document: `{a: 1, b: 2}`,

0 commit comments

Comments
 (0)