Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make extra_hosts values uniques #685

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions override/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func init() {
mergeSpecials["services.*.build"] = mergeBuild
mergeSpecials["services.*.build.args"] = mergeToSequence
mergeSpecials["services.*.build.additional_contexts"] = mergeToSequence
mergeSpecials["services.*.build.extra_hosts"] = mergeToSequence
mergeSpecials["services.*.build.extra_hosts"] = mergeExtraHosts
mergeSpecials["services.*.build.labels"] = mergeToSequence
mergeSpecials["services.*.command"] = override
mergeSpecials["services.*.depends_on"] = mergeDependsOn
Expand All @@ -58,7 +58,7 @@ func init() {
mergeSpecials["services.*.entrypoint"] = override
mergeSpecials["services.*.env_file"] = mergeToSequence
mergeSpecials["services.*.environment"] = mergeToSequence
mergeSpecials["services.*.extra_hosts"] = mergeToSequence
mergeSpecials["services.*.extra_hosts"] = mergeExtraHosts
mergeSpecials["services.*.healthcheck.test"] = override
mergeSpecials["services.*.labels"] = mergeToSequence
mergeSpecials["services.*.logging"] = mergeLogging
Expand Down Expand Up @@ -163,6 +163,22 @@ func mergeNetworks(c any, o any, path tree.Path) (any, error) {
return mergeMappings(right, left, path)
}

func mergeExtraHosts(c any, o any, _ tree.Path) (any, error) {
right := convertIntoSequence(c)
left := convertIntoSequence(o)
// Rewrite content of left slice to remove duplicate elements
i := 0
for _, v := range left {
if !slices.Contains(right, v) {
left[i] = v
i++
}
}
// keep only not duplicated elements from left slice
left = left[:i]
Comment on lines +169 to +178
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use https://pkg.go.dev/slices#Compact to remove duplicates from a slice

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope I tried it and it works only with consecutive values unfortunately

return append(right, left...), nil
}

func mergeToSequence(c any, o any, _ tree.Path) (any, error) {
right := convertIntoSequence(c)
left := convertIntoSequence(o)
Expand All @@ -172,15 +188,21 @@ func mergeToSequence(c any, o any, _ tree.Path) (any, error) {
func convertIntoSequence(value any) []any {
switch v := value.(type) {
case map[string]any:
seq := make([]any, len(v))
i := 0
for k, v := range v {
if v == nil {
seq[i] = k
var seq []any
for k, val := range v {
if val == nil {
seq = append(seq, k)
} else {
seq[i] = fmt.Sprintf("%s=%v", k, v)
switch vl := val.(type) {
// if val is an array we need to add the key with each value one by one
case []any:
for _, vlv := range vl {
seq = append(seq, fmt.Sprintf("%s=%v", k, vlv))
}
default:
seq = append(seq, fmt.Sprintf("%s=%v", k, val))
}
}
i++
}
slices.SortFunc(seq, func(a, b any) int {
return cmp.Compare(a.(string), b.(string))
Expand Down
8 changes: 8 additions & 0 deletions override/merge_extra_hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ services:
test:
extra_hosts:
- localhost=127.0.0.1
- example.com=1.2.3.4
- example.com=4.3.2.1
`, `
services:
test:
image: foo
extra_hosts:
- example.com=1.2.3.4
- localhost=127.0.0.1
- example.com=4.3.2.1
`)
}

Expand All @@ -54,12 +57,14 @@ services:
test:
extra_hosts:
"localhost": "127.0.0.1"
"example.com": ["1.2.3.4", "4.3.2.1"]
`, `
services:
test:
image: foo
extra_hosts:
- example.com=1.2.3.4
- example.com=4.3.2.1
- localhost=127.0.0.1
`)
}
Expand All @@ -76,12 +81,15 @@ services:
test:
extra_hosts:
- localhost=127.0.0.1
- example.com=1.2.3.4
- example.com=4.3.2.1
`, `
services:
test:
image: foo
extra_hosts:
- example.com=1.2.3.4
- localhost=127.0.0.1
- example.com=4.3.2.1
`)
}
3 changes: 2 additions & 1 deletion schema/compose-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,8 @@
"patternProperties": {
".+": {
"type": ["string", "array"]
}
},
"uniqueItems": false
},
"additionalProperties": false
},
Expand Down