Skip to content

Commit

Permalink
receptor.RoutingInfo is a reference type
Browse files Browse the repository at this point in the history
structs that use it do not need to reference it via a pointer to get the
desired omitempty behavior.

[#86337946]
  • Loading branch information
sykesm committed Feb 5, 2015
1 parent 38dc009 commit 3cb6b8b
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 51 deletions.
2 changes: 1 addition & 1 deletion cmd/receptor/desired_lrp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var _ = Describe("Desired LRP API", func() {
update := receptor.DesiredLRPUpdateRequest{
Instances: &instances,
Annotation: &annotation,
Routes: &routingInfo,
Routes: routingInfo,
}

updateErr = client.UpdateDesiredLRP(createLRPReq.ProcessGuid, update)
Expand Down
2 changes: 1 addition & 1 deletion cmd/receptor/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ var _ = Describe("Event", func() {

desiredLRPChangedEvent, ok := event.(receptor.DesiredLRPChangedEvent)
Ω(ok).Should(BeTrue())
Ω(*desiredLRPChangedEvent.After.Routes).Should(Equal(receptor.RoutingInfo(newRoutes)))
Ω(desiredLRPChangedEvent.After.Routes).Should(Equal(receptor.RoutingInfo(newRoutes)))

By("removing the DesiredLRP")
err = bbs.RemoveDesiredLRPByProcessGuid(logger, desiredLRP.ProcessGuid)
Expand Down
2 changes: 1 addition & 1 deletion handlers/desired_lrp_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ var _ = Describe("Desired LRP Handlers", func() {
validUpdateRequest := receptor.DesiredLRPUpdateRequest{
Instances: &instances,
Annotation: &annotation,
Routes: &routingInfo,
Routes: routingInfo,
}

expectedUpdate := models.DesiredLRPUpdate{
Expand Down
10 changes: 5 additions & 5 deletions resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ type DesiredLRPCreateRequest struct {
CPUWeight uint `json:"cpu_weight"`
Privileged bool `json:"privileged"`
Ports []uint16 `json:"ports"`
Routes *RoutingInfo `json:"routes,omitempty"`
Routes RoutingInfo `json:"routes,omitempty"`
LogGuid string `json:"log_guid"`
LogSource string `json:"log_source"`
Annotation string `json:"annotation,omitempty"`
Expand Down Expand Up @@ -272,9 +272,9 @@ func (request *DesiredLRPCreateRequest) UnmarshalJSON(payload []byte) error {
}

type DesiredLRPUpdateRequest struct {
Instances *int `json:"instances,omitempty"`
Routes *RoutingInfo `json:"routes,omitempty"`
Annotation *string `json:"annotation,omitempty"`
Instances *int `json:"instances,omitempty"`
Routes RoutingInfo `json:"routes,omitempty"`
Annotation *string `json:"annotation,omitempty"`
}

type DesiredLRPResponse struct {
Expand All @@ -293,7 +293,7 @@ type DesiredLRPResponse struct {
CPUWeight uint `json:"cpu_weight"`
Privileged bool `json:"privileged"`
Ports []uint16 `json:"ports"`
Routes *RoutingInfo `json:"routes,omitempty"`
Routes RoutingInfo `json:"routes,omitempty"`
LogGuid string `json:"log_guid"`
LogSource string `json:"log_source"`
Annotation string `json:"annotation,omitempty"`
Expand Down
72 changes: 46 additions & 26 deletions resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,48 +326,68 @@ var _ = Describe("Resources", func() {
})

Describe("RoutingInfo", func() {
const jsonRoutes = `{
"cf-router": [{ "port": 1, "hostnames": ["a", "b"]}],
"foo" : "bar"
}`

var routingInfo receptor.RoutingInfo

BeforeEach(func() {
routingInfo = receptor.RoutingInfo{}
})

Context("Serialization", func() {
jsonRoutes := `{
"cf-router": [{ "port": 1, "hostnames": ["a", "b"]}],
"foo" : "bar"
}`
Describe("MarshalJson", func() {
It("marshals routes when present", func() {
routingInfo := receptor.RoutingInfo{}

Context("MarshalJson", func() {
It("marshals routes when present", func() {
routingInfo := receptor.RoutingInfo{}
bytes, err := json.Marshal(receptor.CFRoutes{
{Hostnames: []string{"a", "b"}, Port: 1},
})
Ω(err).ShouldNot(HaveOccurred())

bytes, err := json.Marshal(receptor.CFRoutes{
{Hostnames: []string{"a", "b"}, Port: 1},
})
Ω(err).ShouldNot(HaveOccurred())
cfRawMessage := json.RawMessage(bytes)
routingInfo[receptor.CF_ROUTER] = &cfRawMessage

fooRawMessage := json.RawMessage([]byte(`"bar"`))
routingInfo["foo"] = &fooRawMessage

cfRawMessage := json.RawMessage(bytes)
routingInfo[receptor.CF_ROUTER] = &cfRawMessage
bytes, err = json.Marshal(routingInfo)
Ω(err).ShouldNot(HaveOccurred())
Ω(bytes).Should(MatchJSON(jsonRoutes))
})

fooRawMessage := json.RawMessage([]byte(`"bar"`))
routingInfo["foo"] = &fooRawMessage
Context("when the routing info is empty", func() {
It("serialization can omit it", func() {
type wrapper struct {
Name string `json:"name"`
Routes receptor.RoutingInfo `json:"routes,omitempty"`
}

bytes, err = json.Marshal(routingInfo)
w := wrapper{
Name: "wrapper",
Routes: receptor.RoutingInfo{},
}

Ω(w.Routes).ShouldNot(BeNil())
Ω(w.Routes).Should(BeEmpty())

bytes, err := json.Marshal(w)
Ω(err).ShouldNot(HaveOccurred())
Ω(bytes).Should(MatchJSON(jsonRoutes))

Ω(bytes).Should(MatchJSON(`{"name":"wrapper"}`))
})
})
})

Context("Unmarshal", func() {
It("returns both cf-routes and other", func() {
err := json.Unmarshal([]byte(jsonRoutes), &routingInfo)
Ω(err).ShouldNot(HaveOccurred())
Describe("Unmarshal", func() {
It("returns both cf-routes and other", func() {
err := json.Unmarshal([]byte(jsonRoutes), &routingInfo)
Ω(err).ShouldNot(HaveOccurred())

Ω(len(routingInfo)).Should(Equal(2))
Ω(string(*routingInfo["cf-router"])).Should(MatchJSON(`[{"port": 1,"hostnames":["a", "b"]}]`))
Ω(string(*routingInfo["foo"])).Should(MatchJSON(`"bar"`))
})
Ω(len(routingInfo)).Should(Equal(2))
Ω(string(*routingInfo["cf-router"])).Should(MatchJSON(`[{"port": 1,"hostnames":["a", "b"]}]`))
Ω(string(*routingInfo["foo"])).Should(MatchJSON(`"bar"`))
})
})
})
Expand Down
8 changes: 4 additions & 4 deletions routing_info_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ type CFRoute struct {
Port uint16 `json:"port"`
}

func (c CFRoutes) RoutingInfo() *RoutingInfo {
func (c CFRoutes) RoutingInfo() RoutingInfo {
data, _ := json.Marshal(c)
routingInfo := json.RawMessage(data)
return &RoutingInfo{
return RoutingInfo{
CF_ROUTER: &routingInfo,
}
}

func CFRoutesFromRoutingInfo(routingInfo *RoutingInfo) (CFRoutes, error) {
func CFRoutesFromRoutingInfo(routingInfo RoutingInfo) (CFRoutes, error) {
if routingInfo == nil {
return nil, nil
}

data, found := (*routingInfo)[CF_ROUTER]
data, found := routingInfo[CF_ROUTER]
if !found {
return nil, nil
}
Expand Down
12 changes: 6 additions & 6 deletions routing_info_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var _ = Describe("RoutingInfoHelpers", func() {
})

Describe("RoutingInfo", func() {
var routingInfo *receptor.RoutingInfo
var routingInfo receptor.RoutingInfo

JustBeforeEach(func() {
routingInfo = routes.RoutingInfo()
Expand All @@ -46,7 +46,7 @@ var _ = Describe("RoutingInfoHelpers", func() {
expectedBytes, err := json.Marshal(routes)
Ω(err).ShouldNot(HaveOccurred())

payload, err := (*routingInfo)[receptor.CF_ROUTER].MarshalJSON()
payload, err := routingInfo[receptor.CF_ROUTER].MarshalJSON()
Ω(err).ShouldNot(HaveOccurred())

Ω(payload).Should(MatchJSON(expectedBytes))
Expand All @@ -58,7 +58,7 @@ var _ = Describe("RoutingInfoHelpers", func() {
})

It("marshals an empty list", func() {
payload, err := (*routingInfo)[receptor.CF_ROUTER].MarshalJSON()
payload, err := routingInfo[receptor.CF_ROUTER].MarshalJSON()
Ω(err).ShouldNot(HaveOccurred())

Ω(payload).Should(MatchJSON(`[]`))
Expand All @@ -71,7 +71,7 @@ var _ = Describe("RoutingInfoHelpers", func() {
routesResult receptor.CFRoutes
conversionError error

routingInfo *receptor.RoutingInfo
routingInfo receptor.RoutingInfo
)

JustBeforeEach(func() {
Expand All @@ -89,7 +89,7 @@ var _ = Describe("RoutingInfoHelpers", func() {

Context("when the CF routes are nil", func() {
BeforeEach(func() {
routingInfo = &receptor.RoutingInfo{receptor.CF_ROUTER: nil}
routingInfo = receptor.RoutingInfo{receptor.CF_ROUTER: nil}
})

It("returns nil routes", func() {
Expand All @@ -101,7 +101,7 @@ var _ = Describe("RoutingInfoHelpers", func() {

Context("when CF routes are not present in the routing info", func() {
BeforeEach(func() {
routingInfo = &receptor.RoutingInfo{}
routingInfo = receptor.RoutingInfo{}
})

It("returns nil routes", func() {
Expand Down
8 changes: 4 additions & 4 deletions serialization/desired_lrps.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,20 @@ func DesiredLRPUpdateFromRequest(req receptor.DesiredLRPUpdateRequest) models.De
}
}

func RoutingInfoToRawMessages(r *receptor.RoutingInfo) map[string]*json.RawMessage {
func RoutingInfoToRawMessages(r receptor.RoutingInfo) map[string]*json.RawMessage {
var messages map[string]*json.RawMessage

if r != nil {
messages = map[string]*json.RawMessage{}
for key, value := range *r {
for key, value := range r {
messages[key] = value
}
}

return messages
}

func RoutingInfoFromRawMessages(raw map[string]*json.RawMessage) *receptor.RoutingInfo {
func RoutingInfoFromRawMessages(raw map[string]*json.RawMessage) receptor.RoutingInfo {
if raw == nil {
return nil
}
Expand All @@ -112,5 +112,5 @@ func RoutingInfoFromRawMessages(raw map[string]*json.RawMessage) *receptor.Routi
for key, value := range raw {
info[key] = value
}
return &info
return info
}
6 changes: 3 additions & 3 deletions serialization/desired_lrps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var _ = Describe("DesiredLRP Serialization", func() {
EgressRules: []models.SecurityGroupRule{
securityRule,
},
Routes: &routingInfo,
Routes: routingInfo,
}
})
JustBeforeEach(func() {
Expand Down Expand Up @@ -139,7 +139,7 @@ var _ = Describe("DesiredLRP Serialization", func() {
Ports: []uint16{
456,
},
Routes: &routingInfo,
Routes: routingInfo,
LogGuid: "log-guid-0",
LogSource: "log-source-name-0",
Annotation: "annotation-0",
Expand Down Expand Up @@ -175,7 +175,7 @@ var _ = Describe("DesiredLRP Serialization", func() {
Ports: []uint16{
456,
},
Routes: &routingInfo,
Routes: routingInfo,
LogGuid: "log-guid-0",
LogSource: "log-source-name-0",
Annotation: "annotation-0",
Expand Down

0 comments on commit 3cb6b8b

Please sign in to comment.