Skip to content

Commit

Permalink
Merge pull request #3 from RaftechNL:bidirectional-yaml-merge
Browse files Browse the repository at this point in the history
refactor(yaml): Bidirectional-yaml-merge
  • Loading branch information
RafPe authored Feb 16, 2023
2 parents b61c39f + 3228acc commit c116744
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 27 deletions.
54 changes: 27 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/labstack/gommon/log"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -70,22 +69,40 @@ func main() {
}
}

func removeLastLineIfEmpty(s string) string {
lines := strings.Split(s, "\n")
lastLine := strings.TrimSpace(lines[len(lines)-1])
if lastLine == "" {
lines = lines[:len(lines)-1]
func mergeMaps(dst, src map[string]interface{}) {
for k, v := range src {
if _, ok := dst[k]; !ok {
// key does not exist in dst, just copy from src
dst[k] = v
continue
}

// key exists in both dst and src, need to merge recursively
dstValue := dst[k]
switch dstValue := dstValue.(type) {
case map[string]interface{}:
srcValue, ok := v.(map[string]interface{})
if !ok {
// type mismatch, just copy from src
dst[k] = v
continue
}
mergeMaps(dstValue, srcValue)
default:
// type mismatch or dst has scalar value, just copy from src
dst[k] = v
continue
}
}
return strings.Join(lines, "\n")
}

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

resultValues := make(map[string]interface{})
for _, filename := range filenames {
var override map[string]interface{}
bs, err := ioutil.ReadFile(filename)
if err != nil {
Expand All @@ -96,19 +113,7 @@ func MergeYAML(filenames ...string) ([]byte, error) {
log.Info(err)
return nil, fmt.Errorf("failed to unmarshal data from file %q: %w", filename, err)
}

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

mergeMaps(resultValues, override)
}

bs, err := yaml.Marshal(resultValues)
Expand All @@ -117,10 +122,5 @@ func MergeYAML(filenames ...string) ([]byte, error) {
return nil, err
}

if err != nil {
log.Info(err)
return nil, err
}

return bs, nil
}
25 changes: 25 additions & 0 deletions merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@ import (
"github.com/stretchr/testify/assert"
)

func TestMergeYAML_InvertedMergeOrder(t *testing.T) {
result, err := MergeYAML("tests/test2.yaml", "tests/test1.yaml")
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(`
modules:
my_module:
providerAliasRef: my_provider2
source: github.com/example/module
version: v1.0.0
my_module2:
providerAliasRef: my_provider2
source: github.com/example/module
version: v1.6.0
my_module3:
providerAliasRef: my_provider2
source: github.com/example/module
version: v1.6.0
providers:
my_provider:
auth:
ssh_key: ssh:key:2312312
providerType: github
`), strings.TrimSpace(string(result)))
}

func TestMergeYAML_TwoFiles(t *testing.T) {
result, err := MergeYAML("tests/test1.yaml", "tests/test2.yaml")
assert.NoError(t, err)
Expand Down

0 comments on commit c116744

Please sign in to comment.