You've already forked dcmerge
							
							fix(dockerCompose): add ServiceNetwork.MergeFirstWin()
This commit is contained in:
		@@ -939,6 +939,29 @@ func (sn *ServiceNetwork) Equal(equalable Equalable) bool {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// MergeFirstWin adds only attributes of the passed
 | 
			
		||||
// serviceNetwork if they are undefined.
 | 
			
		||||
func (sn *ServiceNetwork) MergeFirstWin(serviceNetwork *ServiceNetwork) {
 | 
			
		||||
	switch {
 | 
			
		||||
	case sn == nil && serviceNetwork == nil:
 | 
			
		||||
		fallthrough
 | 
			
		||||
	case sn != nil && serviceNetwork == nil:
 | 
			
		||||
		return
 | 
			
		||||
 | 
			
		||||
	// WARN: It's not possible to change the memory pointer sn *ServiceNetwork to a new
 | 
			
		||||
	// initialized ServiceNetwork without returning the serviceNetwork it self.
 | 
			
		||||
	//
 | 
			
		||||
	// case l == nil && serviceULimits != nil:
 | 
			
		||||
	// 	l = NewServiceULimits()
 | 
			
		||||
	// 	fallthrough
 | 
			
		||||
 | 
			
		||||
	case sn == nil && serviceNetwork != nil:
 | 
			
		||||
		sn = serviceNetwork
 | 
			
		||||
	default:
 | 
			
		||||
		sn.mergeFirstWinAliases(serviceNetwork.Aliases)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// MergeLastWin merges adds or overwrite the attributes of the passed
 | 
			
		||||
// serviceNetwork with the existing one.
 | 
			
		||||
func (sn *ServiceNetwork) MergeLastWin(serviceNetwork *ServiceNetwork) {
 | 
			
		||||
@@ -963,9 +986,17 @@ func (sn *ServiceNetwork) MergeLastWin(serviceNetwork *ServiceNetwork) {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (sn *ServiceNetwork) mergeFirstWinAliases(aliases []string) {
 | 
			
		||||
	for _, alias := range aliases {
 | 
			
		||||
		if !existsInSlice(sn.Aliases, alias) && len(alias) > 0 {
 | 
			
		||||
			sn.Aliases = append(sn.Aliases, alias)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (sn *ServiceNetwork) mergeLastWinAliases(aliases []string) {
 | 
			
		||||
	for _, alias := range aliases {
 | 
			
		||||
		if !existsInSlice(sn.Aliases, alias) {
 | 
			
		||||
		if !existsInSlice(sn.Aliases, alias) && len(alias) > 0 {
 | 
			
		||||
			sn.Aliases = append(sn.Aliases, alias)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -1736,6 +1736,65 @@ func TestServiceNetwork_Equal(t *testing.T) {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestServiceNetwork_MergeFirstWin(t *testing.T) {
 | 
			
		||||
	require := require.New(t)
 | 
			
		||||
 | 
			
		||||
	testCases := []struct {
 | 
			
		||||
		ServiceNetworkA        *dockerCompose.ServiceNetwork
 | 
			
		||||
		ServiceNetworkB        *dockerCompose.ServiceNetwork
 | 
			
		||||
		expectedServiceNetwork *dockerCompose.ServiceNetwork
 | 
			
		||||
	}{
 | 
			
		||||
		{
 | 
			
		||||
			ServiceNetworkA:        nil,
 | 
			
		||||
			ServiceNetworkB:        nil,
 | 
			
		||||
			expectedServiceNetwork: nil,
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			ServiceNetworkA:        &dockerCompose.ServiceNetwork{},
 | 
			
		||||
			ServiceNetworkB:        nil,
 | 
			
		||||
			expectedServiceNetwork: &dockerCompose.ServiceNetwork{},
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			ServiceNetworkA: &dockerCompose.ServiceNetwork{
 | 
			
		||||
				Aliases: []string{"my-app.example.com"},
 | 
			
		||||
			},
 | 
			
		||||
			ServiceNetworkB: &dockerCompose.ServiceNetwork{
 | 
			
		||||
				Aliases: []string{"my-app.example.com"},
 | 
			
		||||
			},
 | 
			
		||||
			expectedServiceNetwork: &dockerCompose.ServiceNetwork{
 | 
			
		||||
				Aliases: []string{"my-app.example.com"},
 | 
			
		||||
			},
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			ServiceNetworkA: &dockerCompose.ServiceNetwork{
 | 
			
		||||
				Aliases: []string{"my-app.example.com"},
 | 
			
		||||
			},
 | 
			
		||||
			ServiceNetworkB: &dockerCompose.ServiceNetwork{
 | 
			
		||||
				Aliases: []string{"my-app.example.local"},
 | 
			
		||||
			},
 | 
			
		||||
			expectedServiceNetwork: &dockerCompose.ServiceNetwork{
 | 
			
		||||
				Aliases: []string{"my-app.example.com", "my-app.example.local"},
 | 
			
		||||
			},
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			ServiceNetworkA: &dockerCompose.ServiceNetwork{
 | 
			
		||||
				Aliases: []string{"my-app.example.com"},
 | 
			
		||||
			},
 | 
			
		||||
			ServiceNetworkB: &dockerCompose.ServiceNetwork{
 | 
			
		||||
				Aliases: []string{""},
 | 
			
		||||
			},
 | 
			
		||||
			expectedServiceNetwork: &dockerCompose.ServiceNetwork{
 | 
			
		||||
				Aliases: []string{"my-app.example.com"},
 | 
			
		||||
			},
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for i, testCase := range testCases {
 | 
			
		||||
		testCase.ServiceNetworkA.MergeFirstWin(testCase.ServiceNetworkB)
 | 
			
		||||
		require.True(testCase.expectedServiceNetwork.Equal(testCase.ServiceNetworkA), "Failed test case %v", i)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestServiceNetwork_MergeLastWin(t *testing.T) {
 | 
			
		||||
	require := require.New(t)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user