Skip to content

Commit d431159

Browse files
committed
Use yq string evaluator instead of stream evaluator
This allows us to pass the YAML content in memory instead of having to write it to a file. This commit also switches to "eval-all" mode, so multiple documents are all loaded together, allowing merge operations between them. This is in preparation of multi-file template assembly. Signed-off-by: Jan Dubois <[email protected]>
1 parent 4a40ad0 commit d431159

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

pkg/yqutil/yqutil.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"fmt"
88
"io"
9-
"os"
109
"strings"
1110

1211
"github.com/google/yamlfmt"
@@ -37,7 +36,7 @@ func ValidateContent(content []byte) error {
3736
return err
3837
}
3938

40-
// EvaluateExpression evaluates the yq expression, and returns the modified yaml.
39+
// EvaluateExpression evaluates the yq expression and returns the modified yaml.
4140
func EvaluateExpression(expression string, content []byte) ([]byte, error) {
4241
if expression == "" {
4342
return content, nil
@@ -56,21 +55,6 @@ func EvaluateExpression(expression string, content []byte) ([]byte, error) {
5655
if err != nil {
5756
return nil, err
5857
}
59-
tmpYAMLFile, err := os.CreateTemp("", "lima-yq-*.yaml")
60-
if err != nil {
61-
return nil, err
62-
}
63-
tmpYAMLPath := tmpYAMLFile.Name()
64-
defer os.RemoveAll(tmpYAMLPath)
65-
_, err = tmpYAMLFile.Write(contentModified)
66-
if err != nil {
67-
tmpYAMLFile.Close()
68-
return nil, err
69-
}
70-
if err = tmpYAMLFile.Close(); err != nil {
71-
return nil, err
72-
}
73-
7458
memory := logging.NewMemoryBackend(0)
7559
backend := logging.AddModuleLevel(memory)
7660
logging.SetBackend(backend)
@@ -80,13 +64,8 @@ func EvaluateExpression(expression string, content []byte) ([]byte, error) {
8064
encoderPrefs.Indent = 2
8165
encoderPrefs.ColorsEnabled = false
8266
encoder := yqlib.NewYamlEncoder(encoderPrefs)
83-
out := new(bytes.Buffer)
84-
printer := yqlib.NewPrinter(encoder, yqlib.NewSinglePrinterWriter(out))
8567
decoder := yqlib.NewYamlDecoder(yqlib.ConfiguredYamlPreferences)
86-
87-
streamEvaluator := yqlib.NewStreamEvaluator()
88-
files := []string{tmpYAMLPath}
89-
err = streamEvaluator.EvaluateFiles(expression, files, printer, decoder)
68+
out, err := yqlib.NewStringEvaluator().EvaluateAll(expression, string(contentModified), encoder, decoder)
9069
if err != nil {
9170
logger := logrus.StandardLogger()
9271
for node := memory.Head(); node != nil; node = node.Next() {
@@ -110,8 +89,7 @@ func EvaluateExpression(expression string, content []byte) ([]byte, error) {
11089
}
11190
return nil, err
11291
}
113-
114-
return formatter.Format(out.Bytes())
92+
return formatter.Format([]byte(out))
11593
}
11694

11795
func Join(yqExprs []string) string {

pkg/yqutil/yqutil_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package yqutil
22

33
import (
4+
"strings"
45
"testing"
56

67
"gotest.tools/v3/assert"
@@ -95,3 +96,27 @@ func TestEvaluateExpressionError(t *testing.T) {
9596
_, err := EvaluateExpression(expression, []byte(""))
9697
assert.ErrorContains(t, err, "invalid input text")
9798
}
99+
100+
func TestEvaluateMergeExpression(t *testing.T) {
101+
expression := `select(di == 0) * select(di == 1)`
102+
content := `
103+
yolo: true
104+
foo:
105+
bar: 1
106+
baz: 2
107+
---
108+
foo:
109+
bar: 3
110+
fomo: false
111+
`
112+
expected := `
113+
yolo: true
114+
foo:
115+
bar: 3
116+
baz: 2
117+
fomo: false
118+
`
119+
out, err := EvaluateExpression(expression, []byte(strings.TrimSpace(content)))
120+
assert.NilError(t, err)
121+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(out)))
122+
}

0 commit comments

Comments
 (0)