Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid conflict with extensions used as service name #255

Merged
merged 1 commit into from
Mar 20, 2023
Merged
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
8 changes: 5 additions & 3 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,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 @@ -325,7 +327,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 @@ -364,7 +366,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 @@ -526,7 +528,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 @@ -308,7 +308,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 @@ -2215,3 +2215,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