Add context to acl tree
This commit is contained in:
parent
883e51c84d
commit
49a9edfe6c
@ -1,6 +1,7 @@
|
|||||||
package acltree
|
package acltree
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/account"
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/account"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treestorage"
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treestorage"
|
||||||
"sync"
|
"sync"
|
||||||
@ -28,8 +29,8 @@ type TreeUpdateListener interface {
|
|||||||
|
|
||||||
type ACLTree interface {
|
type ACLTree interface {
|
||||||
ACLState() *ACLState
|
ACLState() *ACLState
|
||||||
AddContent(f func(builder ChangeBuilder) error) (*Change, error)
|
AddContent(ctx context.Context, f func(builder ChangeBuilder) error) (*Change, error)
|
||||||
AddChanges(changes ...*Change) (AddResult, error)
|
AddChanges(ctx context.Context, changes ...*Change) (AddResult, error)
|
||||||
Heads() []string
|
Heads() []string
|
||||||
Root() *Change
|
Root() *Change
|
||||||
Iterate(func(change *Change) bool)
|
Iterate(func(change *Change) bool)
|
||||||
@ -197,7 +198,7 @@ func (a *aclTree) ACLState() *ACLState {
|
|||||||
return a.aclState
|
return a.aclState
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *aclTree) AddContent(build func(builder ChangeBuilder) error) (*Change, error) {
|
func (a *aclTree) AddContent(ctx context.Context, build func(builder ChangeBuilder) error) (*Change, error) {
|
||||||
// TODO: add snapshot creation logic
|
// TODO: add snapshot creation logic
|
||||||
a.Lock()
|
a.Lock()
|
||||||
defer func() {
|
defer func() {
|
||||||
@ -234,7 +235,7 @@ func (a *aclTree) AddContent(build func(builder ChangeBuilder) error) (*Change,
|
|||||||
return ch, nil
|
return ch, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *aclTree) AddChanges(changes ...*Change) (AddResult, error) {
|
func (a *aclTree) AddChanges(ctx context.Context, changes ...*Change) (AddResult, error) {
|
||||||
a.Lock()
|
a.Lock()
|
||||||
// TODO: make proper error handling, because there are a lot of corner cases where this will break
|
// TODO: make proper error handling, because there are a lot of corner cases where this will break
|
||||||
var err error
|
var err error
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package acltree
|
package acltree
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/account"
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/account"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclchanges/pb"
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclchanges/pb"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/testutils/treestoragebuilder"
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/testutils/treestoragebuilder"
|
||||||
@ -78,7 +79,7 @@ func TestACLTree_UserJoinUpdate_Append(t *testing.T) {
|
|||||||
changes = append(changes, newCh)
|
changes = append(changes, newCh)
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := tree.AddChanges(changes...)
|
res, err := tree.AddChanges(context.Background(), changes...)
|
||||||
assert.Equal(t, res.Summary, AddResultSummaryAppend)
|
assert.Equal(t, res.Summary, AddResultSummaryAppend)
|
||||||
|
|
||||||
aclState := tree.ACLState()
|
aclState := tree.ACLState()
|
||||||
@ -128,7 +129,7 @@ func TestACLTree_UserJoinUpdate_Rebuild(t *testing.T) {
|
|||||||
changes = append(changes, newCh)
|
changes = append(changes, newCh)
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := tree.AddChanges(changes...)
|
res, err := tree.AddChanges(context.Background(), changes...)
|
||||||
assert.Equal(t, res.Summary, AddResultSummaryRebuild)
|
assert.Equal(t, res.Summary, AddResultSummaryRebuild)
|
||||||
|
|
||||||
aclState := tree.ACLState()
|
aclState := tree.ACLState()
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package plaintextdocument
|
package plaintextdocument
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/account"
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/account"
|
||||||
aclpb "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclchanges/pb"
|
aclpb "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclchanges/pb"
|
||||||
@ -13,7 +14,7 @@ import (
|
|||||||
|
|
||||||
type PlainTextDocument interface {
|
type PlainTextDocument interface {
|
||||||
Text() string
|
Text() string
|
||||||
AddText(text string) error
|
AddText(ctx context.Context, text string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type plainTextDocument struct {
|
type plainTextDocument struct {
|
||||||
@ -29,8 +30,8 @@ func (p *plainTextDocument) Text() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *plainTextDocument) AddText(text string) error {
|
func (p *plainTextDocument) AddText(ctx context.Context, text string) error {
|
||||||
_, err := p.aclTree.AddContent(func(builder acltree.ChangeBuilder) error {
|
_, err := p.aclTree.AddContent(ctx, func(builder acltree.ChangeBuilder) error {
|
||||||
builder.AddChangeContent(
|
builder.AddChangeContent(
|
||||||
&pb.PlainTextChangeData{
|
&pb.PlainTextChangeData{
|
||||||
Content: []*pb.PlainTextChangeContent{
|
Content: []*pb.PlainTextChangeContent{
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package plaintextdocument
|
package plaintextdocument
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/account"
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/account"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/testutils/treestoragebuilder"
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/testutils/treestoragebuilder"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treestorage"
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treestorage"
|
||||||
@ -43,13 +44,13 @@ func TestDocument_PlainTextDocument_AddText(t *testing.T) {
|
|||||||
t.Fatalf("should not create document with error: %v", err)
|
t.Fatalf("should not create document with error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = doc.AddText("Next")
|
err = doc.AddText(context.Background(), "Next")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("should be able to add document: %v", err)
|
t.Fatalf("should be able to add document: %v", err)
|
||||||
}
|
}
|
||||||
assert.Equal(t, doc.Text(), "Some text|Next")
|
assert.Equal(t, doc.Text(), "Some text|Next")
|
||||||
|
|
||||||
err = doc.AddText("Shmext")
|
err = doc.AddText(context.Background(), "Shmext")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("should be able to add document: %v", err)
|
t.Fatalf("should be able to add document: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user