From 84d6ac2d51eb9ef5fdfb291fdf0a479d3e6885d1 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Thu, 27 Apr 2017 18:22:07 -0700 Subject: [PATCH] api: Return empty list instead of nil --- command/agent/agent_endpoint.go | 16 ++++++++++++++++ command/agent/catalog_endpoint.go | 18 ++++++++++++++++++ command/agent/health_endpoint.go | 30 ++++++++++++++++++++++++++---- 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/command/agent/agent_endpoint.go b/command/agent/agent_endpoint.go index 9eef723081..5e348941a4 100644 --- a/command/agent/agent_endpoint.go +++ b/command/agent/agent_endpoint.go @@ -96,6 +96,14 @@ func (s *HTTPServer) AgentServices(resp http.ResponseWriter, req *http.Request) if err := s.agent.filterServices(token, &services); err != nil { return nil, err } + + // Use empty list instead of nil + for _, s := range services { + if s.Tags == nil { + s.Tags = make([]string, 0) + } + } + return services, nil } @@ -108,6 +116,14 @@ func (s *HTTPServer) AgentChecks(resp http.ResponseWriter, req *http.Request) (i if err := s.agent.filterChecks(token, &checks); err != nil { return nil, err } + + // Use empty list instead of nil + for _, c := range checks { + if c.ServiceTags == nil { + c.ServiceTags = make([]string, 0) + } + } + return checks, nil } diff --git a/command/agent/catalog_endpoint.go b/command/agent/catalog_endpoint.go index 5c82590ef3..b13554d0ae 100644 --- a/command/agent/catalog_endpoint.go +++ b/command/agent/catalog_endpoint.go @@ -96,6 +96,11 @@ func (s *HTTPServer) CatalogServices(resp http.ResponseWriter, req *http.Request if err := s.agent.RPC("Catalog.ListServices", &args, &out); err != nil { return nil, err } + + // Use empty map instead of nil + if out.Services == nil { + out.Services = make(structs.Services, 0) + } return out.Services, nil } @@ -135,6 +140,11 @@ func (s *HTTPServer) CatalogServiceNodes(resp http.ResponseWriter, req *http.Req if out.ServiceNodes == nil { out.ServiceNodes = make(structs.ServiceNodes, 0) } + for _, s := range out.ServiceNodes { + if s.ServiceTags == nil { + s.ServiceTags = make([]string, 0) + } + } return out.ServiceNodes, nil } @@ -163,5 +173,13 @@ func (s *HTTPServer) CatalogNodeServices(resp http.ResponseWriter, req *http.Req translateAddresses(s.agent.config, args.Datacenter, out.NodeServices.Node) } + // Use empty list instead of nil + if out.NodeServices != nil { + for _, s := range out.NodeServices.Services { + if s.Tags == nil { + s.Tags = make([]string, 0) + } + } + } return out.NodeServices, nil } diff --git a/command/agent/health_endpoint.go b/command/agent/health_endpoint.go index ab7ee4d7f6..5b0bac4c49 100644 --- a/command/agent/health_endpoint.go +++ b/command/agent/health_endpoint.go @@ -37,6 +37,11 @@ func (s *HTTPServer) HealthChecksInState(resp http.ResponseWriter, req *http.Req if out.HealthChecks == nil { out.HealthChecks = make(structs.HealthChecks, 0) } + for _, c := range out.HealthChecks { + if c.ServiceTags == nil { + c.ServiceTags = make([]string, 0) + } + } return out.HealthChecks, nil } @@ -66,6 +71,11 @@ func (s *HTTPServer) HealthNodeChecks(resp http.ResponseWriter, req *http.Reques if out.HealthChecks == nil { out.HealthChecks = make(structs.HealthChecks, 0) } + for _, c := range out.HealthChecks { + if c.ServiceTags == nil { + c.ServiceTags = make([]string, 0) + } + } return out.HealthChecks, nil } @@ -97,6 +107,11 @@ func (s *HTTPServer) HealthServiceChecks(resp http.ResponseWriter, req *http.Req if out.HealthChecks == nil { out.HealthChecks = make(structs.HealthChecks, 0) } + for _, c := range out.HealthChecks { + if c.ServiceTags == nil { + c.ServiceTags = make([]string, 0) + } + } return out.HealthChecks, nil } @@ -140,6 +155,9 @@ func (s *HTTPServer) HealthServiceNodes(resp http.ResponseWriter, req *http.Requ translateAddresses(s.agent.config, args.Datacenter, out.Nodes) // Use empty list instead of nil + if out.Nodes == nil { + out.Nodes = make(structs.CheckServiceNodes, 0) + } for i := range out.Nodes { // TODO (slackpad) It's lame that this isn't a slice of pointers // but it's not a well-scoped change to fix this. We should @@ -147,11 +165,15 @@ func (s *HTTPServer) HealthServiceNodes(resp http.ResponseWriter, req *http.Requ if out.Nodes[i].Checks == nil { out.Nodes[i].Checks = make(structs.HealthChecks, 0) } + for _, c := range out.Nodes[i].Checks { + if c.ServiceTags == nil { + c.ServiceTags = make([]string, 0) + } + } + if out.Nodes[i].Service != nil && out.Nodes[i].Service.Tags == nil { + out.Nodes[i].Service.Tags = make([]string, 0) + } } - if out.Nodes == nil { - out.Nodes = make(structs.CheckServiceNodes, 0) - } - return out.Nodes, nil }