more docs

This commit is contained in:
Mike Farah 2020-11-14 13:38:44 +11:00
parent 860655b4cd
commit db4762ef7c
6 changed files with 117 additions and 52 deletions

View File

@ -5,8 +5,8 @@ import (
"container/list" "container/list"
"fmt" "fmt"
"gopkg.in/op/go-logging.v1" logging "gopkg.in/op/go-logging.v1"
"gopkg.in/yaml.v3" yaml "gopkg.in/yaml.v3"
) )
type OperationType struct { type OperationType struct {
@ -70,7 +70,7 @@ var RecursiveDescent = &OperationType{Type: "RECURSIVE_DESCENT", NumArgs: 0, Pre
var Select = &OperationType{Type: "SELECT", NumArgs: 1, Precedence: 50, Handler: SelectOperator} var Select = &OperationType{Type: "SELECT", NumArgs: 1, Precedence: 50, Handler: SelectOperator}
var DeleteChild = &OperationType{Type: "DELETE", NumArgs: 2, Precedence: 40, Handler: DeleteChildOperator} var DeleteChild = &OperationType{Type: "DELETE", NumArgs: 1, Precedence: 40, Handler: DeleteChildOperator}
// var Exists = &OperationType{Type: "Length", NumArgs: 2, Precedence: 35} // var Exists = &OperationType{Type: "Length", NumArgs: 2, Precedence: 35}
// filters matches if they have the existing path // filters matches if they have the existing path

View File

@ -6,13 +6,23 @@ import (
var assignOperatorScenarios = []expressionScenario{ var assignOperatorScenarios = []expressionScenario{
{ {
document: `{a: {b: apple}}`, description: "Update parent to be the child value",
expression: `.a.b |= "frog"`, document: `{a: {b: {g: foof}}}`,
expression: `.a |= .b`,
expected: []string{
"D0, P[], (doc)::{a: {g: foof}}\n",
},
},
{
description: "Update string value",
document: `{a: {b: apple}}`,
expression: `.a.b |= "frog"`,
expected: []string{ expected: []string{
"D0, P[], (doc)::{a: {b: frog}}\n", "D0, P[], (doc)::{a: {b: frog}}\n",
}, },
}, },
{ {
skipDoc: true,
document: `{a: {b: apple}}`, document: `{a: {b: apple}}`,
expression: `.a.b | (. |= "frog")`, expression: `.a.b | (. |= "frog")`,
expected: []string{ expected: []string{
@ -20,6 +30,7 @@ var assignOperatorScenarios = []expressionScenario{
}, },
}, },
{ {
skipDoc: true,
document: `{a: {b: apple}}`, document: `{a: {b: apple}}`,
expression: `.a.b |= 5`, expression: `.a.b |= 5`,
expected: []string{ expected: []string{
@ -27,6 +38,7 @@ var assignOperatorScenarios = []expressionScenario{
}, },
}, },
{ {
skipDoc: true,
document: `{a: {b: apple}}`, document: `{a: {b: apple}}`,
expression: `.a.b |= 3.142`, expression: `.a.b |= 3.142`,
expected: []string{ expected: []string{
@ -34,41 +46,39 @@ var assignOperatorScenarios = []expressionScenario{
}, },
}, },
{ {
document: `{a: {b: {g: foof}}}`, description: "Update selected results",
expression: `.a |= .b`, document: `{a: {b: apple, c: cactus}}`,
expected: []string{ expression: `.a[] | select(. == "apple") |= "frog"`,
"D0, P[], (doc)::{a: {g: foof}}\n",
},
},
{
document: `{a: {b: apple, c: cactus}}`,
expression: `.a[] | select(. == "apple") |= "frog"`,
expected: []string{ expected: []string{
"D0, P[], (doc)::{a: {b: frog, c: cactus}}\n", "D0, P[], (doc)::{a: {b: frog, c: cactus}}\n",
}, },
}, },
{ {
document: `[candy, apple, sandy]`, description: "Update array values",
expression: `.[] | select(. == "*andy") |= "bogs"`, document: `[candy, apple, sandy]`,
expression: `.[] | select(. == "*andy") |= "bogs"`,
expected: []string{ expected: []string{
"D0, P[], (doc)::[bogs, apple, bogs]\n", "D0, P[], (doc)::[bogs, apple, bogs]\n",
}, },
}, },
{ {
document: `{}`, description: "Update empty object",
expression: `.a.b |= "bogs"`, document: `{}`,
expression: `.a.b |= "bogs"`,
expected: []string{ expected: []string{
"D0, P[], (doc)::{a: {b: bogs}}\n", "D0, P[], (doc)::{a: {b: bogs}}\n",
}, },
}, },
{ {
document: `{}`, description: "Update empty object and array",
expression: `.a.b[0] |= "bogs"`, document: `{}`,
expression: `.a.b[0] |= "bogs"`,
expected: []string{ expected: []string{
"D0, P[], (doc)::{a: {b: [bogs]}}\n", "D0, P[], (doc)::{a: {b: [bogs]}}\n",
}, },
}, },
{ {
skipDoc: true,
document: `{}`, document: `{}`,
expression: `.a.b[1].c |= "bogs"`, expression: `.a.b[1].c |= "bogs"`,
expected: []string{ expected: []string{
@ -81,4 +91,5 @@ func TestAssignOperatorScenarios(t *testing.T) {
for _, tt := range assignOperatorScenarios { for _, tt := range assignOperatorScenarios {
testScenario(t, &tt) testScenario(t, &tt)
} }
documentScenarios(t, "Update Assign Operator", assignOperatorScenarios)
} }

View File

@ -3,19 +3,15 @@ package yqlib
import ( import (
"container/list" "container/list"
"gopkg.in/yaml.v3" yaml "gopkg.in/yaml.v3"
) )
func DeleteChildOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) { func DeleteChildOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) {
lhs, err := d.GetMatchingNodes(matchingNodes, pathNode.Lhs)
if err != nil {
return nil, err
}
// for each lhs, splat the node, // for each lhs, splat the node,
// the intersect it against the rhs expression // the intersect it against the rhs expression
// recreate the contents using only the intersection result. // recreate the contents using only the intersection result.
for el := lhs.Front(); el != nil; el = el.Next() { for el := matchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode) candidate := el.Value.(*CandidateNode)
elMap := list.New() elMap := list.New()
elMap.PushBack(candidate) elMap.PushBack(candidate)
@ -25,20 +21,22 @@ func DeleteChildOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNod
return nil, err return nil, err
} }
if candidate.Node.Kind == yaml.SequenceNode { realNode := UnwrapDoc(candidate.Node)
if realNode.Kind == yaml.SequenceNode {
deleteFromArray(candidate, nodesToDelete) deleteFromArray(candidate, nodesToDelete)
} else if candidate.Node.Kind == yaml.MappingNode { } else if realNode.Kind == yaml.MappingNode {
deleteFromMap(candidate, nodesToDelete) deleteFromMap(candidate, nodesToDelete)
} else { } else {
log.Debug("Cannot delete from node that's not a map or array %v", NodeToString(candidate)) log.Debug("Cannot delete from node that's not a map or array %v", NodeToString(candidate))
} }
} }
return lhs, nil return matchingNodes, nil
} }
func deleteFromMap(candidate *CandidateNode, nodesToDelete *list.List) { func deleteFromMap(candidate *CandidateNode, nodesToDelete *list.List) {
log.Debug("deleteFromMap") log.Debug("deleteFromMap")
node := candidate.Node node := UnwrapDoc(candidate.Node)
contents := node.Content contents := node.Content
newContents := make([]*yaml.Node, 0) newContents := make([]*yaml.Node, 0)
@ -51,8 +49,13 @@ func deleteFromMap(candidate *CandidateNode, nodesToDelete *list.List) {
Document: candidate.Document, Document: candidate.Document,
Path: append(candidate.Path, key.Value), Path: append(candidate.Path, key.Value),
} }
// _, shouldDelete := nodesToDelete.Get(childCandidate.GetKey())
shouldDelete := true shouldDelete := false
for el := nodesToDelete.Front(); el != nil && shouldDelete == false; el = el.Next() {
if el.Value.(*CandidateNode).GetKey() == childCandidate.GetKey() {
shouldDelete = true
}
}
log.Debugf("shouldDelete %v ? %v", childCandidate.GetKey(), shouldDelete) log.Debugf("shouldDelete %v ? %v", childCandidate.GetKey(), shouldDelete)
@ -65,21 +68,26 @@ func deleteFromMap(candidate *CandidateNode, nodesToDelete *list.List) {
func deleteFromArray(candidate *CandidateNode, nodesToDelete *list.List) { func deleteFromArray(candidate *CandidateNode, nodesToDelete *list.List) {
log.Debug("deleteFromArray") log.Debug("deleteFromArray")
node := candidate.Node node := UnwrapDoc(candidate.Node)
contents := node.Content contents := node.Content
newContents := make([]*yaml.Node, 0) newContents := make([]*yaml.Node, 0)
for index := 0; index < len(contents); index = index + 1 { for index := 0; index < len(contents); index = index + 1 {
value := contents[index] value := contents[index]
// childCandidate := &CandidateNode{ childCandidate := &CandidateNode{
// Node: value, Node: value,
// Document: candidate.Document, Document: candidate.Document,
// Path: append(candidate.Path, index), Path: append(candidate.Path, index),
// } }
shouldDelete := false
for el := nodesToDelete.Front(); el != nil && shouldDelete == false; el = el.Next() {
if el.Value.(*CandidateNode).GetKey() == childCandidate.GetKey() {
shouldDelete = true
}
}
// _, shouldDelete := nodesToDelete.Get(childCandidate.GetKey())
shouldDelete := true
if !shouldDelete { if !shouldDelete {
newContents = append(newContents, value) newContents = append(newContents, value)
} }

View File

@ -0,0 +1,39 @@
package yqlib
import (
"testing"
)
var deleteOperatorScenarios = []expressionScenario{
{
description: "Delete entry in map",
document: `{a: cat, b: dog}`,
expression: `del(.b)`,
expected: []string{
"D0, P[], (doc)::{a: cat}\n",
},
},
{
description: "Delete entry in array",
document: `[1,2,3]`,
expression: `del(.[1])`,
expected: []string{
"D0, P[], (doc)::[1, 3]\n",
},
},
{
description: "Delete no matches",
document: `{a: cat, b: dog}`,
expression: `del(.c)`,
expected: []string{
"D0, P[], (doc)::{a: cat, b: dog}\n",
},
},
}
func TestDeleteOperatorScenarios(t *testing.T) {
for _, tt := range deleteOperatorScenarios {
testScenario(t, &tt)
}
documentScenarios(t, "Delete Operator", deleteOperatorScenarios)
}

View File

@ -6,27 +6,31 @@ import (
var explodeTest = []expressionScenario{ var explodeTest = []expressionScenario{
{ {
document: `{a: mike}`, description: "Explode alias and anchor",
expression: `explode(.a)`, document: `{f : {a: &a cat, b: *a}}`,
expected: []string{ expression: `explode(.f)`,
"D0, P[], (doc)::{a: mike}\n",
},
},
{
document: `{f : {a: &a cat, b: *a}}`,
expression: `explode(.f)`,
expected: []string{ expected: []string{
"D0, P[], (doc)::{f: {a: cat, b: cat}}\n", "D0, P[], (doc)::{f: {a: cat, b: cat}}\n",
}, },
}, },
{ {
document: `{f : {a: &a cat, *a: b}}`, description: "Explode with no aliases or anchors",
expression: `explode(.f)`, document: `{a: mike}`,
expression: `explode(.a)`,
expected: []string{
"D0, P[], (doc)::{a: mike}\n",
},
},
{
description: "Explode with alias keys",
document: `{f : {a: &a cat, *a: b}}`,
expression: `explode(.f)`,
expected: []string{ expected: []string{
"D0, P[], (doc)::{f: {a: cat, cat: b}}\n", "D0, P[], (doc)::{f: {a: cat, cat: b}}\n",
}, },
}, },
{ {
skipDoc: true,
document: mergeDocSample, document: mergeDocSample,
expression: `.foo* | explode(.) | (. style="flow")`, expression: `.foo* | explode(.) | (. style="flow")`,
expected: []string{ expected: []string{
@ -36,6 +40,7 @@ var explodeTest = []expressionScenario{
}, },
}, },
{ {
skipDoc: true,
document: mergeDocSample, document: mergeDocSample,
expression: `.foo* | explode(explode(.)) | (. style="flow")`, expression: `.foo* | explode(explode(.)) | (. style="flow")`,
expected: []string{ expected: []string{
@ -45,6 +50,7 @@ var explodeTest = []expressionScenario{
}, },
}, },
{ {
skipDoc: true,
document: `{f : {a: &a cat, b: &b {f: *a}, *a: *b}}`, document: `{f : {a: &a cat, b: &b {f: *a}, *a: *b}}`,
expression: `explode(.f)`, expression: `explode(.f)`,
expected: []string{ expected: []string{
@ -57,4 +63,5 @@ func TestExplodeOperatorScenarios(t *testing.T) {
for _, tt := range explodeTest { for _, tt := range explodeTest {
testScenario(t, &tt) testScenario(t, &tt)
} }
documentScenarios(t, "Explode Operator", explodeTest)
} }

View File

@ -209,7 +209,7 @@ func initLexer() (*lex.Lexer, error) {
lexer.Add([]byte(`\s*==\s*`), opToken(Equals)) lexer.Add([]byte(`\s*==\s*`), opToken(Equals))
lexer.Add([]byte(`\s*.-\s*`), opToken(DeleteChild)) lexer.Add([]byte(`del`), opToken(DeleteChild))
lexer.Add([]byte(`\s*\|=\s*`), opToken(Assign)) lexer.Add([]byte(`\s*\|=\s*`), opToken(Assign))