diff options
author | Michael Muré <batolettre@gmail.com> | 2022-07-25 13:16:16 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2022-07-25 13:27:17 +0200 |
commit | 3d454d9dc8ba2409046c0938618a70864e6eb8ef (patch) | |
tree | 8745f656cc8218654632ce003f997a39988d3043 /bug/op_label_change.go | |
parent | 2ade8fb1d570ddcb4aedc9386af46d208b129daa (diff) | |
download | git-bug-3d454d9dc8ba2409046c0938618a70864e6eb8ef.tar.gz |
entity/dag: proper base operation for simplified implementation
- reduce boilerplace necessary to implement an operation
- consolidate what an operation is in the core, which in turn pave the way for a generic cache layer mechanism
- avoid the previously complex unmarshalling process
- support operation metadata from the core
- simplified testing
Diffstat (limited to 'bug/op_label_change.go')
-rw-r--r-- | bug/op_label_change.go | 46 |
1 files changed, 7 insertions, 39 deletions
diff --git a/bug/op_label_change.go b/bug/op_label_change.go index 8b0e5ec8..77caa7dd 100644 --- a/bug/op_label_change.go +++ b/bug/op_label_change.go @@ -1,13 +1,13 @@ package bug import ( - "encoding/json" "fmt" "sort" "github.com/pkg/errors" "github.com/MichaelMure/git-bug/entity" + "github.com/MichaelMure/git-bug/entity/dag" "github.com/MichaelMure/git-bug/identity" "github.com/MichaelMure/git-bug/util/timestamp" ) @@ -16,18 +16,18 @@ var _ Operation = &LabelChangeOperation{} // LabelChangeOperation define a Bug operation to add or remove labels type LabelChangeOperation struct { - OpBase + dag.OpBase Added []Label `json:"added"` Removed []Label `json:"removed"` } func (op *LabelChangeOperation) Id() entity.Id { - return idOperation(op, &op.OpBase) + return dag.IdOperation(op, &op.OpBase) } // Apply apply the operation func (op *LabelChangeOperation) Apply(snapshot *Snapshot) { - snapshot.addActor(op.Author_) + snapshot.addActor(op.Author()) // Add in the set AddLoop: @@ -59,7 +59,7 @@ AddLoop: item := &LabelChangeTimelineItem{ id: op.Id(), - Author: op.Author_, + Author: op.Author(), UnixTime: timestamp.Timestamp(op.UnixTime), Added: op.Added, Removed: op.Removed, @@ -92,41 +92,9 @@ func (op *LabelChangeOperation) Validate() error { return nil } -// UnmarshalJSON is a two step JSON unmarshalling -// This workaround is necessary to avoid the inner OpBase.MarshalJSON -// overriding the outer op's MarshalJSON -func (op *LabelChangeOperation) UnmarshalJSON(data []byte) error { - // Unmarshal OpBase and the op separately - - base := OpBase{} - err := json.Unmarshal(data, &base) - if err != nil { - return err - } - - aux := struct { - Added []Label `json:"added"` - Removed []Label `json:"removed"` - }{} - - err = json.Unmarshal(data, &aux) - if err != nil { - return err - } - - op.OpBase = base - op.Added = aux.Added - op.Removed = aux.Removed - - return nil -} - -// Sign post method for gqlgen -func (op *LabelChangeOperation) IsAuthored() {} - func NewLabelChangeOperation(author identity.Interface, unixTime int64, added, removed []Label) *LabelChangeOperation { return &LabelChangeOperation{ - OpBase: newOpBase(LabelChangeOp, author, unixTime), + OpBase: dag.NewOpBase(LabelChangeOp, author, unixTime), Added: added, Removed: removed, } @@ -144,7 +112,7 @@ func (l LabelChangeTimelineItem) Id() entity.Id { return l.id } -// Sign post method for gqlgen +// IsAuthored is a sign post method for gqlgen func (l *LabelChangeTimelineItem) IsAuthored() {} // ChangeLabels is a convenience function to apply the operation |