diff --git a/lib/ur/contract/contract.go b/lib/ur/contract/contract.go index a2eb6454e..85863ba00 100644 --- a/lib/ur/contract/contract.go +++ b/lib/ur/contract/contract.go @@ -379,6 +379,10 @@ func (r Report) Value() (driver.Value, error) { } func (r *Report) Scan(value interface{}) error { + // Zero out the previous value + // JSON un-marshaller does not touch fields that are not in the payload, so we carry over values from a previous + // scan. + *r = Report{} b, ok := value.([]byte) if !ok { return errors.New("type assertion to []byte failed") diff --git a/lib/ur/contract/contract_test.go b/lib/ur/contract/contract_test.go index 0f676af87..7cf9f2ce3 100644 --- a/lib/ur/contract/contract_test.go +++ b/lib/ur/contract/contract_test.go @@ -128,3 +128,27 @@ func expect(t *testing.T, since int, b interface{}) { t.Errorf("%#v != %#v", x, b) } } + +func TestMarshallingBehaviour(t *testing.T) { + r := Report{} + + if err := r.Scan([]byte(`{"folderUses":{"sendonly": 100}}`)); err != nil { + t.Fatal(err) + } + + if r.FolderUses.SendOnly != 100 { + t.Errorf("%d != 100", r.FolderUses.SendOnly) + } + + if err := r.Scan([]byte(`{"folderUses":{"sendreceive": 200}}`)); err != nil { + t.Fatal(err) + } + + if r.FolderUses.SendReceive != 200 { + t.Errorf("%d != 200", r.FolderUses.SendReceive) + } + + if r.FolderUses.SendOnly != 0 { + t.Errorf("%d != 0", r.FolderUses.SendOnly) + } +}