Fix change builder with no root

This commit is contained in:
mcrakhman 2023-03-02 21:08:14 +01:00
parent 6e5d044251
commit 5218eea8ad
No known key found for this signature in database
GPG Key ID: DED12CFEF5B8396B

View File

@ -220,7 +220,7 @@ func (c *changeBuilder) Build(payload BuilderContent) (ch *Change, rawIdChange *
} }
func (c *changeBuilder) Marshall(ch *Change) (raw *treechangeproto.RawTreeChangeWithId, err error) { 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 return c.rootChange, nil
} }
treeChange := &treechangeproto.TreeChange{ 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) { 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{} unmarshalled := &treechangeproto.RootChange{}
err = proto.Unmarshal(raw.Payload, unmarshalled) err = proto.Unmarshal(raw.Payload, unmarshalled)
if err != nil { if err != nil {
@ -274,3 +274,10 @@ func (c *changeBuilder) unmarshallRawChange(raw *treechangeproto.RawTreeChange,
ch = NewChange(id, unmarshalled, raw.Signature) ch = NewChange(id, unmarshalled, raw.Signature)
return return
} }
func (c *changeBuilder) isRoot(id string) bool {
if c.rootChange != nil {
return c.rootChange.Id == id
}
return false
}