create object fixes

This commit is contained in:
Mike Farah 2020-10-28 11:34:01 +11:00
parent 85d059340b
commit 41c08891d3
7 changed files with 31 additions and 8 deletions

View File

@ -42,10 +42,12 @@ func (n *CandidateNode) UpdateAttributesFrom(other *CandidateNode) {
} }
n.Node.Kind = other.Node.Kind n.Node.Kind = other.Node.Kind
n.Node.Tag = other.Node.Tag n.Node.Tag = other.Node.Tag
// not sure if this ever should happen here...
// if other.Node.Style != 0 { // merge will pickup the style of the new thing
// n.Node.Style = other.Node.Style // when autocreating nodes
// } if n.Node.Style == 0 {
n.Node.Style = other.Node.Style
}
n.Node.FootComment = other.Node.FootComment n.Node.FootComment = other.Node.FootComment
n.Node.HeadComment = other.Node.HeadComment n.Node.HeadComment = other.Node.HeadComment
n.Node.LineComment = other.Node.LineComment n.Node.LineComment = other.Node.LineComment

View File

@ -44,8 +44,10 @@ func collect(d *dataTreeNavigator, aggregate *list.List, remainingMatches *list.
splatCandidate := splatEl.Value.(*CandidateNode) splatCandidate := splatEl.Value.(*CandidateNode)
newCandidate := aggCandidate.Copy() newCandidate := aggCandidate.Copy()
newCandidate.Path = nil newCandidate.Path = nil
splatCandidateClone := splatCandidate.Copy()
splatCandidateClone.Path = nil
newCandidate, err := multiply(d, newCandidate, splatCandidate) newCandidate, err := multiply(d, newCandidate, splatCandidateClone)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -30,6 +30,15 @@ var collectObjectOperatorScenarios = []expressionScenario{
"D0, P[], (!!map)::Mike: dog\nf: burger\n", "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) { func TestCollectObjectOperatorScenarios(t *testing.T) {

View File

@ -27,6 +27,14 @@ var createMapOperatorScenarios = []expressionScenario{
"D0, P[], (!!seq)::- f: hotdog\n- f: burger\n", "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) { func TestCreateMapOperatorScenarios(t *testing.T) {

View File

@ -48,6 +48,8 @@ func MultiplyOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *
func multiply(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) { func multiply(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
lhs.Node = UnwrapDoc(lhs.Node) lhs.Node = UnwrapDoc(lhs.Node)
rhs.Node = UnwrapDoc(rhs.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 || if lhs.Node.Kind == yaml.MappingNode && rhs.Node.Kind == yaml.MappingNode ||
(lhs.Node.Kind == yaml.SequenceNode && rhs.Node.Kind == yaml.SequenceNode) { (lhs.Node.Kind == yaml.SequenceNode && rhs.Node.Kind == yaml.SequenceNode) {

View File

@ -61,7 +61,7 @@ b:
`, `,
expression: `. * {"a":.b}`, expression: `. * {"a":.b}`,
expected: []string{ expected: []string{
`D0, P[], (!!map)::a: {things: great, also: me} `D0, P[], (!!map)::a: {things: great, also: "me"}
b: b:
also: "me" also: "me"
`, `,

View File

@ -37,7 +37,7 @@ func traverse(d *dataTreeNavigator, matchingNode *CandidateNode, pathNode *Opera
log.Debug("Traversing %v", NodeToString(matchingNode)) log.Debug("Traversing %v", NodeToString(matchingNode))
value := matchingNode.Node value := matchingNode.Node
if value.Tag == "!!null" { if value.Tag == "!!null" && pathNode.Value != "[]" {
log.Debugf("Guessing kind") log.Debugf("Guessing kind")
// we must ahve added this automatically, lets guess what it should be now // we must ahve added this automatically, lets guess what it should be now
switch pathNode.Value.(type) { switch pathNode.Value.(type) {
@ -45,7 +45,7 @@ func traverse(d *dataTreeNavigator, matchingNode *CandidateNode, pathNode *Opera
log.Debugf("probably an array") log.Debugf("probably an array")
value.Kind = yaml.SequenceNode value.Kind = yaml.SequenceNode
default: default:
log.Debugf("probabel a map") log.Debugf("probably a map")
value.Kind = yaml.MappingNode value.Kind = yaml.MappingNode
} }
value.Tag = "" value.Tag = ""