Skip to content

Commit 77c6c15

Browse files
chore(import): support Deep Merge Strategy
Signed-off-by: JordanGoasdoue <jordan.goasdoue@dailymotion.com>
1 parent 8cba970 commit 77c6c15

5 files changed

Lines changed: 311 additions & 8 deletions

File tree

devspace-schema.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,6 +2021,14 @@
20212021
],
20222022
"description": "Enabled specifies if the given import should be enabled"
20232023
},
2024+
"mergeStrategy": {
2025+
"type": "string",
2026+
"enum": [
2027+
"shallowMerge",
2028+
"deepMerge"
2029+
],
2030+
"description": "MergeStrategy specifies how the imported config should be merged (e.g. shallowMerge or deepMerge)"
2031+
},
20242032
"path": {
20252033
"type": "string",
20262034
"description": "Path is the local path where DevSpace can find the artifact.\nThis option is mutually exclusive with the git option.",

docs/schemas/config-openapi.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,14 @@
10451045
"type": "boolean",
10461046
"description": "Enabled specifies if the given import should be enabled"
10471047
},
1048+
"mergeStrategy": {
1049+
"type": "string",
1050+
"enum": [
1051+
"shallowMerge",
1052+
"deepMerge"
1053+
],
1054+
"description": "MergeStrategy specifies how the imported config should be merged (e.g. shallowMerge or deepMerge)"
1055+
},
10481056
"path": {
10491057
"type": "string",
10501058
"description": "Path is the local path where DevSpace can find the artifact.\nThis option is mutually exclusive with the git option.",

pkg/devspace/config/loader/imports.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/loft-sh/devspace/pkg/devspace/config/loader/variable"
1010
"github.com/loft-sh/devspace/pkg/devspace/config/versions"
11+
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
1112
"github.com/loft-sh/devspace/pkg/devspace/config/versions/util"
1213
dependencyutil "github.com/loft-sh/devspace/pkg/devspace/dependency/util"
1314
"github.com/loft-sh/devspace/pkg/util/log"
@@ -118,7 +119,15 @@ func ResolveImports(ctx context.Context, resolver variable.Resolver, basePath st
118119
if mergedMap[section] == nil {
119120
mergedMap[section] = map[string]interface{}{}
120121
}
121-
deepMerge(mergedMap[section].(map[string]interface{}), sectionMap)
122+
123+
switch i.MergeStrategy {
124+
case latest.MergeStrategyDeep:
125+
deepMerge(mergedMap[section].(map[string]interface{}), sectionMap)
126+
case "", latest.MergeStrategyShallow:
127+
shallowMerge(mergedMap[section].(map[string]interface{}), sectionMap)
128+
default:
129+
return nil, fmt.Errorf("invalid mergeStrategy %q in import %s", i.MergeStrategy, configPath)
130+
}
122131
}
123132

124133
// resolve the import imports
@@ -173,3 +182,13 @@ func deepMerge(dst, src map[string]interface{}) {
173182
// For other types (arrays, scalars), dst takes precedence (no action needed)
174183
}
175184
}
185+
186+
// shallowMerge merges src into dst only at the top level.
187+
// Existing keys in dst take precedence.
188+
func shallowMerge(dst, src map[string]interface{}) {
189+
for key, value := range src {
190+
if _, ok := dst[key]; !ok {
191+
dst[key] = value
192+
}
193+
}
194+
}

0 commit comments

Comments
 (0)