From 395b6eb1c2de3e229d31d46f8d2afdb3f1d59fab Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michael=20Mur=C3=A9?= <batolettre@gmail.com>
Date: Fri, 11 Aug 2023 14:50:45 +0200
Subject: [PATCH] WIP

---
 entities/bug/bug.go                |  4 ++--
 entities/bug/op_add_comment.go     |  1 +
 entities/bug/snapshot.go           |  8 ++++----
 entity/dag/op_noop.go              |  8 ++++----
 entity/dag/op_set_metadata.go      |  8 ++++----
 entity/dag/op_set_metadata_test.go |  8 ++++----
 entity/dag/operation.go            |  4 +++-
 entity/entity.go                   |  8 ++++----
 entity/operations.go               |  2 +-
 entity/snapshot.go                 | 11 +++++++----
 10 files changed, 34 insertions(+), 28 deletions(-)

diff --git a/entities/bug/bug.go b/entities/bug/bug.go
index ab873ed8..6c440433 100644
--- a/entities/bug/bug.go
+++ b/entities/bug/bug.go
@@ -13,7 +13,7 @@ import (
 )
 
 var _ Interface = &Bug{}
-var _ entity.Interface[*Snapshot, Operation] = &Bug{}
+var _ entity.Interface[Operation, *Snapshot] = &Bug{}
 
 // 1: original format
 // 2: no more legacy identities
