Merge pull request #767 from MichaelMure/single-author

bug: don't serialize multiple time the author, only once in OperationPack
This commit is contained in:
Michael Muré 2022-04-19 21:47:15 +02:00 committed by GitHub
commit b8a799a97d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 12 additions and 29 deletions

View File

@ -5,7 +5,6 @@ import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/MichaelMure/git-bug/identity" "github.com/MichaelMure/git-bug/identity"
@ -22,18 +21,17 @@ func TestAddCommentSerialize(t *testing.T) {
before := NewAddCommentOp(rene, unix, "message", nil) before := NewAddCommentOp(rene, unix, "message", nil)
data, err := json.Marshal(before) data, err := json.Marshal(before)
assert.NoError(t, err) require.NoError(t, err)
var after AddCommentOperation var after AddCommentOperation
err = json.Unmarshal(data, &after) err = json.Unmarshal(data, &after)
assert.NoError(t, err) require.NoError(t, err)
// enforce creating the ID // enforce creating the ID
before.Id() before.Id()
// Replace the identity stub with the real thing // Replace the identity as it's not serialized
assert.Equal(t, rene.Id(), after.Author().Id())
after.Author_ = rene after.Author_ = rene
assert.Equal(t, before, &after) require.Equal(t, before, &after)
} }

View File

@ -76,8 +76,7 @@ func TestCreateSerialize(t *testing.T) {
// enforce creating the ID // enforce creating the ID
before.Id() before.Id()
// Replace the identity stub with the real thing // Replace the identity as it's not serialized
require.Equal(t, rene.Id(), after.Author().Id())
after.Author_ = rene after.Author_ = rene
require.Equal(t, before, &after) require.Equal(t, before, &after)

View File

@ -93,8 +93,7 @@ func TestEditCommentSerialize(t *testing.T) {
// enforce creating the ID // enforce creating the ID
before.Id() before.Id()
// Replace the identity stub with the real thing // Replace the identity as it's not serialized
require.Equal(t, rene.Id(), after.Author().Id())
after.Author_ = rene after.Author_ = rene
require.Equal(t, before, &after) require.Equal(t, before, &after)

View File

@ -30,8 +30,7 @@ func TestLabelChangeSerialize(t *testing.T) {
// enforce creating the ID // enforce creating the ID
before.Id() before.Id()
// Replace the identity stub with the real thing // Replace the identity as it's not serialized
require.Equal(t, rene.Id(), after.Author().Id())
after.Author_ = rene after.Author_ = rene
require.Equal(t, before, &after) require.Equal(t, before, &after)

View File

@ -32,8 +32,7 @@ func TestNoopSerialize(t *testing.T) {
// enforce creating the ID // enforce creating the ID
before.Id() before.Id()
// Replace the identity stub with the real thing // Replace the identity as it's not serialized
assert.Equal(t, rene.Id(), after.Author().Id())
after.Author_ = rene after.Author_ = rene
assert.Equal(t, before, &after) assert.Equal(t, before, &after)

View File

@ -119,8 +119,7 @@ func TestSetMetadataSerialize(t *testing.T) {
// enforce creating the ID // enforce creating the ID
before.Id() before.Id()
// Replace the identity stub with the real thing // Replace the identity as it's not serialized
require.Equal(t, rene.Id(), after.Author().Id())
after.Author_ = rene after.Author_ = rene
require.Equal(t, before, &after) require.Equal(t, before, &after)

View File

@ -30,8 +30,7 @@ func TestSetStatusSerialize(t *testing.T) {
// enforce creating the ID // enforce creating the ID
before.Id() before.Id()
// Replace the identity stub with the real thing // Replace the identity as it's not serialized
require.Equal(t, rene.Id(), after.Author().Id())
after.Author_ = rene after.Author_ = rene
require.Equal(t, before, &after) require.Equal(t, before, &after)

View File

@ -30,8 +30,7 @@ func TestSetTitleSerialize(t *testing.T) {
// enforce creating the ID // enforce creating the ID
before.Id() before.Id()
// Replace the identity stub with the real thing // Replace the identity as it's not serialized
require.Equal(t, rene.Id(), after.Author().Id())
after.Author_ = rene after.Author_ = rene
require.Equal(t, before, &after) require.Equal(t, before, &after)

View File

@ -135,7 +135,7 @@ func operationUnmarshaller(author identity.Interface, raw json.RawMessage, resol
// OpBase implement the common code for all operations // OpBase implement the common code for all operations
type OpBase struct { type OpBase struct {
OperationType OperationType `json:"type"` OperationType OperationType `json:"type"`
Author_ identity.Interface `json:"author"` Author_ identity.Interface `json:"-"` // not serialized
// TODO: part of the data model upgrade, this should eventually be a timestamp + lamport // TODO: part of the data model upgrade, this should eventually be a timestamp + lamport
UnixTime int64 `json:"timestamp"` UnixTime int64 `json:"timestamp"`
Metadata map[string]string `json:"metadata,omitempty"` Metadata map[string]string `json:"metadata,omitempty"`
@ -178,7 +178,6 @@ func (base *OpBase) UnmarshalJSON(data []byte) error {
aux := struct { aux := struct {
OperationType OperationType `json:"type"` OperationType OperationType `json:"type"`
Author json.RawMessage `json:"author"`
UnixTime int64 `json:"timestamp"` UnixTime int64 `json:"timestamp"`
Metadata map[string]string `json:"metadata,omitempty"` Metadata map[string]string `json:"metadata,omitempty"`
Nonce []byte `json:"nonce"` Nonce []byte `json:"nonce"`
@ -188,14 +187,7 @@ func (base *OpBase) UnmarshalJSON(data []byte) error {
return err return err
} }
// delegate the decoding of the identity
author, err := identity.UnmarshalJSON(aux.Author)
if err != nil {
return err
}
base.OperationType = aux.OperationType base.OperationType = aux.OperationType
base.Author_ = author
base.UnixTime = aux.UnixTime base.UnixTime = aux.UnixTime
base.Metadata = aux.Metadata base.Metadata = aux.Metadata
base.Nonce = aux.Nonce base.Nonce = aux.Nonce