Skip to content

Commit

Permalink
Remove Option pattern from Operation interface
Browse files Browse the repository at this point in the history
  • Loading branch information
chacha912 committed Dec 9, 2024
1 parent e784fe5 commit 40bf57f
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 54 deletions.
2 changes: 1 addition & 1 deletion pkg/document/change/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func New(id ID, message string, operations []operations.Operation, p *innerprese
// Execute applies this change to the given JSON root.
func (c *Change) Execute(root *crdt.Root, presences *innerpresence.Map) error {
for _, op := range c.operations {
if err := op.Execute(root, operations.WithVersionVector(c.ID().versionVector)); err != nil {
if err := op.Execute(root, c.ID().versionVector); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/document/operations/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewAdd(
}

// Execute executes this operation on the given document(`root`).
func (o *Add) Execute(root *crdt.Root, _ ...Option) error {
func (o *Add) Execute(root *crdt.Root, _ time.VersionVector) error {
parent := root.FindByCreatedAt(o.parentCreatedAt)

obj, ok := parent.(*crdt.Array)
Expand Down
3 changes: 1 addition & 2 deletions pkg/document/operations/array_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ func NewArraySet(
}

// Execute executes this operation on the given document(`root`).
func (o *ArraySet) Execute(root *crdt.Root, _ ...Option) error {
func (o *ArraySet) Execute(root *crdt.Root, _ time.VersionVector) error {
parent := root.FindByCreatedAt(o.parentCreatedAt)

obj, ok := parent.(*crdt.Array)
if !ok {
return ErrNotApplicableDataType
Expand Down
9 changes: 2 additions & 7 deletions pkg/document/operations/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,13 @@ func NewEdit(
}

// Execute executes this operation on the given document(`root`).
func (e *Edit) Execute(root *crdt.Root, opts ...Option) error {
options := &ExecuteOption{}
for _, opt := range opts {
opt(options)
}

func (e *Edit) Execute(root *crdt.Root, versionVector time.VersionVector) error {
parent := root.FindByCreatedAt(e.parentCreatedAt)

switch obj := parent.(type) {
case *crdt.Text:
_, _, pairs, err := obj.Edit(e.from, e.to, e.maxCreatedAtMapByActor,
e.content, e.attributes, e.executedAt, options.VersionVector)
e.content, e.attributes, e.executedAt, versionVector)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/document/operations/increase.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewIncrease(
}

// Execute executes this operation on the given document(`root`).
func (o *Increase) Execute(root *crdt.Root, _ ...Option) error {
func (o *Increase) Execute(root *crdt.Root, _ time.VersionVector) error {
parent := root.FindByCreatedAt(o.parentCreatedAt)
cnt, ok := parent.(*crdt.Counter)
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion pkg/document/operations/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewMove(
}

// Execute executes this operation on the given document(`root`).
func (o *Move) Execute(root *crdt.Root, _ ...Option) error {
func (o *Move) Execute(root *crdt.Root, _ time.VersionVector) error {
parent := root.FindByCreatedAt(o.parentCreatedAt)

obj, ok := parent.(*crdt.Array)
Expand Down
18 changes: 1 addition & 17 deletions pkg/document/operations/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,10 @@ var (
ErrNotApplicableDataType = errors.New("not applicable datatype")
)

// ExecuteOption contains options for executing operations.
type ExecuteOption struct {
// VersionVector represents the version vector at the time of operation creation.
VersionVector time.VersionVector
}

// Option is a function type that configures ExecuteOption.
type Option func(*ExecuteOption)

// WithVersionVector returns an Option that sets the version vector for the operation.
func WithVersionVector(versionVector time.VersionVector) Option {
return func(option *ExecuteOption) {
option.VersionVector = versionVector
}
}

// Operation represents an operation to be executed on a document.
type Operation interface {
// Execute executes this operation on the given document(`root`).
Execute(root *crdt.Root, opts ...Option) error
Execute(root *crdt.Root, versionVector time.VersionVector) error

// ExecutedAt returns execution time of this operation.
ExecutedAt() *time.Ticket
Expand Down
2 changes: 1 addition & 1 deletion pkg/document/operations/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewRemove(
}

// Execute executes this operation on the given document(`root`).
func (o *Remove) Execute(root *crdt.Root, _ ...Option) error {
func (o *Remove) Execute(root *crdt.Root, _ time.VersionVector) error {
parentElem := root.FindByCreatedAt(o.parentCreatedAt)

switch parent := parentElem.(type) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/document/operations/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewSet(
}

// Execute executes this operation on the given document(`root`).
func (o *Set) Execute(root *crdt.Root, _ ...Option) error {
func (o *Set) Execute(root *crdt.Root, _ time.VersionVector) error {
parent := root.FindByCreatedAt(o.parentCreatedAt)

obj, ok := parent.(*crdt.Object)
Expand Down
9 changes: 2 additions & 7 deletions pkg/document/operations/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,14 @@ func NewStyle(
}

// Execute executes this operation on the given document(`root`).
func (e *Style) Execute(root *crdt.Root, opts ...Option) error {
options := &ExecuteOption{}
for _, opt := range opts {
opt(options)
}

func (e *Style) Execute(root *crdt.Root, versionVector time.VersionVector) error {
parent := root.FindByCreatedAt(e.parentCreatedAt)
obj, ok := parent.(*crdt.Text)
if !ok {
return ErrNotApplicableDataType
}

_, pairs, err := obj.Style(e.from, e.to, e.maxCreatedAtMapByActor, e.attributes, e.executedAt, options.VersionVector)
_, pairs, err := obj.Style(e.from, e.to, e.maxCreatedAtMapByActor, e.attributes, e.executedAt, versionVector)
if err != nil {
return err
}
Expand Down
9 changes: 2 additions & 7 deletions pkg/document/operations/tree_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,7 @@ func NewTreeEdit(
}

// Execute executes this operation on the given `CRDTRoot`.
func (e *TreeEdit) Execute(root *crdt.Root, opts ...Option) error {
options := &ExecuteOption{}
for _, opt := range opts {
opt(options)
}

func (e *TreeEdit) Execute(root *crdt.Root, versionVector time.VersionVector) error {
parent := root.FindByCreatedAt(e.parentCreatedAt)

switch obj := parent.(type) {
Expand Down Expand Up @@ -122,7 +117,7 @@ func (e *TreeEdit) Execute(root *crdt.Root, opts ...Option) error {
}
}(),
e.maxCreatedAtMapByActor,
options.VersionVector,
versionVector,
)
if err != nil {
return err
Expand Down
11 changes: 3 additions & 8 deletions pkg/document/operations/tree_style.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,7 @@ func NewTreeStyleRemove(
}

// Execute executes this operation on the given `CRDTRoot`.
func (e *TreeStyle) Execute(root *crdt.Root, opts ...Option) error {
options := &ExecuteOption{}
for _, opt := range opts {
opt(options)
}

func (e *TreeStyle) Execute(root *crdt.Root, versionVector time.VersionVector) error {
parent := root.FindByCreatedAt(e.parentCreatedAt)
obj, ok := parent.(*crdt.Tree)
if !ok {
Expand All @@ -102,13 +97,13 @@ func (e *TreeStyle) Execute(root *crdt.Root, opts ...Option) error {
var pairs []crdt.GCPair
var err error
if len(e.attributes) > 0 {
_, pairs, err = obj.Style(e.from, e.to, e.attributes, e.executedAt, e.maxCreatedAtMapByActor, options.VersionVector)
_, pairs, err = obj.Style(e.from, e.to, e.attributes, e.executedAt, e.maxCreatedAtMapByActor, versionVector)
if err != nil {
return err
}
} else {
_, pairs, err = obj.RemoveStyle(e.from, e.to, e.attributesToRemove, e.executedAt,
e.maxCreatedAtMapByActor, options.VersionVector)
e.maxCreatedAtMapByActor, versionVector)
if err != nil {
return err
}
Expand Down

0 comments on commit 40bf57f

Please sign in to comment.