mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
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:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
35
vendor/github.com/moby/moby/api/types/network/endpoint.go
generated
vendored
35
vendor/github.com/moby/moby/api/types/network/endpoint.go
generated
vendored
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user