From 615df13a13a84bbb6d6ee21b2cd2b34295a07cbe Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 30 Nov 2022 12:35:10 +0100 Subject: [PATCH] fix network aliasses merge logic to conform to docker compose v1 Signed-off-by: Nicolas De Loof --- loader/merge.go | 15 --------------- loader/merge_test.go | 17 ++++++----------- 2 files changed, 6 insertions(+), 26 deletions(-) diff --git a/loader/merge.go b/loader/merge.go index ce7c1dcf..2b1e330d 100644 --- a/loader/merge.go +++ b/loader/merge.go @@ -38,7 +38,6 @@ var serviceSpecials = &specials{ reflect.TypeOf([]types.ServiceSecretConfig{}): mergeSlice(toServiceSecretConfigsMap, toServiceSecretConfigsSlice), reflect.TypeOf([]types.ServiceConfigObjConfig{}): mergeSlice(toServiceConfigObjConfigsMap, toSServiceConfigObjConfigsSlice), reflect.TypeOf(&types.UlimitsConfig{}): mergeUlimitsConfig, - reflect.TypeOf(&types.ServiceNetworkConfig{}): mergeServiceNetworkConfig, }, } @@ -327,20 +326,6 @@ func mergeUlimitsConfig(dst, src reflect.Value) error { return nil } -// nolint: unparam -func mergeServiceNetworkConfig(dst, src reflect.Value) error { - if src.Interface() != reflect.Zero(reflect.TypeOf(src.Interface())).Interface() { - dst.Elem().FieldByName("Aliases").Set(src.Elem().FieldByName("Aliases")) - if ipv4 := src.Elem().FieldByName("Ipv4Address").Interface().(string); ipv4 != "" { - dst.Elem().FieldByName("Ipv4Address").SetString(ipv4) - } - if ipv6 := src.Elem().FieldByName("Ipv6Address").Interface().(string); ipv6 != "" { - dst.Elem().FieldByName("Ipv6Address").SetString(ipv6) - } - } - return nil -} - func getLoggingDriver(v reflect.Value) string { return v.FieldByName("Driver").String() } diff --git a/loader/merge_test.go b/loader/merge_test.go index 4f173b11..698c5108 100644 --- a/loader/merge_test.go +++ b/loader/merge_test.go @@ -816,7 +816,7 @@ func TestLoadMultipleServiceNetworks(t *testing.T) { }, expected: map[string]*types.ServiceNetworkConfig{ "net1": { - Aliases: []string{"alias2", "alias3"}, + Aliases: []string{"alias1", "alias2", "alias3"}, }, "net2": nil, "net3": {}, @@ -1111,13 +1111,8 @@ func TestMergeUlimitsConfig(t *testing.T) { } func TestMergeServiceNetworkConfig(t *testing.T) { - specials := &specials{ - m: map[reflect.Type]func(dst, src reflect.Value) error{ - reflect.TypeOf(&types.ServiceNetworkConfig{}): mergeServiceNetworkConfig, - }, - } base := map[string]*types.ServiceNetworkConfig{ - "override-aliases": { + "merge": { Aliases: []string{"100", "101"}, Ipv4Address: "127.0.0.1", Ipv6Address: "0:0:0:0:0:0:0:1", @@ -1129,7 +1124,7 @@ func TestMergeServiceNetworkConfig(t *testing.T) { }, } override := map[string]*types.ServiceNetworkConfig{ - "override-aliases": { + "merge": { Aliases: []string{"110", "111"}, Ipv4Address: "127.0.1.1", Ipv6Address: "0:0:0:0:0:0:1:1", @@ -1140,14 +1135,14 @@ func TestMergeServiceNetworkConfig(t *testing.T) { Ipv6Address: "0:0:0:0:0:0:3:1", }, } - err := mergo.Merge(&base, &override, mergo.WithOverride, mergo.WithTransformers(specials)) + err := mergo.Merge(&base, &override, mergo.WithAppendSlice, mergo.WithOverride) assert.NilError(t, err) assert.DeepEqual( t, base, map[string]*types.ServiceNetworkConfig{ - "override-aliases": { - Aliases: []string{"110", "111"}, + "merge": { + Aliases: []string{"100", "101", "110", "111"}, Ipv4Address: "127.0.1.1", Ipv6Address: "0:0:0:0:0:0:1:1", },