mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
create object fixes
This commit is contained in:
parent
85d059340b
commit
41c08891d3
@ -42,10 +42,12 @@ func (n *CandidateNode) UpdateAttributesFrom(other *CandidateNode) {
|
||||
}
|
||||
n.Node.Kind = other.Node.Kind
|
||||
n.Node.Tag = other.Node.Tag
|
||||
// not sure if this ever should happen here...
|
||||
// if other.Node.Style != 0 {
|
||||
// n.Node.Style = other.Node.Style
|
||||
// }
|
||||
|
||||
// merge will pickup the style of the new thing
|
||||
// when autocreating nodes
|
||||
if n.Node.Style == 0 {
|
||||
n.Node.Style = other.Node.Style
|
||||
}
|
||||
n.Node.FootComment = other.Node.FootComment
|
||||
n.Node.HeadComment = other.Node.HeadComment
|
||||
n.Node.LineComment = other.Node.LineComment
|
||||
|
@ -44,8 +44,10 @@ func collect(d *dataTreeNavigator, aggregate *list.List, remainingMatches *list.
|
||||
splatCandidate := splatEl.Value.(*CandidateNode)
|
||||
newCandidate := aggCandidate.Copy()
|
||||
newCandidate.Path = nil
|
||||
splatCandidateClone := splatCandidate.Copy()
|
||||
splatCandidateClone.Path = nil
|
||||
|
||||
newCandidate, err := multiply(d, newCandidate, splatCandidate)
|
||||
newCandidate, err := multiply(d, newCandidate, splatCandidateClone)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -30,6 +30,15 @@ var collectObjectOperatorScenarios = []expressionScenario{
|
||||
"D0, P[], (!!map)::Mike: dog\nf: burger\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
document: `{name: Mike, pets: {cows: [apl, bba]}}`,
|
||||
expression: `{"a":.name, "b":.pets}`,
|
||||
expected: []string{
|
||||
`D0, P[], (!!map)::a: Mike
|
||||
b: {cows: [apl, bba]}
|
||||
`,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TestCollectObjectOperatorScenarios(t *testing.T) {
|
@ -27,6 +27,14 @@ var createMapOperatorScenarios = []expressionScenario{
|
||||
"D0, P[], (!!seq)::- f: hotdog\n- f: burger\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
document: `{name: Mike, pets: {cows: [apl, bba]}}`,
|
||||
expression: `"a":.name, "b":.pets`,
|
||||
expected: []string{
|
||||
"D0, P[], (!!seq)::- a: Mike\n",
|
||||
"D0, P[], (!!seq)::- b: {cows: [apl, bba]}\n",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TestCreateMapOperatorScenarios(t *testing.T) {
|
||||
|
@ -48,6 +48,8 @@ func MultiplyOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *
|
||||
func multiply(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
|
||||
lhs.Node = UnwrapDoc(lhs.Node)
|
||||
rhs.Node = UnwrapDoc(rhs.Node)
|
||||
log.Debugf("Multipling LHS: %v", NodeToString(lhs))
|
||||
log.Debugf("- RHS: %v", NodeToString(rhs))
|
||||
|
||||
if lhs.Node.Kind == yaml.MappingNode && rhs.Node.Kind == yaml.MappingNode ||
|
||||
(lhs.Node.Kind == yaml.SequenceNode && rhs.Node.Kind == yaml.SequenceNode) {
|
||||
|
@ -61,7 +61,7 @@ b:
|
||||
`,
|
||||
expression: `. * {"a":.b}`,
|
||||
expected: []string{
|
||||
`D0, P[], (!!map)::a: {things: great, also: me}
|
||||
`D0, P[], (!!map)::a: {things: great, also: "me"}
|
||||
b:
|
||||
also: "me"
|
||||
`,
|
||||
|
@ -37,7 +37,7 @@ func traverse(d *dataTreeNavigator, matchingNode *CandidateNode, pathNode *Opera
|
||||
log.Debug("Traversing %v", NodeToString(matchingNode))
|
||||
value := matchingNode.Node
|
||||
|
||||
if value.Tag == "!!null" {
|
||||
if value.Tag == "!!null" && pathNode.Value != "[]" {
|
||||
log.Debugf("Guessing kind")
|
||||
// we must ahve added this automatically, lets guess what it should be now
|
||||
switch pathNode.Value.(type) {
|
||||
@ -45,7 +45,7 @@ func traverse(d *dataTreeNavigator, matchingNode *CandidateNode, pathNode *Opera
|
||||
log.Debugf("probably an array")
|
||||
value.Kind = yaml.SequenceNode
|
||||
default:
|
||||
log.Debugf("probabel a map")
|
||||
log.Debugf("probably a map")
|
||||
value.Kind = yaml.MappingNode
|
||||
}
|
||||
value.Tag = ""
|
||||
|
Loading…
Reference in New Issue
Block a user