mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 05:38:04 +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.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)
|
||||
|
@ -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)
|
||||
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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 {
|
||||
|
@ -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}`,
|
||||
|
Loading…
Reference in New Issue
Block a user