Skip to content

Commit

Permalink
Fixed to_entries and del bug #1886
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Nov 30, 2023
1 parent c11a533 commit 730f240
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions pkg/yqlib/candidate_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions pkg/yqlib/candidate_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
2 changes: 1 addition & 1 deletion pkg/yqlib/operator_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 4 additions & 6 deletions pkg/yqlib/operator_entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions pkg/yqlib/operator_entries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down

0 comments on commit 730f240

Please sign in to comment.