Skip to content

Commit

Permalink
Merge pull request #255 from ndeloof/extension_conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
ndeloof authored Mar 20, 2023
2 parents 684383f + 7105042 commit e2df0ed
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 67 deletions.
8 changes: 5 additions & 3 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ func parseConfig(b []byte, opts *Options) (map[string]interface{}, error) {
return yml, err
}

const extensions = "#extensions" // Using # prefix, we prevent risk to conflict with an actual yaml key

func groupXFieldsIntoExtensions(dict map[string]interface{}) map[string]interface{} {
extras := map[string]interface{}{}
for key, value := range dict {
Expand All @@ -346,7 +348,7 @@ func groupXFieldsIntoExtensions(dict map[string]interface{}) map[string]interfac
}
}
if len(extras) > 0 {
dict["extensions"] = extras
dict[extensions] = extras
}
return dict
}
Expand Down Expand Up @@ -385,7 +387,7 @@ func loadSections(filename string, config map[string]interface{}, configDetails
if err != nil {
return nil, err
}
extensions := getSection(config, "extensions")
extensions := getSection(config, extensions)
if len(extensions) > 0 {
cfg.Extensions = extensions
}
Expand Down Expand Up @@ -547,7 +549,7 @@ func formatInvalidKeyError(keyPrefix string, key interface{}) error {
func LoadServices(filename string, servicesDict map[string]interface{}, workingDir string, lookupEnv template.Mapping, opts *Options) ([]types.ServiceConfig, error) {
var services []types.ServiceConfig

x, ok := servicesDict["extensions"]
x, ok := servicesDict[extensions]
if ok {
// as a top-level attribute, "services" doesn't support extensions, and a service can be named `x-foo`
for k, v := range x.(map[string]interface{}) {
Expand Down
17 changes: 16 additions & 1 deletion loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ services:
assert.Check(t, is.Len(actual.Services, 1))
service := actual.Services[0]
assert.Check(t, is.Equal("busybox", service.Image))
extras := map[string]interface{}{
extras := types.Extensions{
"x-foo": "bar",
}
assert.Check(t, is.DeepEqual(extras, service.Extensions))
Expand Down Expand Up @@ -2301,3 +2301,18 @@ volumes:
path := project.Volumes["data"].DriverOpts["device"]
assert.Check(t, filepath.IsAbs(path))
}

func TestLoadServiceExtension(t *testing.T) {
dict := `
services:
extension: # this name should be allowed
image: web
x-foo: bar
`
configDetails := buildConfigDetails(dict, nil)

project, err := Load(configDetails)
assert.NilError(t, err)
assert.Equal(t, project.Services[0].Name, "extension")
assert.Equal(t, project.Services[0].Extensions["x-foo"], "bar")
}
2 changes: 1 addition & 1 deletion types/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Project struct {
Volumes Volumes `yaml:",omitempty" json:"volumes,omitempty"`
Secrets Secrets `yaml:",omitempty" json:"secrets,omitempty"`
Configs Configs `yaml:",omitempty" json:"configs,omitempty"`
Extensions Extensions `yaml:",inline" json:"-"` // https://github.com/golang/go/issues/6213
Extensions Extensions `mapstructure:"#extensions" yaml:",inline" json:"-"` // https://github.com/golang/go/issues/6213
ComposeFiles []string `yaml:"-" json:"-"`
Environment Mapping `yaml:"-" json:"-"`

Expand Down
Loading

0 comments on commit e2df0ed

Please sign in to comment.