diff --git a/pkg/yqlib/candidate_node.go b/pkg/yqlib/candidate_node.go index 488eed251e..4762ea67d2 100644 --- a/pkg/yqlib/candidate_node.go +++ b/pkg/yqlib/candidate_node.go @@ -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) diff --git a/pkg/yqlib/candidate_node_test.go b/pkg/yqlib/candidate_node_test.go index 1ca707ba9d..2a09b48a6a 100644 --- a/pkg/yqlib/candidate_node_test.go +++ b/pkg/yqlib/candidate_node_test.go @@ -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) + +} diff --git a/pkg/yqlib/operator_delete.go b/pkg/yqlib/operator_delete.go index 4ca37a83d9..63f05f81d8 100644 --- a/pkg/yqlib/operator_delete.go +++ b/pkg/yqlib/operator_delete.go @@ -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) diff --git a/pkg/yqlib/operator_entries.go b/pkg/yqlib/operator_entries.go index 2279d75bd1..3b19350267 100644 --- a/pkg/yqlib/operator_entries.go +++ b/pkg/yqlib/operator_entries.go @@ -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 { diff --git a/pkg/yqlib/operator_entries_test.go b/pkg/yqlib/operator_entries_test.go index b867b94da9..7d27095aa1 100644 --- a/pkg/yqlib/operator_entries_test.go +++ b/pkg/yqlib/operator_entries_test.go @@ -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}`,