瀏覽代碼

daemon: refactor buildNetworkResource to use a struct-literal

Now that all helper functions are updated, we can use a struct-literal
for this function, which makes it slightly easier to read.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 1 年之前
父節點
當前提交
f3b76bc1da
共有 1 個文件被更改,包括 21 次插入23 次删除
  1. 21 23
      daemon/network.go

+ 21 - 23
daemon/network.go

@@ -593,34 +593,32 @@ func (daemon *Daemon) GetNetworks(filter filters.Args, config types.NetworkListC
 	return networks, nil
 }
 
+// buildNetworkResource builds a [types.NetworkResource] from the given
+// [libnetwork.Network], to be returned by the API.
 func buildNetworkResource(nw *libnetwork.Network) types.NetworkResource {
-	r := types.NetworkResource{}
 	if nw == nil {
-		return r
+		return types.NetworkResource{}
 	}
 
 	info := nw.Info()
-	r.Name = nw.Name()
-	r.ID = nw.ID()
-	r.Created = info.Created()
-	r.Scope = info.Scope()
-	r.Driver = nw.Type()
-	r.EnableIPv6 = info.IPv6Enabled()
-	r.Internal = info.Internal()
-	r.Attachable = info.Attachable()
-	r.Ingress = info.Ingress()
-	r.Options = info.DriverOptions()
-	r.Containers = make(map[string]types.EndpointResource)
-	r.IPAM = buildIPAMResources(info)
-	r.Labels = info.Labels()
-	r.ConfigOnly = info.ConfigOnly()
-	r.Peers = buildPeerInfoResources(info.Peers())
-
-	if cn := info.ConfigFrom(); cn != "" {
-		r.ConfigFrom = network.ConfigReference{Network: cn}
-	}
-
-	return r
+	return types.NetworkResource{
+		Name:       nw.Name(),
+		ID:         nw.ID(),
+		Created:    info.Created(),
+		Scope:      info.Scope(),
+		Driver:     nw.Type(),
+		EnableIPv6: info.IPv6Enabled(),
+		IPAM:       buildIPAMResources(info),
+		Internal:   info.Internal(),
+		Attachable: info.Attachable(),
+		Ingress:    info.Ingress(),
+		ConfigFrom: network.ConfigReference{Network: info.ConfigFrom()},
+		ConfigOnly: info.ConfigOnly(),
+		Containers: map[string]types.EndpointResource{},
+		Options:    info.DriverOptions(),
+		Labels:     info.Labels(),
+		Peers:      buildPeerInfoResources(info.Peers()),
+	}
 }
 
 // buildContainerAttachments creates a [types.EndpointResource] map of all