From dbd63ac845d14a02f927302e6af7b59107dff4b1 Mon Sep 17 00:00:00 2001 From: Norman Meier Date: Thu, 2 May 2024 23:45:30 +0200 Subject: [PATCH] fix: avoid stack overflow Signed-off-by: Norman Meier --- .../projects_manager/projects_manager.gno | 36 +++++++++++-------- .../projects_manager_test.gno | 6 ++-- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/examples/gno.land/r/demo/teritori/projects_manager/projects_manager.gno b/examples/gno.land/r/demo/teritori/projects_manager/projects_manager.gno index 93af1876b29..bbf3150f6e0 100644 --- a/examples/gno.land/r/demo/teritori/projects_manager/projects_manager.gno +++ b/examples/gno.land/r/demo/teritori/projects_manager/projects_manager.gno @@ -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 { @@ -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)), diff --git a/examples/gno.land/r/demo/teritori/projects_manager/projects_manager_test.gno b/examples/gno.land/r/demo/teritori/projects_manager/projects_manager_test.gno index 78044c4b90b..f9c5d7d9dcf 100644 --- a/examples/gno.land/r/demo/teritori/projects_manager/projects_manager_test.gno +++ b/examples/gno.land/r/demo/teritori/projects_manager/projects_manager_test.gno @@ -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)) } }