Skip to content

Commit e2df0ed

Browse files
authored
Merge pull request #255 from ndeloof/extension_conflict
2 parents 684383f + 7105042 commit e2df0ed

File tree

4 files changed

+84
-67
lines changed

4 files changed

+84
-67
lines changed

loader/loader.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ func parseConfig(b []byte, opts *Options) (map[string]interface{}, error) {
334334
return yml, err
335335
}
336336

337+
const extensions = "#extensions" // Using # prefix, we prevent risk to conflict with an actual yaml key
338+
337339
func groupXFieldsIntoExtensions(dict map[string]interface{}) map[string]interface{} {
338340
extras := map[string]interface{}{}
339341
for key, value := range dict {
@@ -346,7 +348,7 @@ func groupXFieldsIntoExtensions(dict map[string]interface{}) map[string]interfac
346348
}
347349
}
348350
if len(extras) > 0 {
349-
dict["extensions"] = extras
351+
dict[extensions] = extras
350352
}
351353
return dict
352354
}
@@ -385,7 +387,7 @@ func loadSections(filename string, config map[string]interface{}, configDetails
385387
if err != nil {
386388
return nil, err
387389
}
388-
extensions := getSection(config, "extensions")
390+
extensions := getSection(config, extensions)
389391
if len(extensions) > 0 {
390392
cfg.Extensions = extensions
391393
}
@@ -547,7 +549,7 @@ func formatInvalidKeyError(keyPrefix string, key interface{}) error {
547549
func LoadServices(filename string, servicesDict map[string]interface{}, workingDir string, lookupEnv template.Mapping, opts *Options) ([]types.ServiceConfig, error) {
548550
var services []types.ServiceConfig
549551

550-
x, ok := servicesDict["extensions"]
552+
x, ok := servicesDict[extensions]
551553
if ok {
552554
// as a top-level attribute, "services" doesn't support extensions, and a service can be named `x-foo`
553555
for k, v := range x.(map[string]interface{}) {

loader/loader_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ services:
315315
assert.Check(t, is.Len(actual.Services, 1))
316316
service := actual.Services[0]
317317
assert.Check(t, is.Equal("busybox", service.Image))
318-
extras := map[string]interface{}{
318+
extras := types.Extensions{
319319
"x-foo": "bar",
320320
}
321321
assert.Check(t, is.DeepEqual(extras, service.Extensions))
@@ -2301,3 +2301,18 @@ volumes:
23012301
path := project.Volumes["data"].DriverOpts["device"]
23022302
assert.Check(t, filepath.IsAbs(path))
23032303
}
2304+
2305+
func TestLoadServiceExtension(t *testing.T) {
2306+
dict := `
2307+
services:
2308+
extension: # this name should be allowed
2309+
image: web
2310+
x-foo: bar
2311+
`
2312+
configDetails := buildConfigDetails(dict, nil)
2313+
2314+
project, err := Load(configDetails)
2315+
assert.NilError(t, err)
2316+
assert.Equal(t, project.Services[0].Name, "extension")
2317+
assert.Equal(t, project.Services[0].Extensions["x-foo"], "bar")
2318+
}

types/project.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type Project struct {
4141
Volumes Volumes `yaml:",omitempty" json:"volumes,omitempty"`
4242
Secrets Secrets `yaml:",omitempty" json:"secrets,omitempty"`
4343
Configs Configs `yaml:",omitempty" json:"configs,omitempty"`
44-
Extensions Extensions `yaml:",inline" json:"-"` // https://github.com/golang/go/issues/6213
44+
Extensions Extensions `mapstructure:"#extensions" yaml:",inline" json:"-"` // https://github.com/golang/go/issues/6213
4545
ComposeFiles []string `yaml:"-" json:"-"`
4646
Environment Mapping `yaml:"-" json:"-"`
4747

0 commit comments

Comments
 (0)