Skip to content

Commit

Permalink
fix: avoid stack overflow
Browse files Browse the repository at this point in the history
Signed-off-by: Norman Meier <[email protected]>
  • Loading branch information
n0izn0iz committed May 2, 2024
1 parent e1e1451 commit dbd63ac
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,24 +163,29 @@ type Conflict struct {
}

func (c Conflict) ToJSON() *json.Node {
responseMessage := json.NullNode("")
children := map[string]*json.Node{
"initiator": json.StringNode("", c.initiator.String()),
"createdAt": json.StringNode("", c.createdAt.Format(time.RFC3339)),
"initiatorMessage": json.StringNode("", c.initiatorMessage),
}

if c.responseMessage != nil {
responseMessage = json.StringNode("", *c.responseMessage)
children["responseMessage"] = json.StringNode("", *c.responseMessage)
}
if c.respondedAt != nil {
children["respondedAt"] = json.StringNode("", c.respondedAt.Format(time.RFC3339))
}
if c.resolvedAt != nil {
children["resolvedAt"] = json.StringNode("", c.resolvedAt.Format(time.RFC3339))
}
resolutionMessage := json.NullNode("")
if c.resolutionMessage != nil {
resolutionMessage = json.StringNode("", *c.resolutionMessage)
children["resolutionMessage"] = json.StringNode("", *c.resolutionMessage)
}
return json.ObjectNode("", map[string]*json.Node{
"initiator": json.StringNode("", c.initiator.String()),
"createdAt": json.StringNode("", c.createdAt.Format(time.RFC3339)),
"respondedAt": json.StringNode("", c.respondedAt.Format(time.RFC3339)),
"resolvedAt": json.StringNode("", c.resolvedAt.Format(time.RFC3339)),
"initiatorMessage": json.StringNode("", c.initiatorMessage),
"responseMessage": responseMessage,
"resolutionMessage": resolutionMessage,
"outcome": c.outcome.ToJSON(),
})
if c.outcome != nil {
children["outcome"] = c.outcome.ToJSON()
}

return json.ObjectNode("", children)
}

type Contract struct {
Expand Down Expand Up @@ -219,6 +224,9 @@ func (c Contract) ToJSON() *json.Node {
}

conflicts := make([]*json.Node, len(c.conflicts))
for i, conflict := range c.conflicts {
conflicts[i] = conflict.ToJSON()
}

return json.ObjectNode("", map[string]*json.Node{
"id": json.StringNode("", strconv.FormatUint(c.id, 10)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func TestJSONRender(t *testing.T) {
t.Fatalf("Error marshalling contract to JSON: %s", err)
}

expected := `{"id":1,"sender":"sender","contractor":"contractor2","contractorCandidates":["contractor1","contractor2"],"funder":"funder","paymentDenom":"denom","metadata":"metadata","status":"CREATED","expireAt":"2021-08-31T00:00:00Z","funderFeedback":"funderFeedback","contractorFeedback":"contractorFeedback","milestones":[{"id":1,"title":"title","desc":"desc","amount":100,"paid":0,"duration":2592000000000000,"link":"link","funded":false,"priority":"MS_PRIORITY_HIGH","status":"MS_OPEN"}],"pausedBy":"pausedBy","conflictHandler":"conflictHandler","handlerCandidate":"handlerCandidate","handlerSuggestor":"handlerSuggestor","createdAt":"2021-08-01T00:00:00Z","budget":1000,"funded":false,"rejectReason":"rejectReason","conflicts":[{"initiator":"initiator","createdAt":"2021-08-01T00:00:00Z","respondedAt":null,"resolvedAt":null,"initiatorMessage":"initiatorMessage","responseMessage":null,"resolutionMessage":null,"outcome":null}]}`
if output != expected {
t.Errorf("Expected output to be `%s`, got `%s`", expected, output)
expected := `{"id":"1","sender":"sender","contractor":"contractor2","contractorCandidates":["contractor1","contractor2"],"funder":"funder","paymentDenom":"denom","metadata":"metadata","status":"CREATED","expireAt":"2021-08-31T00:00:00Z","funderFeedback":"funderFeedback","contractorFeedback":"contractorFeedback","milestones":[{"id":"1","title":"title","desc":"desc","amount":"100","paid":"0","duration":2592000,"link":"link","funded":false,"priority":"MS_PRIORITY_HIGH","status":"MS_OPEN"}],"pausedBy":"pausedBy","conflictHandler":"conflictHandler","handlerCandidate":"handlerCandidate","handlerSuggestor":"handlerSuggestor","createdAt":"2021-08-01T00:00:00Z","budget":"1000","funded":false,"rejectReason":"rejectReason","conflicts":[{"initiator":"initiator","createdAt":"2021-08-01T00:00:00Z","initiatorMessage":"initiatorMessage"}]}`
if string(output) != expected {
t.Errorf("Expected output to be `%s`, got:\n`%s`", expected, string(output))
}
}

0 comments on commit dbd63ac

Please sign in to comment.