Skip to content

Commit 7105042

Browse files
committed
avoid conflict with extension used as service name
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 882507f commit 7105042

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
@@ -313,6 +313,8 @@ func parseConfig(b []byte, opts *Options) (map[string]interface{}, error) {
313313
return yml, err
314314
}
315315

316+
const extensions = "#extensions" // Using # prefix, we prevent risk to conflict with an actual yaml key
317+
316318
func groupXFieldsIntoExtensions(dict map[string]interface{}) map[string]interface{} {
317319
extras := map[string]interface{}{}
318320
for key, value := range dict {
@@ -325,7 +327,7 @@ func groupXFieldsIntoExtensions(dict map[string]interface{}) map[string]interfac
325327
}
326328
}
327329
if len(extras) > 0 {
328-
dict["extensions"] = extras
330+
dict[extensions] = extras
329331
}
330332
return dict
331333
}
@@ -364,7 +366,7 @@ func loadSections(filename string, config map[string]interface{}, configDetails
364366
if err != nil {
365367
return nil, err
366368
}
367-
extensions := getSection(config, "extensions")
369+
extensions := getSection(config, extensions)
368370
if len(extensions) > 0 {
369371
cfg.Extensions = extensions
370372
}
@@ -526,7 +528,7 @@ func formatInvalidKeyError(keyPrefix string, key interface{}) error {
526528
func LoadServices(filename string, servicesDict map[string]interface{}, workingDir string, lookupEnv template.Mapping, opts *Options) ([]types.ServiceConfig, error) {
527529
var services []types.ServiceConfig
528530

529-
x, ok := servicesDict["extensions"]
531+
x, ok := servicesDict[extensions]
530532
if ok {
531533
// as a top-level attribute, "services" doesn't support extensions, and a service can be named `x-foo`
532534
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
@@ -308,7 +308,7 @@ services:
308308
assert.Check(t, is.Len(actual.Services, 1))
309309
service := actual.Services[0]
310310
assert.Check(t, is.Equal("busybox", service.Image))
311-
extras := map[string]interface{}{
311+
extras := types.Extensions{
312312
"x-foo": "bar",
313313
}
314314
assert.Check(t, is.DeepEqual(extras, service.Extensions))
@@ -2215,3 +2215,18 @@ volumes:
22152215
path := project.Volumes["data"].DriverOpts["device"]
22162216
assert.Check(t, filepath.IsAbs(path))
22172217
}
2218+
2219+
func TestLoadServiceExtension(t *testing.T) {
2220+
dict := `
2221+
services:
2222+
extension: # this name should be allowed
2223+
image: web
2224+
x-foo: bar
2225+
`
2226+
configDetails := buildConfigDetails(dict, nil)
2227+
2228+
project, err := Load(configDetails)
2229+
assert.NilError(t, err)
2230+
assert.Equal(t, project.Services[0].Name, "extension")
2231+
assert.Equal(t, project.Services[0].Extensions["x-foo"], "bar")
2232+
}

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)