@@ -34,7 +34,7 @@ var def = dag.Definition{
 var ClockLoader = dag.ClockLoader(def)
 
 type Interface interface {
-	entity.Interface[*Snapshot, Operation]
+	entity.Interface[Operation, *Snapshot]
 }
 
 // Bug holds the data of a bug thread, organized in a way close to
diff --git a/entities/bug/op_add_comment.go b/entities/bug/op_add_comment.go
index 17cc5dd0..267fe731 100644
--- a/entities/bug/op_add_comment.go
+++ b/entities/bug/op_add_comment.go
@@ -12,6 +12,7 @@ import (
 )
 
 var _ Operation = &AddCommentOperation{}
+var _ dag.Operation = &AddCommentOperation{}
 var _ entity.OperationWithFiles = &AddCommentOperation{}
 
 // AddCommentOperation will add a new comment in the bug
diff --git a/entities/bug/snapshot.go b/entities/bug/snapshot.go
index 9dbc7862..32baf42c 100644
--- a/entities/bug/snapshot.go
+++ b/entities/bug/snapshot.go
@@ -9,7 +9,7 @@ import (
 	"github.com/MichaelMure/git-bug/entity"
 )
 
-var _ entity.Snapshot = &Snapshot{}
+// var _ entity.Snapshot = &Snapshot{}
 
 // Snapshot is a compiled form of the Bug data structure used for storage and merge
 type Snapshot struct {
@@ -26,7 +26,7 @@ type Snapshot struct {
 
 	Timeline []TimelineItem
 
-	Operations []entity.Operation
+	Operations []Operation
 }
 
 // Id returns the Bug identifier
@@ -38,11 +38,11 @@ func (snap *Snapshot) Id() entity.Id {
 	return snap.id
 }
 
-func (snap *Snapshot) AllOperations() []entity.Operation {
+func (snap *Snapshot) AllOperations() []Operation {
 	return snap.Operations
 }
 
-func (snap *Snapshot) AppendOperation(op entity.Operation) {
+func (snap *Snapshot) AppendOperation(op Operation) {
 	snap.Operations = append(snap.Operations, op)
 }
 
diff --git a/entity/dag/op_noop.go b/entity/dag/op_noop.go
index d8a2a05a..e94b2edf 100644
--- a/entity/dag/op_noop.go
+++ b/entity/dag/op_noop.go
@@ -5,17 +5,17 @@ import (
 	"github.com/MichaelMure/git-bug/entity"
 )
 
-var _ Operation = &NoOpOperation[entity.Snapshot]{}
-var _ entity.OperationDoesntChangeSnapshot = &NoOpOperation[entity.Snapshot]{}
+var _ Operation = &NoOpOperation[Snapshot]{}
+var _ entity.OperationDoesntChangeSnapshot = &NoOpOperation[Snapshot]{}
 
 // NoOpOperation is an operation that does not change the entity state. It can
 // however be used to store arbitrary metadata in the entity history, for example
 // to support a bridge feature.
-type NoOpOperation[SnapT entity.Snapshot] struct {
+type NoOpOperation[SnapT Snapshot] struct {
 	OpBase
 }
 
-func NewNoOpOp[SnapT entity.Snapshot](opType entity.OperationType, author identity.Interface, unixTime int64) *NoOpOperation[SnapT] {
+func NewNoOpOp[SnapT Snapshot](opType entity.OperationType, author identity.Interface, unixTime int64) *NoOpOperation[SnapT] {
 	return &NoOpOperation[SnapT]{
 		OpBase: NewOpBase(opType, author, unixTime),
 	}
diff --git a/entity/dag/op_set_metadata.go b/entity/dag/op_set_metadata.go
index 19176183..42749fda 100644
--- a/entity/dag/op_set_metadata.go
+++ b/entity/dag/op_set_metadata.go
@@ -10,16 +10,16 @@ import (
 	"github.com/MichaelMure/git-bug/util/text"
 )
 
-var _ Operation = &SetMetadataOperation[entity.Snapshot]{}
-var _ entity.OperationDoesntChangeSnapshot = &SetMetadataOperation[entity.Snapshot]{}
+var _ Operation = &SetMetadataOperation[Snapshot]{}
+var _ entity.OperationDoesntChangeSnapshot = &SetMetadataOperation[Snapshot]{}
 
-type SetMetadataOperation[SnapT entity.Snapshot] struct {
+type SetMetadataOperation[SnapT Snapshot] struct {
 	OpBase
 	Target      entity.Id         `json:"target"`
 	NewMetadata map[string]string `json:"new_metadata"`
 }
 
-func NewSetMetadataOp[SnapT entity.Snapshot](opType entity.OperationType, author identity.Interface, unixTime int64, target entity.Id, newMetadata map[string]string) *SetMetadataOperation[SnapT] {
+func NewSetMetadataOp[SnapT Snapshot](opType entity.OperationType, author identity.Interface, unixTime int64, target entity.Id, newMetadata map[string]string) *SetMetadataOperation[SnapT] {
 	return &SetMetadataOperation[SnapT]{
 		OpBase:      NewOpBase(opType, author, unixTime),
 		Target:      target,
diff --git a/entity/dag/op_set_metadata_test.go b/entity/dag/op_set_metadata_test.go
index 591ce9b2..a06f89da 100644
--- a/entity/dag/op_set_metadata_test.go
+++ b/entity/dag/op_set_metadata_test.go
@@ -12,17 +12,17 @@ import (
 	"github.com/stretchr/testify/require"
 )
 
-var _ entity.Snapshot = &snapshotMock{}
+var _ Snapshot = &snapshotMock{}
 
 type snapshotMock struct {
-	ops []entity.Operation
+	ops []Operation
 }
 
-func (s *snapshotMock) AllOperations() []entity.Operation {
+func (s *snapshotMock) AllOperations() []Operation {
 	return s.ops
 }
 
-func (s *snapshotMock) AppendOperation(op entity.Operation) {
+func (s *snapshotMock) AppendOperation(op Operation) {
 	s.ops = append(s.ops, op)
 }
 
diff --git a/entity/dag/operation.go b/entity/dag/operation.go
index 40bd7da8..5099ad80 100644
--- a/entity/dag/operation.go
+++ b/entity/dag/operation.go
@@ -12,6 +12,8 @@ import (
 	"github.com/MichaelMure/git-bug/entity"
 )
 
+type Snapshot entity.SnapshotT[Operation]
+
 // Operation is an extended interface for an entity.Operation working with the dag package.
 type Operation interface {
 	entity.Operation
@@ -24,7 +26,7 @@ type Operation interface {
 	setExtraMetadataImmutable(key string, value string)
 }
 
-type OperationWithApply[SnapT entity.Snapshot] interface {
+type OperationWithApply[SnapT Snapshot] interface {
 	Operation
 
 	// Apply the operation to a Snapshot to create the final state
diff --git a/entity/entity.go b/entity/entity.go
index 9f523858..eb20e65b 100644
--- a/entity/entity.go
+++ b/entity/entity.go
@@ -9,9 +9,9 @@ import (
 type Bare bootstrap.Entity
 
 // Interface define the extended interface of an Entity
-type Interface[SnapT Snapshot, OpT Operation] interface {
+type Interface[OpT Operation, SnapT Snapshot] interface {
 	Bare
-	CompileToSnapshot[SnapT]
+	CompileToSnapshot[OpT, SnapT]
 
 	// Validate checks if the Entity data is valid
 	Validate() error
@@ -36,8 +36,8 @@ type Interface[SnapT Snapshot, OpT Operation] interface {
 	EditLamportTime() lamport.Time
 }
 
-type WithCommit[SnapT Snapshot, OpT Operation] interface {
-	Interface[SnapT, OpT]
+type WithCommit[OpT Operation, SnapT Snapshot] interface {
+	Interface[OpT, SnapT]
 	Committer
 }
 
diff --git a/entity/operations.go b/entity/operations.go
index fd88f033..76c42ce8 100644
--- a/entity/operations.go
+++ b/entity/operations.go
@@ -50,7 +50,7 @@ type Operation interface {
 	AllMetadata() map[string]string
 }
 
-type OperationWithApply[SnapT Snapshot] interface {
+type OperationWithApply[OpT Operation, SnapT Snapshot] interface {
 	Operation
 
 	// Apply the operation to a Snapshot to create the final state
diff --git a/entity/snapshot.go b/entity/snapshot.go
index c6ea43fc..9dd109cf 100644
--- a/entity/snapshot.go
+++ b/entity/snapshot.go
@@ -1,14 +1,17 @@
 package entity
 
 // Snapshot is the minimal interface that a snapshot need to implement
-type Snapshot interface {
+type Snapshot SnapshotT[Operation]
+
+// SnapshotT is the minimal interface that a snapshot need to implement
+type SnapshotT[OpT Operation] interface {
 	// AllOperations returns all the operations that have been applied to that snapshot, in order
-	AllOperations() []Operation
+	AllOperations() []OpT
 	// AppendOperation add an operation in the list
-	AppendOperation(op Operation)
+	AppendOperation(op OpT)
 }
 
-type CompileToSnapshot[SnapT Snapshot] interface {
+type CompileToSnapshot[OpT Operation, SnapT Snapshot] interface {
 	// Compile an Entity in an easily usable snapshot
 	Compile() SnapT
 }
-- 
GitLab