Skip to content

Commit

Permalink
Merge pull request #3008 from jandubois/yqlib
Browse files Browse the repository at this point in the history
Use yq string evaluator instead of stream evaluator
  • Loading branch information
AkihiroSuda authored Dec 11, 2024
2 parents af60ff7 + d431159 commit d365855
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
28 changes: 3 additions & 25 deletions pkg/yqutil/yqutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"os"
"strings"

"github.com/google/yamlfmt"
Expand Down Expand Up @@ -37,7 +36,7 @@ func ValidateContent(content []byte) error {
return err
}

// EvaluateExpression evaluates the yq expression, and returns the modified yaml.
// EvaluateExpression evaluates the yq expression and returns the modified yaml.
func EvaluateExpression(expression string, content []byte) ([]byte, error) {
if expression == "" {
return content, nil
Expand All @@ -56,21 +55,6 @@ func EvaluateExpression(expression string, content []byte) ([]byte, error) {
if err != nil {
return nil, err
}
tmpYAMLFile, err := os.CreateTemp("", "lima-yq-*.yaml")
if err != nil {
return nil, err
}
tmpYAMLPath := tmpYAMLFile.Name()
defer os.RemoveAll(tmpYAMLPath)
_, err = tmpYAMLFile.Write(contentModified)
if err != nil {
tmpYAMLFile.Close()
return nil, err
}
if err = tmpYAMLFile.Close(); err != nil {
return nil, err
}

memory := logging.NewMemoryBackend(0)
backend := logging.AddModuleLevel(memory)
logging.SetBackend(backend)
Expand All @@ -80,13 +64,8 @@ func EvaluateExpression(expression string, content []byte) ([]byte, error) {
encoderPrefs.Indent = 2
encoderPrefs.ColorsEnabled = false
encoder := yqlib.NewYamlEncoder(encoderPrefs)
out := new(bytes.Buffer)
printer := yqlib.NewPrinter(encoder, yqlib.NewSinglePrinterWriter(out))
decoder := yqlib.NewYamlDecoder(yqlib.ConfiguredYamlPreferences)

streamEvaluator := yqlib.NewStreamEvaluator()
files := []string{tmpYAMLPath}
err = streamEvaluator.EvaluateFiles(expression, files, printer, decoder)
out, err := yqlib.NewStringEvaluator().EvaluateAll(expression, string(contentModified), encoder, decoder)
if err != nil {
logger := logrus.StandardLogger()
for node := memory.Head(); node != nil; node = node.Next() {
Expand All @@ -110,8 +89,7 @@ func EvaluateExpression(expression string, content []byte) ([]byte, error) {
}
return nil, err
}

return formatter.Format(out.Bytes())
return formatter.Format([]byte(out))
}

func Join(yqExprs []string) string {
Expand Down
25 changes: 25 additions & 0 deletions pkg/yqutil/yqutil_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package yqutil

import (
"strings"
"testing"

"gotest.tools/v3/assert"
Expand Down Expand Up @@ -95,3 +96,27 @@ func TestEvaluateExpressionError(t *testing.T) {
_, err := EvaluateExpression(expression, []byte(""))
assert.ErrorContains(t, err, "invalid input text")
}

func TestEvaluateMergeExpression(t *testing.T) {
expression := `select(di == 0) * select(di == 1)`
content := `
yolo: true
foo:
bar: 1
baz: 2
---
foo:
bar: 3
fomo: false
`
expected := `
yolo: true
foo:
bar: 3
baz: 2
fomo: false
`
out, err := EvaluateExpression(expression, []byte(strings.TrimSpace(content)))
assert.NilError(t, err)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(out)))
}

0 comments on commit d365855

Please sign in to comment.