Skip to content

Commit

Permalink
[wip] merge sequences and values
Browse files Browse the repository at this point in the history
Signed-off-by: Joana Hrotko <joana.hrotko@docker.com>
  • Loading branch information
jhrotko committed Jul 18, 2024
1 parent 769147e commit 28292f8
Show file tree
Hide file tree
Showing 4 changed files with 334 additions and 3 deletions.
16 changes: 13 additions & 3 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ func parseYAML(decoder *yaml.Decoder) (map[string]interface{}, PostProcessor, er
return nil, nil, err
}
stringMap, ok := cfg.(map[string]interface{})
fmt.Printf("[parseYAML] stringMap %v\n\n", stringMap)

if ok {
converted, err := convertToStringKeysRecursive(stringMap, "")
if err != nil {
Expand Down Expand Up @@ -424,6 +426,7 @@ func loadYamlFile(ctx context.Context, file types.ConfigFile, opts *Options, wor

processRawYaml := func(raw interface{}, processors ...PostProcessor) error {
converted, err := convertToStringKeysRecursive(raw, "")
fmt.Printf("CONVERTED %v\n", converted)
if err != nil {
return err
}
Expand All @@ -442,6 +445,8 @@ func loadYamlFile(ctx context.Context, file types.ConfigFile, opts *Options, wor
}

for _, processor := range processors {
fmt.Printf("[Apply] processor:%v\n\n", processor)

if err := processor.Apply(dict); err != nil {
return err
}
Expand All @@ -454,14 +459,15 @@ func loadYamlFile(ctx context.Context, file types.ConfigFile, opts *Options, wor
return err
}
}

fmt.Printf("[loadYaml] merge\ndict:%v\ncfg:%v\n", dict, cfg)
dict, err = override.Merge(dict, cfg)
if err != nil {
return err
}

if !opts.SkipValidation {
if err := schema.Validate(dict); err != nil {
fmt.Printf("\nVALIDATION DICT: %v\n\n", dict)
return fmt.Errorf("validating %s: %w", file.Filename, err)
}
if _, ok := dict["version"]; ok {
Expand All @@ -479,15 +485,18 @@ func loadYamlFile(ctx context.Context, file types.ConfigFile, opts *Options, wor
for {
var raw interface{}
reset := &ResetProcessor{target: &raw}
err := decoder.Decode(reset)
sequence := &SequenceProcessor{target: &raw}
err := decoder.Decode(sequence)
fmt.Printf("\nRESET %v\n", raw)
if err != nil && errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil, nil, err
}
processor = reset
if err := processRawYaml(raw, processor); err != nil {

if err := processRawYaml(raw, processor, sequence); err != nil {
return nil, nil, err
}
}
Expand Down Expand Up @@ -763,6 +772,7 @@ func nameServices(from reflect.Value, to reflect.Value) (interface{}, error) {

// keys need to be converted to strings for jsonschema
func convertToStringKeysRecursive(value interface{}, keyPrefix string) (interface{}, error) {
// fmt.Printf("[convertToStringKeysRecursive] value %v\n\n", value)

Check failure on line 775 in loader/loader.go

View workflow job for this annotation

GitHub Actions / test (1.22, ubuntu-latest)

commentedOutCode: may want to remove commented-out code (gocritic)

Check failure on line 775 in loader/loader.go

View workflow job for this annotation

GitHub Actions / test (1.22, macos-latest)

commentedOutCode: may want to remove commented-out code (gocritic)

Check failure on line 775 in loader/loader.go

View workflow job for this annotation

GitHub Actions / test (1.21, ubuntu-latest)

commentedOutCode: may want to remove commented-out code (gocritic)

Check failure on line 775 in loader/loader.go

View workflow job for this annotation

GitHub Actions / test (1.21, macos-latest)

commentedOutCode: may want to remove commented-out code (gocritic)
if mapping, ok := value.(map[string]interface{}); ok {
for key, entry := range mapping {
var newKeyPrefix string
Expand Down
100 changes: 100 additions & 0 deletions loader/loader_yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,103 @@ configs:
},
})
}

func TestParseYAMLFilesMergeOverrideArray(t *testing.T) {
model, err := loadYamlModel(context.TODO(), types.ConfigDetails{
ConfigFiles: []types.ConfigFile{
{Filename: "override.yaml",
Content: []byte(`
x-app:
volumes: &app-volumes
- /data/app:/app/data
services:
app:
image: myapp:latest
volumes:
- *app-volumes
- /logs/app:/app/logs
`)},
}}, &Options{}, &cycleTracker{}, nil)
assert.NilError(t, err)
assert.DeepEqual(t, model, map[string]interface{}{
"services": map[string]interface{}{
"app": map[string]interface{}{
"image": "myapp:latest",
"volumes": []interface{}{
map[string]interface{}{
"bind": map[string]interface{}{
"create_host_path": bool(true),
},
"source": string("/data/app"),
"target": string("/app/data"), // should merge /data/app:/app/data
"type": string("bind"),
},
map[string]interface{}{
"bind": map[string]interface{}{
"create_host_path": bool(true),
},
"source": string("/logs/app"),
"target": string("/app/logs"),
"type": string("bind"),
},
},
},
},
"x-app": map[string]interface{}{
"volumes": []interface{}{
string("/data/app:/app/data"),
},
},
})
}

func TestParseYAMLFilesMergeMapArray(t *testing.T) {
model, err := loadYamlModel(context.TODO(), types.ConfigDetails{
ConfigFiles: []types.ConfigFile{
{Filename: "override.yaml",
Content: []byte(`
x-app: &app-volumes
image: alpine
volumes:
- /data/app:/app/data
services:
app:
image: python
<<: *app-volumes
volumes:
- /logs/app:/app/logs
`)},
}}, &Options{}, &cycleTracker{}, nil)
assert.NilError(t, err)
assert.DeepEqual(t, model, map[string]interface{}{
"services": map[string]interface{}{
"app": map[string]interface{}{
"image": "alpine", // should override
"volumes": []interface{}{
map[string]interface{}{
"bind": map[string]interface{}{
"create_host_path": bool(true),
},
"source": string("/logs/app"),
"target": string("/app/logs"),
"type": string("bind"),
},
map[string]interface{}{
"bind": map[string]interface{}{
"create_host_path": bool(true),
},
"source": string("/data/app"),
"target": string("/app/data"), // should merge /data/app:/app/data
"type": string("bind"),
},
},
},
},
"x-app": map[string]interface{}{
"image": "alpine",
"volumes": []interface{}{
string("/data/app:/app/data"),
},
},
})
}
1 change: 1 addition & 0 deletions loader/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (p *ResetProcessor) resolveReset(node *yaml.Node, path tree.Path) (*yaml.No
}
// 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 {
fmt.Printf("\nnode: %v\n", node.Value)
return p.resolveReset(node.Alias, path)
}

Expand Down
Loading

0 comments on commit 28292f8

Please sign in to comment.