Skip to content

Commit

Permalink
Fix mergo merging panic for volumes
Browse files Browse the repository at this point in the history
Signed-off-by: Ulysses Souza <[email protected]>
  • Loading branch information
ulyssessouza committed Feb 17, 2021
1 parent 5ed5708 commit 64250b0
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 20 deletions.
2 changes: 1 addition & 1 deletion compatibility/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func Check(project *types.Project, c Checker) {
}

for i, volume := range project.Volumes {
CheckVolumeConfig(&volume, c)
CheckVolumeConfig(volume, c)
project.Volumes[i] = volume
}

Expand Down
12 changes: 5 additions & 7 deletions loader/full-struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,9 @@ func networks() map[string]types.NetworkConfig {
}
}

func volumes() map[string]types.VolumeConfig {
return map[string]types.VolumeConfig{
"some-volume": {},
func volumes() map[string]*types.VolumeConfig {
return map[string]*types.VolumeConfig{
"some-volume": nil,
"other-volume": {
Driver: "flocker",
DriverOpts: map[string]string{
Expand Down Expand Up @@ -901,7 +901,7 @@ volumes:
foo: bar
labels:
foo: bar
some-volume: {}
some-volume: null
secrets:
secret1:
file: %s
Expand Down Expand Up @@ -1494,9 +1494,7 @@ func fullExampleJSON(workingDir, homeDir string) string {
"foo": "bar"
}
},
"some-volume": {
"external": false
}
"some-volume": null
},
"x-bar": "baz",
"x-foo": "bar",
Expand Down
6 changes: 3 additions & 3 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,14 +621,14 @@ func externalVolumeError(volume, key string) error {

// LoadVolumes produces a VolumeConfig map from a compose file Dict
// the source Dict is not validated if directly used. Use Load() to enable validation
func LoadVolumes(source map[string]interface{}) (map[string]types.VolumeConfig, error) {
volumes := make(map[string]types.VolumeConfig)
func LoadVolumes(source map[string]interface{}) (map[string]*types.VolumeConfig, error) {
volumes := make(map[string]*types.VolumeConfig)
if err := Transform(source, &volumes); err != nil {
return volumes, err
}

for name, volume := range volumes {
if !volume.External.External {
if volume == nil || !volume.External.External {
continue
}
switch {
Expand Down
8 changes: 4 additions & 4 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ var sampleConfig = types.Config{
},
},
},
Volumes: map[string]types.VolumeConfig{
Volumes: map[string]*types.VolumeConfig{
"hello": {
Driver: "default",
DriverOpts: map[string]string{
Expand Down Expand Up @@ -665,7 +665,7 @@ networks:
Secrets: map[string]types.SecretConfig{
"super": {External: types.External{External: true}, Name: "super"},
},
Volumes: map[string]types.VolumeConfig{
Volumes: map[string]*types.VolumeConfig{
"data": {External: types.External{External: true}, Name: "data"},
},
Networks: map[string]types.NetworkConfig{
Expand Down Expand Up @@ -1211,7 +1211,7 @@ func TestLoadVolumesWarnOnDeprecatedExternalNameVersion34(t *testing.T) {
}
volumes, err := LoadVolumes(source)
assert.NilError(t, err)
expected := map[string]types.VolumeConfig{
expected := map[string]*types.VolumeConfig{
"foo": {
Name: "oops",
External: types.External{External: true},
Expand Down Expand Up @@ -1242,7 +1242,7 @@ func TestLoadVolumesWarnOnDeprecatedExternalName(t *testing.T) {
}
volumes, err := LoadVolumes(source)
assert.NilError(t, err)
expected := map[string]types.VolumeConfig{
expected := map[string]*types.VolumeConfig{
"foo": {
Name: "oops",
External: types.External{External: true},
Expand Down
2 changes: 1 addition & 1 deletion loader/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func mapByName(services []types.ServiceConfig) map[string]types.ServiceConfig {
return m
}

func mergeVolumes(base, override map[string]types.VolumeConfig) (map[string]types.VolumeConfig, error) {
func mergeVolumes(base, override map[string]*types.VolumeConfig) (map[string]*types.VolumeConfig, error) {
err := mergo.Map(&base, &override, mergo.WithOverride)
return base, err
}
Expand Down
2 changes: 1 addition & 1 deletion loader/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestValidateNamedVolume(t *testing.T) {
err := checkConsistency(project)
assert.Error(t, err, `service "myservice" refers to undefined volume myVolume: invalid compose project`)

project.Volumes = types.Volumes(map[string]types.VolumeConfig{
project.Volumes = types.Volumes(map[string]*types.VolumeConfig{
"myVolume": {
Name: "myVolume",
},
Expand Down
4 changes: 2 additions & 2 deletions loader/with-version-struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func withVersionNetworks() map[string]types.NetworkConfig {
}
}

func withVersionVolumes() map[string]types.VolumeConfig {
return map[string]types.VolumeConfig{
func withVersionVolumes() map[string]*types.VolumeConfig {
return map[string]*types.VolumeConfig{
"data": {
Driver: "local",
},
Expand Down
2 changes: 1 addition & 1 deletion types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Config struct {
}

// Volumes is a map of VolumeConfig
type Volumes map[string]VolumeConfig
type Volumes map[string]*VolumeConfig

// Networks is a map of NetworkConfig
type Networks map[string]NetworkConfig
Expand Down

0 comments on commit 64250b0

Please sign in to comment.