api/types/network: modernize EndpointIPAMConfig.Copy, EndpointSettings.Copy

- Use slices.Clone where suitable.
- Handle `nil` values so that callers don't have to check for `nil`.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-08-10 12:59:21 +02:00
parent feeaa167ea
commit 11094e27b0
2 changed files with 30 additions and 40 deletions

View File

@@ -3,7 +3,9 @@ package network
import (
"errors"
"fmt"
"maps"
"net"
"slices"
)
// EndpointSettings stores the network endpoint details
@@ -39,25 +41,16 @@ type EndpointSettings struct {
// Copy makes a deep copy of `EndpointSettings`
func (es *EndpointSettings) Copy() *EndpointSettings {
if es == nil {
return nil
}
epCopy := *es
if es.IPAMConfig != nil {
epCopy.IPAMConfig = es.IPAMConfig.Copy()
}
if es.Links != nil {
links := make([]string, 0, len(es.Links))
epCopy.Links = append(links, es.Links...)
}
if es.Aliases != nil {
aliases := make([]string, 0, len(es.Aliases))
epCopy.Aliases = append(aliases, es.Aliases...)
}
if len(es.DNSNames) > 0 {
epCopy.DNSNames = make([]string, len(es.DNSNames))
copy(epCopy.DNSNames, es.DNSNames)
}
epCopy.IPAMConfig = es.IPAMConfig.Copy()
epCopy.Links = slices.Clone(es.Links)
epCopy.Aliases = slices.Clone(es.Aliases)
epCopy.DNSNames = slices.Clone(es.DNSNames)
epCopy.DriverOpts = maps.Clone(es.DriverOpts)
return &epCopy
}
@@ -71,9 +64,11 @@ type EndpointIPAMConfig struct {
// Copy makes a copy of the endpoint ipam config
func (cfg *EndpointIPAMConfig) Copy() *EndpointIPAMConfig {
if cfg == nil {
return nil
}
cfgCopy := *cfg
cfgCopy.LinkLocalIPs = make([]string, 0, len(cfg.LinkLocalIPs))
cfgCopy.LinkLocalIPs = append(cfgCopy.LinkLocalIPs, cfg.LinkLocalIPs...)
cfgCopy.LinkLocalIPs = slices.Clone(cfg.LinkLocalIPs)
return &cfgCopy
}

View File

@@ -3,7 +3,9 @@ package network
import (
"errors"
"fmt"
"maps"
"net"
"slices"
)
// EndpointSettings stores the network endpoint details
@@ -39,25 +41,16 @@ type EndpointSettings struct {
// Copy makes a deep copy of `EndpointSettings`
func (es *EndpointSettings) Copy() *EndpointSettings {
if es == nil {
return nil
}
epCopy := *es
if es.IPAMConfig != nil {
epCopy.IPAMConfig = es.IPAMConfig.Copy()
}
if es.Links != nil {
links := make([]string, 0, len(es.Links))
epCopy.Links = append(links, es.Links...)
}
if es.Aliases != nil {
aliases := make([]string, 0, len(es.Aliases))
epCopy.Aliases = append(aliases, es.Aliases...)
}
if len(es.DNSNames) > 0 {
epCopy.DNSNames = make([]string, len(es.DNSNames))
copy(epCopy.DNSNames, es.DNSNames)
}
epCopy.IPAMConfig = es.IPAMConfig.Copy()
epCopy.Links = slices.Clone(es.Links)
epCopy.Aliases = slices.Clone(es.Aliases)
epCopy.DNSNames = slices.Clone(es.DNSNames)
epCopy.DriverOpts = maps.Clone(es.DriverOpts)
return &epCopy
}
@@ -71,9 +64,11 @@ type EndpointIPAMConfig struct {
// Copy makes a copy of the endpoint ipam config
func (cfg *EndpointIPAMConfig) Copy() *EndpointIPAMConfig {
if cfg == nil {
return nil
}
cfgCopy := *cfg
cfgCopy.LinkLocalIPs = make([]string, 0, len(cfg.LinkLocalIPs))
cfgCopy.LinkLocalIPs = append(cfgCopy.LinkLocalIPs, cfg.LinkLocalIPs...)
cfgCopy.LinkLocalIPs = slices.Clone(cfg.LinkLocalIPs)
return &cfgCopy
}