diff options
author | Michael Muré <batolettre@gmail.com> | 2022-08-01 17:16:45 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-01 17:16:45 +0200 |
commit | cd52872475f1b39f3fb6546606c1e78afb6c08e3 (patch) | |
tree | 9f04b1e1ee362e210e74aaa324d950058ed14bf2 /bug/op_set_title.go | |
parent | 2ade8fb1d570ddcb4aedc9386af46d208b129daa (diff) | |
parent | 56966fec5562c3a0e23340d0fbe754626c3beb64 (diff) | |
download | git-bug-cd52872475f1b39f3fb6546606c1e78afb6c08e3.tar.gz |
Merge pull request #835 from MichaelMure/op-base
entity/dag: proper base operation for simplified implementation
Diffstat (limited to 'bug/op_set_title.go')
-rw-r--r-- | bug/op_set_title.go | 64 |
1 files changed, 17 insertions, 47 deletions
diff --git a/bug/op_set_title.go b/bug/op_set_title.go index badd192c..d26a60fa 100644 --- a/bug/op_set_title.go +++ b/bug/op_set_title.go @@ -1,10 +1,10 @@ package bug import ( - "encoding/json" "fmt" "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" @@ -15,22 +15,22 @@ var _ Operation = &SetTitleOperation{} // SetTitleOperation will change the title of a bug type SetTitleOperation struct { - OpBase + dag.OpBase Title string `json:"title"` Was string `json:"was"` } func (op *SetTitleOperation) Id() entity.Id { - return idOperation(op, &op.OpBase) + return dag.IdOperation(op, &op.OpBase) } func (op *SetTitleOperation) Apply(snapshot *Snapshot) { snapshot.Title = op.Title - snapshot.addActor(op.Author_) + snapshot.addActor(op.Author()) item := &SetTitleTimelineItem{ id: op.Id(), - Author: op.Author_, + Author: op.Author(), UnixTime: timestamp.Timestamp(op.UnixTime), Title: op.Title, Was: op.Was, @@ -59,41 +59,9 @@ func (op *SetTitleOperation) 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 *SetTitleOperation) UnmarshalJSON(data []byte) error { - // Unmarshal OpBase and the op separately - - base := OpBase{} - err := json.Unmarshal(data, &base) - if err != nil { - return err - } - - aux := struct { - Title string `json:"title"` - Was string `json:"was"` - }{} - - err = json.Unmarshal(data, &aux) - if err != nil { - return err - } - - op.OpBase = base - op.Title = aux.Title - op.Was = aux.Was - - return nil -} - -// Sign post method for gqlgen -func (op *SetTitleOperation) IsAuthored() {} - func NewSetTitleOp(author identity.Interface, unixTime int64, title string, was string) *SetTitleOperation { return &SetTitleOperation{ - OpBase: newOpBase(SetTitleOp, author, unixTime), + OpBase: dag.NewOpBase(SetTitleOp, author, unixTime), Title: title, Was: was, } @@ -111,11 +79,11 @@ func (s SetTitleTimelineItem) Id() entity.Id { return s.id } -// Sign post method for gqlgen -func (s *SetTitleTimelineItem) IsAuthored() {} +// IsAuthored is a sign post method for gqlgen +func (s SetTitleTimelineItem) IsAuthored() {} -// Convenience function to apply the operation -func SetTitle(b Interface, author identity.Interface, unixTime int64, title string) (*SetTitleOperation, error) { +// SetTitle is a convenience function to change a bugs title +func SetTitle(b Interface, author identity.Interface, unixTime int64, title string, metadata map[string]string) (*SetTitleOperation, error) { var lastTitleOp *SetTitleOperation for _, op := range b.Operations() { switch op := op.(type) { @@ -131,12 +99,14 @@ func SetTitle(b Interface, author identity.Interface, unixTime int64, title stri was = b.FirstOp().(*CreateOperation).Title } - setTitleOp := NewSetTitleOp(author, unixTime, title, was) - - if err := setTitleOp.Validate(); err != nil { + op := NewSetTitleOp(author, unixTime, title, was) + for key, value := range metadata { + op.SetMetadata(key, value) + } + if err := op.Validate(); err != nil { return nil, err } - b.Append(setTitleOp) - return setTitleOp, nil + b.Append(op) + return op, nil } |