mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-27 08:55:37 +00:00
Fixed to_entries and del bug #1886
This commit is contained in:
parent
c11a53322b
commit
730f240d24
@ -205,6 +205,7 @@ func (n *CandidateNode) AddKeyValueChild(rawKey *CandidateNode, rawValue *Candid
|
|||||||
|
|
||||||
value := rawValue.Copy()
|
value := rawValue.Copy()
|
||||||
value.SetParent(n)
|
value.SetParent(n)
|
||||||
|
value.IsMapKey = false // force this, incase we are creating a value from a key
|
||||||
value.Key = key
|
value.Key = key
|
||||||
|
|
||||||
n.Content = append(n.Content, key, value)
|
n.Content = append(n.Content, key, value)
|
||||||
|
@ -147,3 +147,16 @@ func TestGetParsedKeyForArrayValue(t *testing.T) {
|
|||||||
n := CandidateNode{Key: key, Value: "meow", document: 3}
|
n := CandidateNode{Key: key, Value: "meow", document: 3}
|
||||||
test.AssertResult(t, 4, n.getParsedKey())
|
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)
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -61,7 +61,7 @@ func deleteFromMap(node *CandidateNode, childPath interface{}) {
|
|||||||
|
|
||||||
shouldDelete := key.Value == childPath
|
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 {
|
if !shouldDelete {
|
||||||
newContents = append(newContents, key, value)
|
newContents = append(newContents, key, value)
|
||||||
|
@ -8,12 +8,10 @@ import (
|
|||||||
func entrySeqFor(key *CandidateNode, value *CandidateNode) *CandidateNode {
|
func entrySeqFor(key *CandidateNode, value *CandidateNode) *CandidateNode {
|
||||||
var keyKey = &CandidateNode{Kind: ScalarNode, Tag: "!!str", Value: "key"}
|
var keyKey = &CandidateNode{Kind: ScalarNode, Tag: "!!str", Value: "key"}
|
||||||
var valueKey = &CandidateNode{Kind: ScalarNode, Tag: "!!str", Value: "value"}
|
var valueKey = &CandidateNode{Kind: ScalarNode, Tag: "!!str", Value: "value"}
|
||||||
|
candidate := &CandidateNode{Kind: MappingNode, Tag: "!!map"}
|
||||||
return &CandidateNode{
|
candidate.AddKeyValueChild(keyKey, key)
|
||||||
Kind: MappingNode,
|
candidate.AddKeyValueChild(valueKey, value)
|
||||||
Tag: "!!map",
|
return candidate
|
||||||
Content: []*CandidateNode{keyKey, key, valueKey, value},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func toEntriesFromMap(candidateNode *CandidateNode) *CandidateNode {
|
func toEntriesFromMap(candidateNode *CandidateNode) *CandidateNode {
|
||||||
|
@ -5,6 +5,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var entriesOperatorScenarios = []expressionScenario{
|
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",
|
description: "to_entries Map",
|
||||||
document: `{a: 1, b: 2}`,
|
document: `{a: 1, b: 2}`,
|
||||||
|
Loading…
Reference in New Issue
Block a user