Repository navigation
How can I merge two yaml files using yq library ? #1966
Answered
by
mikefarah
GeorgeDuckman
asked this question in
Q&A
|
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: GoodbyeAnd I want the result to be : ---
a: GoodbyeIs 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 ! |
Answered by
mikefarah
Mar 24, 2024
Replies: 1 comment
|
I've added |
0 replies
Answer selected by
mikefarah
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've added
EvaluateAllto the string evaluator in v4.43.1 - give that a go instead :)