mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Fixed create yaml
This commit is contained in:
parent
c9229439f7
commit
08f579f4e3
@ -63,7 +63,6 @@ func evaluateSequence(cmd *cobra.Command, args []string) error {
|
|||||||
printer := yqlib.NewPrinter(out, outputToJSON, unwrapScalar, colorsEnabled, indent, !noDocSeparators)
|
printer := yqlib.NewPrinter(out, outputToJSON, unwrapScalar, colorsEnabled, indent, !noDocSeparators)
|
||||||
|
|
||||||
streamEvaluator := yqlib.NewStreamEvaluator()
|
streamEvaluator := yqlib.NewStreamEvaluator()
|
||||||
allAtOnceEvaluator := yqlib.NewAllAtOnceEvaluator()
|
|
||||||
|
|
||||||
switch len(args) {
|
switch len(args) {
|
||||||
case 0:
|
case 0:
|
||||||
@ -75,7 +74,7 @@ func evaluateSequence(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
case 1:
|
case 1:
|
||||||
if nullInput {
|
if nullInput {
|
||||||
err = allAtOnceEvaluator.EvaluateFiles(args[0], []string{}, printer)
|
err = streamEvaluator.EvaluateNew(args[0], printer)
|
||||||
} else {
|
} else {
|
||||||
err = streamEvaluator.EvaluateFiles("", []string{args[0]}, printer)
|
err = streamEvaluator.EvaluateFiles("", []string{args[0]}, printer)
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,18 @@ Which will assign the LHS node values to the RHS node values. The RHS expression
|
|||||||
|
|
||||||
### relative form: `|=`
|
### relative form: `|=`
|
||||||
This will do a similar thing to the plain form, however, the RHS expression is run against _the LHS nodes_. This is useful for updating values based on old values, e.g. increment.
|
This will do a similar thing to the plain form, however, the RHS expression is run against _the LHS nodes_. This is useful for updating values based on old values, e.g. increment.
|
||||||
|
## Create yaml file
|
||||||
|
Running
|
||||||
|
```bash
|
||||||
|
yq eval --null-input '(.a.b = "cat") | (.x = "frog")'
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
a:
|
||||||
|
b: cat
|
||||||
|
x: frog
|
||||||
|
```
|
||||||
|
|
||||||
## Update node to be the child value
|
## Update node to be the child value
|
||||||
Given a sample.yml file of:
|
Given a sample.yml file of:
|
||||||
```yaml
|
```yaml
|
||||||
|
@ -36,6 +36,11 @@ func AssignUpdateOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNo
|
|||||||
candidate.UpdateFrom(first.Value.(*CandidateNode))
|
candidate.UpdateFrom(first.Value.(*CandidateNode))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// // if there was nothing given, perhaps we are creating a new yaml doc
|
||||||
|
// if matchingNodes.Len() == 0 {
|
||||||
|
// log.Debug("started with nothing, returning LHS, %v", lhs.Len())
|
||||||
|
// return lhs, nil
|
||||||
|
// }
|
||||||
return matchingNodes, nil
|
return matchingNodes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var assignOperatorScenarios = []expressionScenario{
|
var assignOperatorScenarios = []expressionScenario{
|
||||||
|
{
|
||||||
|
description: "Create yaml file",
|
||||||
|
expression: `(.a.b = "cat") | (.x = "frog")`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], ()::a:\n b: cat\nx: frog\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: "Update node to be the child value",
|
description: "Update node to be the child value",
|
||||||
document: `{a: {b: {g: foof}}}`,
|
document: `{a: {b: {g: foof}}}`,
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/mikefarah/yq/v4/test"
|
"github.com/mikefarah/yq/v4/test"
|
||||||
|
yaml "gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type expressionScenario struct {
|
type expressionScenario struct {
|
||||||
@ -40,6 +41,15 @@ func testScenario(t *testing.T, s *expressionScenario) {
|
|||||||
t.Error(err, s.document)
|
t.Error(err, s.document)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
candidateNode := &CandidateNode{
|
||||||
|
Document: 0,
|
||||||
|
Filename: "",
|
||||||
|
Node: &yaml.Node{Tag: "!!null"},
|
||||||
|
FileIndex: 0,
|
||||||
|
}
|
||||||
|
inputs.PushBack(candidateNode)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
results, err = treeNavigator.GetMatchingNodes(inputs, node)
|
results, err = treeNavigator.GetMatchingNodes(inputs, node)
|
||||||
@ -152,20 +162,20 @@ func documentScenarios(t *testing.T, title string, scenarios []expressionScenari
|
|||||||
var output bytes.Buffer
|
var output bytes.Buffer
|
||||||
var err error
|
var err error
|
||||||
printer := NewPrinter(bufio.NewWriter(&output), false, true, false, 2, true)
|
printer := NewPrinter(bufio.NewWriter(&output), false, true, false, 2, true)
|
||||||
|
streamEvaluator := NewStreamEvaluator()
|
||||||
|
|
||||||
if s.document != "" {
|
if s.document != "" {
|
||||||
node, err := treeCreator.ParsePath(s.expression)
|
node, err := treeCreator.ParsePath(s.expression)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
streamEvaluator := NewStreamEvaluator()
|
|
||||||
err = streamEvaluator.Evaluate("sample.yaml", strings.NewReader(formattedDoc), node, printer)
|
err = streamEvaluator.Evaluate("sample.yaml", strings.NewReader(formattedDoc), node, printer)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
allAtOnceEvaluator := NewAllAtOnceEvaluator()
|
err = streamEvaluator.EvaluateNew(s.expression, printer)
|
||||||
err = allAtOnceEvaluator.EvaluateFiles(s.expression, []string{}, printer)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
type StreamEvaluator interface {
|
type StreamEvaluator interface {
|
||||||
Evaluate(filename string, reader io.Reader, node *PathTreeNode, printer Printer) error
|
Evaluate(filename string, reader io.Reader, node *PathTreeNode, printer Printer) error
|
||||||
EvaluateFiles(expression string, filenames []string, printer Printer) error
|
EvaluateFiles(expression string, filenames []string, printer Printer) error
|
||||||
|
EvaluateNew(expression string, printer Printer) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type streamEvaluator struct {
|
type streamEvaluator struct {
|
||||||
@ -23,6 +24,27 @@ func NewStreamEvaluator() StreamEvaluator {
|
|||||||
return &streamEvaluator{treeNavigator: NewDataTreeNavigator(), treeCreator: NewPathTreeCreator()}
|
return &streamEvaluator{treeNavigator: NewDataTreeNavigator(), treeCreator: NewPathTreeCreator()}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *streamEvaluator) EvaluateNew(expression string, printer Printer) error {
|
||||||
|
node, err := treeCreator.ParsePath(expression)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
candidateNode := &CandidateNode{
|
||||||
|
Document: 0,
|
||||||
|
Filename: "",
|
||||||
|
Node: &yaml.Node{Tag: "!!null"},
|
||||||
|
FileIndex: 0,
|
||||||
|
}
|
||||||
|
inputList := list.New()
|
||||||
|
inputList.PushBack(candidateNode)
|
||||||
|
|
||||||
|
matches, errorParsing := treeNavigator.GetMatchingNodes(inputList, node)
|
||||||
|
if errorParsing != nil {
|
||||||
|
return errorParsing
|
||||||
|
}
|
||||||
|
return printer.PrintResults(matches)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *streamEvaluator) EvaluateFiles(expression string, filenames []string, printer Printer) error {
|
func (s *streamEvaluator) EvaluateFiles(expression string, filenames []string, printer Printer) error {
|
||||||
|
|
||||||
node, err := treeCreator.ParsePath(expression)
|
node, err := treeCreator.ParsePath(expression)
|
||||||
|
Loading…
Reference in New Issue
Block a user