Skip to content

Commit

Permalink
corner case merging override
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Jan 17, 2024
1 parent 3b9fc71 commit 8263290
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 5 deletions.
69 changes: 68 additions & 1 deletion loader/override_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ services:
- alias1
- alias2
`
_, err := LoadWithContext(context.Background(), types.ConfigDetails{
p, err := LoadWithContext(context.Background(), types.ConfigDetails{
ConfigFiles: []types.ConfigFile{
{
Filename: "base",
Expand All @@ -60,4 +60,71 @@ services:
},
})
assert.NilError(t, err)
assert.DeepEqual(t, p.Services["test"].Networks["test_network"].Aliases, []string{"alias1", "alias2"})
}

func TestOverrideBuildContext(t *testing.T) {
yaml := `
name: test-override-networks
services:
test:
build: .
`

override := `
services:
test:
build:
context: src
`
p, err := LoadWithContext(context.Background(), types.ConfigDetails{
ConfigFiles: []types.ConfigFile{
{
Filename: "base",
Content: []byte(yaml),
},
{
Filename: "override",
Content: []byte(override),
},
},
})
assert.NilError(t, err)
assert.Equal(t, p.Services["test"].Build.Context, "src")
}

func TestOverrideDepends_on(t *testing.T) {
yaml := `
name: test-override-networks
services:
test:
image: test
depends_on:
- foo
foo:
image: foo
`

override := `
services:
test:
depends_on:
foo:
condition: service_healthy
required: false
`
p, err := LoadWithContext(context.Background(), types.ConfigDetails{
ConfigFiles: []types.ConfigFile{
{
Filename: "base",
Content: []byte(yaml),
},
{
Filename: "override",
Content: []byte(override),
},
},
})
assert.NilError(t, err)
assert.Check(t, p.Services["test"].DependsOn["foo"].Required == false)
}
37 changes: 33 additions & 4 deletions override/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type merger func(any, any, tree.Path) (any, error)
var mergeSpecials = map[tree.Path]merger{}

func init() {
mergeSpecials["services.*.build"] = mergeBuild
mergeSpecials["services.*.depends_on"] = mergeDependsOn
mergeSpecials["services.*.logging"] = mergeLogging
mergeSpecials["services.*.networks"] = mergeNetworks
mergeSpecials["services.*.command"] = override
Expand Down Expand Up @@ -107,9 +109,36 @@ func mergeLogging(c any, o any, p tree.Path) (any, error) {
return other, nil
}

func mergeBuild(c any, o any, path tree.Path) (any, error) {
toBuild := func(c any) map[string]any {
switch v := c.(type) {
case string:
return map[string]any{
"context": v,
}
case map[string]any:
return v
}
return nil
}
return mergeMappings(toBuild(c), toBuild(o), path)
}

func mergeDependsOn(c any, o any, path tree.Path) (any, error) {
right := convertIntoMapping(c, map[string]any{
"condition": "service_started",
"required": true,
})
left := convertIntoMapping(o, map[string]any{
"condition": "service_started",
"required": true,
})
return mergeMappings(right, left, path)
}

func mergeNetworks(c any, o any, path tree.Path) (any, error) {
right := convertIntoMapping(c)
left := convertIntoMapping(o)
right := convertIntoMapping(c, nil)
left := convertIntoMapping(o, nil)
return mergeMappings(right, left, path)
}

Expand Down Expand Up @@ -151,14 +180,14 @@ func mergeUlimit(_ any, o any, p tree.Path) (any, error) {
return o, nil
}

func convertIntoMapping(a any) map[string]any {
func convertIntoMapping(a any, defaultValue any) map[string]any {
switch v := a.(type) {
case map[string]any:
return v
case []any:
converted := map[string]any{}
for _, s := range v {
converted[s.(string)] = nil
converted[s.(string)] = defaultValue
}
return converted
}
Expand Down

0 comments on commit 8263290

Please sign in to comment.