Skip to content
Open
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
3 changes: 3 additions & 0 deletions loader/include.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ func importResources(source map[string]any, target map[string]any) error {
if err := importResource(source, target, "configs"); err != nil {
return err
}
if err := importResource(source, target, "models"); err != nil {
return err
}
return nil
}

Expand Down
43 changes: 43 additions & 0 deletions override/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type merger func(any, any, tree.Path) (any, error)
var mergeSpecials = map[tree.Path]merger{}

func init() {
mergeSpecials["models.*.runtime_flags"] = override
Copy link
Collaborator

Choose a reason for hiding this comment

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

This requires an update on the compose-spec documentation to make it clear runtime_flags won't be appended
see https://github.com/compose-spec/compose-spec/blob/main/13-merge.md#exceptions

mergeSpecials["networks.*.ipam.config"] = mergeIPAMConfig
mergeSpecials["networks.*.labels"] = mergeToSequence
mergeSpecials["volumes.*.labels"] = mergeToSequence
Expand Down Expand Up @@ -160,11 +161,53 @@ func mergeDependsOn(c any, o any, path tree.Path) (any, error) {
}

func mergeModels(c any, o any, path tree.Path) (any, error) {
// Check if both sides are string arrays for short syntax only
if rightArr, ok := c.([]any); ok {
if leftArr, ok := o.([]any); ok {
if isStringArray(rightArr) && isStringArray(leftArr) {
return mergeStringArrays(rightArr, leftArr), nil
}
}
}

// Otherwise, use map merge for long syntax or mixed syntax
Comment on lines +164 to +173
Copy link
Collaborator

Choose a reason for hiding this comment

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

this should not be necessary, as next lines merge equivalent mapping syntax

Copy link
Author

Choose a reason for hiding this comment

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

You mean to remove the comment only?

Copy link
Collaborator

Choose a reason for hiding this comment

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

no, I mean this all block. Mutating into mapping format then merge is the right way, and should not require any extra processing - we do the same for the many other comparable structs

right := convertIntoMapping(c, nil)
left := convertIntoMapping(o, nil)
return mergeMappings(right, left, path)
}

func isStringArray(arr []any) bool {
for _, item := range arr {
if _, ok := item.(string); !ok {
return false
}
}
return true
}

func mergeStringArrays(right, left []any) []any {
seen := make(map[string]bool)
var result []any

for _, item := range right {
str := item.(string)
if !seen[str] {
result = append(result, str)
seen[str] = true
}
}

for _, item := range left {
str := item.(string)
if !seen[str] {
result = append(result, str)
seen[str] = true
}
}

return result
}

func mergeNetworks(c any, o any, path tree.Path) (any, error) {
right := convertIntoMapping(c, nil)
left := convertIntoMapping(o, nil)
Expand Down
Loading