mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
getting closer
This commit is contained in:
parent
ee8cc90307
commit
cd50b35787
@ -138,10 +138,11 @@ func (n *CandidateNode) getParsedKey() interface{} {
|
||||
}
|
||||
|
||||
func (n *CandidateNode) GetPath() []interface{} {
|
||||
if n.Parent != nil && n.getParsedKey() != nil {
|
||||
return append(n.Parent.GetPath(), n.getParsedKey())
|
||||
}
|
||||
key := n.getParsedKey()
|
||||
if n.Parent != nil && key != nil {
|
||||
return append(n.Parent.GetPath(), key)
|
||||
}
|
||||
|
||||
if key != nil {
|
||||
return []interface{}{key}
|
||||
}
|
||||
@ -153,6 +154,8 @@ func (n *CandidateNode) GetNicePath() string {
|
||||
path := n.GetPath()
|
||||
for i, element := range path {
|
||||
elementStr := fmt.Sprintf("%v", element)
|
||||
log.Debugf("element: %v", element)
|
||||
log.Debugf("elementStr: %v", elementStr)
|
||||
switch element.(type) {
|
||||
case int:
|
||||
sb.WriteString("[" + elementStr + "]")
|
||||
@ -176,10 +179,15 @@ func (n *CandidateNode) AsList() *list.List {
|
||||
}
|
||||
|
||||
func (n *CandidateNode) AddKeyValueChild(rawKey *CandidateNode, rawValue *CandidateNode) {
|
||||
value := rawValue.unwrapDocument()
|
||||
value.Key = rawKey.unwrapDocument()
|
||||
value.Key.IsMapKey = true
|
||||
n.Content = append(n.Content, value.Key, value)
|
||||
key := rawKey.unwrapDocument().Copy()
|
||||
key.Parent = n
|
||||
key.IsMapKey = true
|
||||
|
||||
value := rawValue.unwrapDocument().Copy()
|
||||
value.Parent = n
|
||||
value.Key = key
|
||||
|
||||
n.Content = append(n.Content, key, value)
|
||||
}
|
||||
|
||||
func (n *CandidateNode) GetValueRep() (interface{}, error) {
|
||||
@ -233,10 +241,16 @@ func (n *CandidateNode) CopyAsReplacement(replacement *CandidateNode) *Candidate
|
||||
newCopy := replacement.Copy()
|
||||
newCopy.Parent = n.Parent
|
||||
newCopy.Key = n.Key
|
||||
|
||||
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.Filename = n.Filename
|
||||
newCopy.FileIndex = n.FileIndex
|
||||
|
||||
return newCopy
|
||||
}
|
||||
|
||||
@ -275,7 +289,7 @@ func (n *CandidateNode) doCopy(cloneContent bool) *CandidateNode {
|
||||
copyKey = n.Key.Copy()
|
||||
}
|
||||
|
||||
return &CandidateNode{
|
||||
clone := &CandidateNode{
|
||||
Kind: n.Kind,
|
||||
Style: n.Style,
|
||||
|
||||
@ -308,6 +322,12 @@ func (n *CandidateNode) doCopy(cloneContent bool) *CandidateNode {
|
||||
EvaluateTogether: n.EvaluateTogether,
|
||||
IsMapKey: n.IsMapKey,
|
||||
}
|
||||
|
||||
for _, newChild := range content {
|
||||
newChild.Parent = clone
|
||||
}
|
||||
|
||||
return clone
|
||||
}
|
||||
|
||||
// updates this candidate from the given candidate node
|
||||
|
@ -172,7 +172,7 @@ func (o *CandidateNode) UnmarshalYAML(node *yaml.Node, anchorMap map[string]*Can
|
||||
o.Content = make([]*CandidateNode, len(node.Content))
|
||||
for i := 0; i < len(node.Content); i += 1 {
|
||||
keyNode := o.CreateChild()
|
||||
keyNode.IsMapKey = true // can't remember if we need this for sequences
|
||||
keyNode.IsMapKey = true
|
||||
keyNode.Tag = "!!int"
|
||||
keyNode.Kind = ScalarNode
|
||||
keyNode.Value = fmt.Sprintf("%v", i)
|
||||
|
@ -95,6 +95,7 @@ func (o *CandidateNode) UnmarshalJSON(data []byte) error {
|
||||
childKey.Kind = ScalarNode
|
||||
childKey.Tag = "!!int"
|
||||
childKey.Value = fmt.Sprintf("%v", i)
|
||||
childKey.IsMapKey = true
|
||||
|
||||
child.Parent = o
|
||||
child.Document = o.Document
|
||||
|
@ -27,7 +27,9 @@ will output
|
||||
```yaml
|
||||
item_value: &item_value
|
||||
value: true
|
||||
thingOne: false
|
||||
thingOne:
|
||||
name: item_1
|
||||
value: false
|
||||
thingTwo:
|
||||
name: item_2
|
||||
!!merge <<: *item_value
|
||||
|
@ -74,6 +74,39 @@ then
|
||||
yq '[... | {"p": path | join("."), "isKey": is_key, "hc": headComment, "lc": lineComment, "fc": footComment}]' sample.yml
|
||||
```
|
||||
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
|
||||
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
|
||||
```
|
||||
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
|
||||
From the previous example, we know that the comment is on the first child as a headComment
|
||||
|
||||
|
@ -19,7 +19,7 @@ var columnOperatorScenarios = []expressionScenario{
|
||||
document: "a: cat\nb: bob",
|
||||
expression: `.b | key | column`,
|
||||
expected: []string{
|
||||
"D0, P[b], (!!int)::1\n",
|
||||
"D0, P[1], (!!int)::1\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
@ -27,7 +27,7 @@ var columnOperatorScenarios = []expressionScenario{
|
||||
document: "a: cat",
|
||||
expression: `.a | key | column`,
|
||||
expected: []string{
|
||||
"D0, P[a], (!!int)::1\n",
|
||||
"D0, P[1], (!!int)::1\n",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -48,15 +48,19 @@ func sequenceFor(d *dataTreeNavigator, context Context, matchingNode *CandidateN
|
||||
matches.PushBack(matchingNode)
|
||||
}
|
||||
|
||||
log.Debugf("**********sequenceFor %v", NodeToString(matchingNode))
|
||||
|
||||
mapPairs, err := crossFunction(d, context.ChildContext(matches), expressionNode,
|
||||
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.Document = document
|
||||
|
||||
return &node, nil
|
||||
return node, nil
|
||||
}, false)
|
||||
|
||||
if err != nil {
|
||||
|
@ -28,6 +28,22 @@ var createMapOperatorScenarios = []expressionScenario{
|
||||
"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}`,
|
||||
expression: `.name: .age`,
|
||||
|
Loading…
Reference in New Issue
Block a user