How can I merge two yaml files using yq library ? #1966
-
Hello, I am attempting to use yq as a library in my Golang application. I want to merge two yaml contents that are contained in two different string variables. I am aware that the yq command can be used to merge two YAML files : yq -n 'load("file1.yaml") * load("file2.yaml")' To use yqlib, I am using the StringEvaluator which only takes one input. I am concatenating my two variables into one as follows: ---
a: Hello
---
a: Goodbye And I want the result to be : ---
a: Goodbye Is there a way to do this ? I would prefer not to create temporary files if possible :) Here is my minimal code sample if you want to try yourself : package main
import (
"fmt"
"log"
"github.com/mikefarah/yq/v4/pkg/yqlib"
)
func main() {
yamlA := "---\n" +
"a: Hello"
yamlB := "---\n" +
"a: Goodbye"
yaml := yamlA + "\n" + yamlB
se := yqlib.NewStringEvaluator()
expression := "select(document_index == 0) * select(document_index == 1)"
strung, err := se.Evaluate(expression, yaml, yqlib.NewYamlEncoder(2, true, yqlib.YamlPreferences{}), yqlib.NewGoccyYAMLDecoder())
if err != nil {
log.Fatal(err)
}
fmt.Println(strung)
} Thank you in advance ! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I've added |
Beta Was this translation helpful? Give feedback.
I've added
EvaluateAll
to the string evaluator in v4.43.1 - give that a go instead :)