Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read and merge two Yaml files in go language

Assuming we have two yaml files

master.yaml

someProperty: "someVaue"
anotherProperty: "anotherValue"

override.yaml

someProperty: "overriddenVaue"

Is it possible to unmarshall, merge, and then write those changes to a file without having to define a struct for every property in the yaml file?

The master file has over 500 properties in it that are not at all important to the service at this point of execution, so ideally I'd be able to just unmarshal into a map, do a merge and write out in yaml again but I'm relatively new to go so wanted some opinions.

I've got some code to read the yaml into an interface but i'm unsure on the best approach to then merge the two.

var masterYaml interface{}
yamlBytes, _ := ioutil.ReadFile("master.yaml")
yaml.Unmarshal(yamlBytes, &masterYaml)

var overrideYaml interface{}
yamlBytes, _ = ioutil.ReadFile("override.yaml")
yaml.Unmarshal(yamlBytes, &overrideYaml)

I've looked into libraries like mergo but i'm not sure if that's the right approach.

I'm hoping that after the master I would be able to write out to file with properties

someProperty: "overriddenVaue"
anotherProperty: "anotherValue"
like image 227
DominicEU Avatar asked Aug 21 '18 11:08

DominicEU


2 Answers

Assuming that you just want to merge at the top level, you can unmarshal into maps of type map[string]interface{}, as follows:

package main

import (
    "io/ioutil"

    "gopkg.in/yaml.v2"
)

func main() {
    var master map[string]interface{}
    bs, err := ioutil.ReadFile("master.yaml")
    if err != nil {
        panic(err)
    }
    if err := yaml.Unmarshal(bs, &master); err != nil {
        panic(err)
    }

    var override map[string]interface{}
    bs, err = ioutil.ReadFile("override.yaml")
    if err != nil {
        panic(err)
    }
    if err := yaml.Unmarshal(bs, &override); err != nil {
        panic(err)
    }

    for k, v := range override {
        master[k] = v
    }

    bs, err = yaml.Marshal(master)
    if err != nil {
        panic(err)
    }
    if err := ioutil.WriteFile("merged.yaml", bs, 0644); err != nil {
        panic(err)
    }
}
like image 52
robx Avatar answered Nov 02 '22 20:11

robx


For a broader solution (with n input files), you can use this function. I have used @robox answer to do my solution:

func ReadValues(filenames ...string) (string, error) {
    if len(filenames) <= 0 {
        return "", errors.New("You must provide at least one filename for reading Values")
    }
    var resultValues map[string]interface{}
    for _, filename := range filenames {

        var override map[string]interface{}
        bs, err := ioutil.ReadFile(filename)
        if err != nil {
            log.Info(err)
            continue
        }
        if err := yaml.Unmarshal(bs, &override); err != nil {
            log.Info(err)
            continue
        }

        //check if is nil. This will only happen for the first filename
        if resultValues == nil {
            resultValues = override
        } else {
            for k, v := range override {
                resultValues[k] = v
            }
        }

    }
    bs, err := yaml.Marshal(resultValues)
    if err != nil {
        log.Info(err)
        return "", err
    }

    return string(bs), nil
}

So for this example you should call it with this order:

result, _ := ReadValues("master.yaml", "overwrite.yaml")

In the case you have an extra file newFile.yaml, you could also use this function:

result, _ := ReadValues("master.yaml", "overwrite.yaml", "newFile.yaml")
like image 2
pcampana Avatar answered Nov 02 '22 19:11

pcampana