aboutsummaryrefslogtreecommitdiffstats
path: root/entities/bug/operation.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2022-08-20 10:14:09 +0200
committerGitHub <noreply@github.com>2022-08-20 10:14:09 +0200
commit58df94d38d754bff4dcca11e2ae4b99326a9a87e (patch)
tree8701efc87732439f993eb4f1d00585fc419b87ab /entities/bug/operation.go
parent5ca686b59751e3c87740b84108c54fc675a074cf (diff)
parent5511c230b678a181cc596238bf6669428d1b1902 (diff)
downloadgit-bug-58df94d38d754bff4dcca11e2ae4b99326a9a87e.tar.gz
Merge pull request #852 from MichaelMure/move-around
move {bug,identity} to /entities, move input to /commands
Diffstat (limited to 'entities/bug/operation.go')
-rw-r--r--entities/bug/operation.go73
1 files changed, 73 insertions, 0 deletions
diff --git a/entities/bug/operation.go b/entities/bug/operation.go
new file mode 100644
index 00000000..a02fc780
--- /dev/null
+++ b/entities/bug/operation.go
@@ -0,0 +1,73 @@
+package bug
+
+import (
+ "encoding/json"
+ "fmt"
+
+ "github.com/MichaelMure/git-bug/entity"
+ "github.com/MichaelMure/git-bug/entity/dag"
+)
+
+const (
+ _ dag.OperationType = iota
+ CreateOp
+ SetTitleOp
+ AddCommentOp
+ SetStatusOp
+ LabelChangeOp
+ EditCommentOp
+ NoOpOp
+ SetMetadataOp
+)
+
+// Operation define the interface to fulfill for an edit operation of a Bug
+type Operation interface {
+ dag.Operation
+
+ // Apply the operation to a Snapshot to create the final state
+ Apply(snapshot *Snapshot)
+}
+
+// make sure that package external operations do conform to our interface
+var _ Operation = &dag.NoOpOperation[*Snapshot]{}
+var _ Operation = &dag.SetMetadataOperation[*Snapshot]{}
+
+func operationUnmarshaller(raw json.RawMessage, resolvers entity.Resolvers) (dag.Operation, error) {
+ var t struct {
+ OperationType dag.OperationType `json:"type"`
+ }
+
+ if err := json.Unmarshal(raw, &t); err != nil {
+ return nil, err
+ }
+
+ var op dag.Operation
+
+ switch t.OperationType {
+ case AddCommentOp:
+ op = &AddCommentOperation{}
+ case CreateOp:
+ op = &CreateOperation{}
+ case EditCommentOp:
+ op = &EditCommentOperation{}
+ case LabelChangeOp:
+ op = &LabelChangeOperation{}
+ case NoOpOp:
+ op = &dag.NoOpOperation[*Snapshot]{}
+ case SetMetadataOp:
+ op = &dag.SetMetadataOperation[*Snapshot]{}
+ case SetStatusOp:
+ op = &SetStatusOperation{}
+ case SetTitleOp:
+ op = &SetTitleOperation{}
+ default:
+ panic(fmt.Sprintf("unknown operation type %v", t.OperationType))
+ }
+
+ err := json.Unmarshal(raw, &op)
+ if err != nil {
+ return nil, err
+ }
+
+ return op, nil
+}