From 76a3c2af9b707960b568e2af44e858aaa4b8c959 Mon Sep 17 00:00:00 2001 From: mcrakhman Date: Thu, 2 Mar 2023 21:08:14 +0100 Subject: [PATCH] Fix change builder with no root --- commonspace/object/tree/objecttree/changebuilder.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/commonspace/object/tree/objecttree/changebuilder.go b/commonspace/object/tree/objecttree/changebuilder.go index f23648b0..29a1a500 100644 --- a/commonspace/object/tree/objecttree/changebuilder.go +++ b/commonspace/object/tree/objecttree/changebuilder.go @@ -220,7 +220,7 @@ func (c *changeBuilder) Build(payload BuilderContent) (ch *Change, rawIdChange * } func (c *changeBuilder) Marshall(ch *Change) (raw *treechangeproto.RawTreeChangeWithId, err error) { - if ch.Id == c.rootChange.Id { + if c.isRoot(ch.Id) { return c.rootChange, nil } treeChange := &treechangeproto.TreeChange{ @@ -255,7 +255,7 @@ func (c *changeBuilder) Marshall(ch *Change) (raw *treechangeproto.RawTreeChange } func (c *changeBuilder) unmarshallRawChange(raw *treechangeproto.RawTreeChange, id string) (ch *Change, err error) { - if c.rootChange.Id == id { + if c.isRoot(id) { unmarshalled := &treechangeproto.RootChange{} err = proto.Unmarshal(raw.Payload, unmarshalled) if err != nil { @@ -274,3 +274,10 @@ func (c *changeBuilder) unmarshallRawChange(raw *treechangeproto.RawTreeChange, ch = NewChange(id, unmarshalled, raw.Signature) return } + +func (c *changeBuilder) isRoot(id string) bool { + if c.rootChange != nil { + return c.rootChange.Id == id + } + return false +}