getting closer

This commit is contained in:
Mike Farah 2023-04-17 17:23:48 +10:00
parent ee8cc90307
commit cd50b35787
8 changed files with 115 additions and 14 deletions

View File

@ -138,10 +138,11 @@ func (n *CandidateNode) getParsedKey() interface{} {
} }
func (n *CandidateNode) GetPath() []interface{} { func (n *CandidateNode) GetPath() []interface{} {
if n.Parent != nil && n.getParsedKey() != nil {
return append(n.Parent.GetPath(), n.getParsedKey())
}
key := n.getParsedKey() key := n.getParsedKey()
if n.Parent != nil && key != nil {
return append(n.Parent.GetPath(), key)
}
if key != nil { if key != nil {
return []interface{}{key} return []interface{}{key}
} }
@ -153,6 +154,8 @@ func (n *CandidateNode) GetNicePath() string {
path := n.GetPath() path := n.GetPath()
for i, element := range path { for i, element := range path {
elementStr := fmt.Sprintf("%v", element) elementStr := fmt.Sprintf("%v", element)
log.Debugf("element: %v", element)
log.Debugf("elementStr: %v", elementStr)
switch element.(type) { switch element.(type) {
case int: case int:
sb.WriteString("[" + elementStr + "]") sb.WriteString("[" + elementStr + "]")
@ -176,10 +179,15 @@ func (n *CandidateNode) AsList() *list.List {
} }
func (n *CandidateNode) AddKeyValueChild(rawKey *CandidateNode, rawValue *CandidateNode) { func (n *CandidateNode) AddKeyValueChild(rawKey *CandidateNode, rawValue *CandidateNode) {
value := rawValue.unwrapDocument() key := rawKey.unwrapDocument().Copy()
value.Key = rawKey.unwrapDocument() key.Parent = n
value.Key.IsMapKey = true key.IsMapKey = true
n.Content = append(n.Content, value.Key, value)
value := rawValue.unwrapDocument().Copy()
value.Parent = n
value.Key = key
n.Content = append(n.Content, key, value)
} }
func (n *CandidateNode) GetValueRep() (interface{}, error) { func (n *CandidateNode) GetValueRep() (interface{}, error) {
@ -233,10 +241,16 @@ func (n *CandidateNode) CopyAsReplacement(replacement *CandidateNode) *Candidate
newCopy := replacement.Copy() newCopy := replacement.Copy()
newCopy.Parent = n.Parent newCopy.Parent = n.Parent
newCopy.Key = n.Key newCopy.Key = n.Key
newCopy.IsMapKey = n.IsMapKey newCopy.IsMapKey = n.IsMapKey
log.Debugf("n path: %v", n.GetNicePath())
log.Debugf("HERE**************")
log.Debugf("newCopy path: %v", newCopy.GetNicePath())
newCopy.Document = n.Document newCopy.Document = n.Document
newCopy.Filename = n.Filename newCopy.Filename = n.Filename
newCopy.FileIndex = n.FileIndex newCopy.FileIndex = n.FileIndex
return newCopy return newCopy
} }
@ -275,7 +289,7 @@ func (n *CandidateNode) doCopy(cloneContent bool) *CandidateNode {
copyKey = n.Key.Copy() copyKey = n.Key.Copy()
} }
return &CandidateNode{ clone := &CandidateNode{
Kind: n.Kind, Kind: n.Kind,
Style: n.Style, Style: n.Style,
@ -308,6 +322,12 @@ func (n *CandidateNode) doCopy(cloneContent bool) *CandidateNode {
EvaluateTogether: n.EvaluateTogether, EvaluateTogether: n.EvaluateTogether,
IsMapKey: n.IsMapKey, IsMapKey: n.IsMapKey,
} }
for _, newChild := range content {
newChild.Parent = clone
}
return clone
} }
// updates this candidate from the given candidate node // updates this candidate from the given candidate node

View File

@ -172,7 +172,7 @@ func (o *CandidateNode) UnmarshalYAML(node *yaml.Node, anchorMap map[string]*Can
o.Content = make([]*CandidateNode, len(node.Content)) o.Content = make([]*CandidateNode, len(node.Content))
for i := 0; i < len(node.Content); i += 1 { for i := 0; i < len(node.Content); i += 1 {
keyNode := o.CreateChild() keyNode := o.CreateChild()
keyNode.IsMapKey = true // can't remember if we need this for sequences keyNode.IsMapKey = true
keyNode.Tag = "!!int" keyNode.Tag = "!!int"
keyNode.Kind = ScalarNode keyNode.Kind = ScalarNode
keyNode.Value = fmt.Sprintf("%v", i) keyNode.Value = fmt.Sprintf("%v", i)

View File

@ -95,6 +95,7 @@ func (o *CandidateNode) UnmarshalJSON(data []byte) error {
childKey.Kind = ScalarNode childKey.Kind = ScalarNode
childKey.Tag = "!!int" childKey.Tag = "!!int"
childKey.Value = fmt.Sprintf("%v", i) childKey.Value = fmt.Sprintf("%v", i)
childKey.IsMapKey = true
child.Parent = o child.Parent = o
child.Document = o.Document child.Document = o.Document

View File

@ -27,7 +27,9 @@ will output
```yaml ```yaml
item_value: &item_value item_value: &item_value
value: true value: true
thingOne: false thingOne:
name: item_1
value: false
thingTwo: thingTwo:
name: item_2 name: item_2
!!merge <<: *item_value !!merge <<: *item_value

View File

@ -74,6 +74,39 @@ then
yq '[... | {"p": path | join("."), "isKey": is_key, "hc": headComment, "lc": lineComment, "fc": footComment}]' sample.yml yq '[... | {"p": path | join("."), "isKey": is_key, "hc": headComment, "lc": lineComment, "fc": footComment}]' sample.yml
``` ```
will output will output
```yaml
- p: ""
isKey: false
hc: ""
lc: ""
fc: ""
- p: hello
isKey: null
true: null
hc: null
"": null
lc: null
hello-world-comment: null
fc: null
- p: hello
isKey: false
hc: ""
lc: ""
fc: ""
- p: hello.message
isKey: null
true: null
hc: null
"": null
lc: null
fc: null
- p: hello.message
isKey: false
hc: ""
lc: ""
fc: ""
```
## Retrieve comment - map key example ## Retrieve comment - map key example
From the previous example, we know that the comment is on the 'hello' _key_ as a lineComment From the previous example, we know that the comment is on the 'hello' _key_ as a lineComment
@ -106,6 +139,31 @@ then
yq '[... | {"p": path | join("."), "isKey": is_key, "hc": headComment, "lc": lineComment, "fc": footComment}]' sample.yml yq '[... | {"p": path | join("."), "isKey": is_key, "hc": headComment, "lc": lineComment, "fc": footComment}]' sample.yml
``` ```
will output will output
```yaml
- p: ""
isKey: false
hc: ""
lc: ""
fc: ""
- p: name
isKey: null
true: null
hc: null
"": null
lc: null
fc: null
- p: name
isKey: false
hc: ""
lc: ""
fc: ""
- p: name.0
isKey: false
hc: under-name-comment
lc: ""
fc: ""
```
## Retrieve comment - array example ## Retrieve comment - array example
From the previous example, we know that the comment is on the first child as a headComment From the previous example, we know that the comment is on the first child as a headComment

View File

@ -19,7 +19,7 @@ var columnOperatorScenarios = []expressionScenario{
document: "a: cat\nb: bob", document: "a: cat\nb: bob",
expression: `.b | key | column`, expression: `.b | key | column`,
expected: []string{ expected: []string{
"D0, P[b], (!!int)::1\n", "D0, P[1], (!!int)::1\n",
}, },
}, },
{ {
@ -27,7 +27,7 @@ var columnOperatorScenarios = []expressionScenario{
document: "a: cat", document: "a: cat",
expression: `.a | key | column`, expression: `.a | key | column`,
expected: []string{ expected: []string{
"D0, P[a], (!!int)::1\n", "D0, P[1], (!!int)::1\n",
}, },
}, },
{ {

View File

@ -48,15 +48,19 @@ func sequenceFor(d *dataTreeNavigator, context Context, matchingNode *CandidateN
matches.PushBack(matchingNode) matches.PushBack(matchingNode)
} }
log.Debugf("**********sequenceFor %v", NodeToString(matchingNode))
mapPairs, err := crossFunction(d, context.ChildContext(matches), expressionNode, mapPairs, err := crossFunction(d, context.ChildContext(matches), expressionNode,
func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) { func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
node := CandidateNode{Kind: MappingNode, Tag: "!!map"} node := &CandidateNode{Kind: MappingNode, Tag: "!!map"}
log.Debugf("**********adding key %v and value %v", NodeToString(lhs), NodeToString(rhs))
node.AddKeyValueChild(lhs, rhs) node.AddKeyValueChild(lhs, rhs)
node.Document = document node.Document = document
return &node, nil return node, nil
}, false) }, false)
if err != nil { if err != nil {

View File

@ -28,6 +28,22 @@ var createMapOperatorScenarios = []expressionScenario{
"D0, P[frog], (!!str)::jumps\n", "D0, P[frog], (!!str)::jumps\n",
}, },
}, },
{
document: `{name: Mike, pets: [cat, dog]}`,
expression: `(.name: .pets.[]) | .[0][0] | ..`,
expected: []string{
"D0, P[], (!!map)::Mike: cat\n",
"D0, P[Mike], (!!str)::cat\n",
},
},
{
description: "check path of nested child",
document: "pets:\n cows: value",
expression: `("b":.pets) | .[0][0] | .b.cows`,
expected: []string{
"D0, P[b cows], (!!str)::value\n",
},
},
{ {
document: `{name: Mike, age: 32}`, document: `{name: Mike, age: 32}`,
expression: `.name: .age`, expression: `.name: .age`,