1package bug
2
3import (
4 "encoding/json"
5 "fmt"
6
7 "github.com/MichaelMure/git-bug/entity"
8 "github.com/MichaelMure/git-bug/entity/dag"
9)
10
11const (
12 _ dag.OperationType = iota
13 CreateOp
14 SetTitleOp
15 AddCommentOp
16 SetStatusOp
17 LabelChangeOp
18 EditCommentOp
19 NoOpOp
20 SetMetadataOp
21)
22
23// Operation define the interface to fulfill for an edit operation of a Bug
24type Operation interface {
25 dag.Operation
26
27 // Apply the operation to a Snapshot to create the final state
28 Apply(snapshot *Snapshot)
29}
30
31// make sure that package external operations do conform to our interface
32var _ Operation = &dag.NoOpOperation[*Snapshot]{}
33var _ Operation = &dag.SetMetadataOperation[*Snapshot]{}
34
35func operationUnmarshaller(raw json.RawMessage, resolvers entity.Resolvers) (dag.Operation, error) {
36 var t struct {
37 OperationType dag.OperationType `json:"type"`
38 }
39
40 if err := json.Unmarshal(raw, &t); err != nil {
41 return nil, err
42 }
43
44 var op dag.Operation
45
46 switch t.OperationType {
47 case AddCommentOp:
48 op = &AddCommentOperation{}
49 case CreateOp:
50 op = &CreateOperation{}
51 case EditCommentOp:
52 op = &EditCommentOperation{}
53 case LabelChangeOp:
54 op = &LabelChangeOperation{}
55 case NoOpOp:
56 op = &dag.NoOpOperation[*Snapshot]{}
57 case SetMetadataOp:
58 op = &dag.SetMetadataOperation[*Snapshot]{}
59 case SetStatusOp:
60 op = &SetStatusOperation{}
61 case SetTitleOp:
62 op = &SetTitleOperation{}
63 default:
64 panic(fmt.Sprintf("unknown operation type %v", t.OperationType))
65 }
66
67 err := json.Unmarshal(raw, &op)
68 if err != nil {
69 return nil, err
70 }
71
72 return op, nil
73}