From 5422e49a9e35963312a9da88fa5e2470dc1c8cad Mon Sep 17 00:00:00 2001 From: parth aranke Date: Thu, 11 Apr 2024 16:38:51 +0530 Subject: [PATCH] incorporating review comments Signed-off-by: parth aranke --- loader/loader_yaml_test.go | 49 ++++++++++++++++++++++++++++++++++++++ loader/reset.go | 10 ++------ 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/loader/loader_yaml_test.go b/loader/loader_yaml_test.go index 75ae2e87..0f084f3b 100644 --- a/loader/loader_yaml_test.go +++ b/loader/loader_yaml_test.go @@ -55,3 +55,52 @@ services: }, }) } + +func TestParseYAMLFilesMergeOverride(t *testing.T) { + model, err := loadYamlModel(context.TODO(), types.ConfigDetails{ + ConfigFiles: []types.ConfigFile{ + {Filename: "override.yaml", + Content: []byte(` +services: + base: + configs: + - source: credentials + target: /credentials/file1 + x: &x + extends: + base + configs: !override + - source: credentials + target: /literally-anywhere-else + + y: + <<: *x + +configs: + credentials: + content: | + dummy value +`)}, + }}, &Options{}, &cycleTracker{}, nil) + assert.NilError(t, err) + assert.DeepEqual(t, model, map[string]interface{}{ + "configs": map[string]interface{}{"credentials": map[string]interface{}{"content": string("dummy value\n")}}, + "services": map[string]interface{}{ + "base": map[string]interface{}{ + "configs": []interface{}{ + map[string]interface{}{"source": string("credentials"), "target": string("/credentials/file1")}, + }, + }, + "x": map[string]interface{}{ + "configs": []interface{}{ + map[string]interface{}{"source": string("credentials"), "target": string("/literally-anywhere-else")}, + }, + }, + "y": map[string]interface{}{ + "configs": []interface{}{ + map[string]interface{}{"source": string("credentials"), "target": string("/literally-anywhere-else")}, + }, + }, + }, + }) +} diff --git a/loader/reset.go b/loader/reset.go index 42335ba7..2b7f04c3 100644 --- a/loader/reset.go +++ b/loader/reset.go @@ -42,14 +42,8 @@ func (p *ResetProcessor) UnmarshalYAML(value *yaml.Node) error { // resolveReset detects `!reset` tag being set on yaml nodes and record position in the yaml tree func (p *ResetProcessor) resolveReset(node *yaml.Node, path tree.Path) (*yaml.Node, error) { // If the path contains "<<", removing the "<<" element and merging the path - if strings.Contains(path.String(), "<<") { - pathArr := strings.Split(path.String(), ".") - path = tree.NewPath(pathArr[0]) - for _, el := range pathArr[1:] { - if el != "<<" { - path = tree.Path(strings.Join([]string{path.String(), el}, ".")) - } - } + if strings.Contains(path.String(), ".<<") { + path = tree.NewPath(strings.Replace(path.String(), ".<<", "", 1)) } // If the node is an alias, We need to process the alias field in order to consider the !override and !reset tags if node.Kind == yaml.AliasNode {