api: Return empty list instead of nil

pull/2988/head
Frank Schroeder 2017-04-27 18:22:07 -07:00 committed by Frank Schröder
parent 9685bdcd0b
commit 84d6ac2d51
3 changed files with 60 additions and 4 deletions

View File

@ -96,6 +96,14 @@ func (s *HTTPServer) AgentServices(resp http.ResponseWriter, req *http.Request)
if err := s.agent.filterServices(token, &services); err != nil { if err := s.agent.filterServices(token, &services); err != nil {
return nil, err 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 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 { if err := s.agent.filterChecks(token, &checks); err != nil {
return nil, err 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 return checks, nil
} }

View File

@ -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 { if err := s.agent.RPC("Catalog.ListServices", &args, &out); err != nil {
return nil, err return nil, err
} }
// Use empty map instead of nil
if out.Services == nil {
out.Services = make(structs.Services, 0)
}
return out.Services, nil return out.Services, nil
} }
@ -135,6 +140,11 @@ func (s *HTTPServer) CatalogServiceNodes(resp http.ResponseWriter, req *http.Req
if out.ServiceNodes == nil { if out.ServiceNodes == nil {
out.ServiceNodes = make(structs.ServiceNodes, 0) out.ServiceNodes = make(structs.ServiceNodes, 0)
} }
for _, s := range out.ServiceNodes {
if s.ServiceTags == nil {
s.ServiceTags = make([]string, 0)
}
}
return out.ServiceNodes, nil 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) 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 return out.NodeServices, nil
} }

View File

@ -37,6 +37,11 @@ func (s *HTTPServer) HealthChecksInState(resp http.ResponseWriter, req *http.Req
if out.HealthChecks == nil { if out.HealthChecks == nil {
out.HealthChecks = make(structs.HealthChecks, 0) out.HealthChecks = make(structs.HealthChecks, 0)
} }
for _, c := range out.HealthChecks {
if c.ServiceTags == nil {
c.ServiceTags = make([]string, 0)
}
}
return out.HealthChecks, nil return out.HealthChecks, nil
} }
@ -66,6 +71,11 @@ func (s *HTTPServer) HealthNodeChecks(resp http.ResponseWriter, req *http.Reques
if out.HealthChecks == nil { if out.HealthChecks == nil {
out.HealthChecks = make(structs.HealthChecks, 0) out.HealthChecks = make(structs.HealthChecks, 0)
} }
for _, c := range out.HealthChecks {
if c.ServiceTags == nil {
c.ServiceTags = make([]string, 0)
}
}
return out.HealthChecks, nil return out.HealthChecks, nil
} }
@ -97,6 +107,11 @@ func (s *HTTPServer) HealthServiceChecks(resp http.ResponseWriter, req *http.Req
if out.HealthChecks == nil { if out.HealthChecks == nil {
out.HealthChecks = make(structs.HealthChecks, 0) out.HealthChecks = make(structs.HealthChecks, 0)
} }
for _, c := range out.HealthChecks {
if c.ServiceTags == nil {
c.ServiceTags = make([]string, 0)
}
}
return out.HealthChecks, nil 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) translateAddresses(s.agent.config, args.Datacenter, out.Nodes)
// Use empty list instead of nil // Use empty list instead of nil
if out.Nodes == nil {
out.Nodes = make(structs.CheckServiceNodes, 0)
}
for i := range out.Nodes { for i := range out.Nodes {
// TODO (slackpad) It's lame that this isn't a slice of pointers // 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 // 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 { if out.Nodes[i].Checks == nil {
out.Nodes[i].Checks = make(structs.HealthChecks, 0) 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 return out.Nodes, nil
} }