Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): workspace policy was not loaded from db correctly #721

Merged
merged 3 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"mode": "auto",
"cwd": "${workspaceRoot}/server",
"program": "${workspaceRoot}/server/cmd/reearth",
"envFile": "${workspaceRoot}/server/.env",
"env": {},
"args": []
}
Expand Down
2 changes: 1 addition & 1 deletion server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
github.com/paulmach/go.geojson v1.4.0
github.com/pkg/errors v0.9.1
github.com/ravilushqa/otelgqlgen v0.8.0
github.com/reearth/reearthx v0.0.0-20230825072907-543ac159f33b
github.com/reearth/reearthx v0.0.0-20231004095401-bceec5158e2a
github.com/samber/lo v1.27.0
github.com/spf13/afero v1.9.3
github.com/square/mongo-lock v0.0.0-20201208161834-4db518ed7fb2
Expand Down
6 changes: 6 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,12 @@ github.com/ravilushqa/otelgqlgen v0.8.0 h1:x48k+D1GMgm87xhMO2Lekrr9YGzFbpG3yijn9
github.com/ravilushqa/otelgqlgen v0.8.0/go.mod h1:6JO5YO2iY4POC7R6yB/L/RKXCcyISL8qQt5NnHOhh0o=
github.com/reearth/reearthx v0.0.0-20230825072907-543ac159f33b h1:7IsqB7KdGzp0hwlOIGb2imQW8mWJ9Ua9Ad5pAGNYaRs=
github.com/reearth/reearthx v0.0.0-20230825072907-543ac159f33b/go.mod h1:b8EygPZ9VcMv9vTbnl/oz3PU/wHz3wpBa4rc7W7URjw=
github.com/reearth/reearthx v0.0.0-20231004085546-58d2b9a580d3 h1:OWXYkQ7IafOuhLAB6h+M/lUmHoYfSWrPaExkq7PxT5I=
github.com/reearth/reearthx v0.0.0-20231004085546-58d2b9a580d3/go.mod h1:b8EygPZ9VcMv9vTbnl/oz3PU/wHz3wpBa4rc7W7URjw=
github.com/reearth/reearthx v0.0.0-20231004090951-7e7cbfdb49ba h1:O+R6Pjm5gwPSjedbygWBwjtN7BW4bKw0n5NX+8Szxpg=
github.com/reearth/reearthx v0.0.0-20231004090951-7e7cbfdb49ba/go.mod h1:b8EygPZ9VcMv9vTbnl/oz3PU/wHz3wpBa4rc7W7URjw=
github.com/reearth/reearthx v0.0.0-20231004095401-bceec5158e2a h1:/aUjXiRKu+YlbhOmiNnCKgtjJqvomQsMg2PNDX655Wc=
github.com/reearth/reearthx v0.0.0-20231004095401-bceec5158e2a/go.mod h1:b8EygPZ9VcMv9vTbnl/oz3PU/wHz3wpBa4rc7W7URjw=
github.com/robertkrimen/godocdown v0.0.0-20130622164427-0bfa04905481/go.mod h1:C9WhFzY47SzYBIvzFqSvHIR6ROgDo4TtdTuRaOMjF/s=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand Down
3 changes: 2 additions & 1 deletion server/internal/adapter/gql/gqlmodel/workspace.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gqlmodel

import (
"github.com/reearth/reearth/server/pkg/policy"
"github.com/reearth/reearthx/account/accountdomain/workspace"
)

Expand Down Expand Up @@ -51,7 +52,7 @@ func FromRole(r Role) workspace.Role {
return workspace.Role("")
}

func ToPolicy(p *workspace.Policy) *Policy {
func ToPolicy(p *policy.Policy) *Policy {
if p == nil {
return nil
}
Expand Down
5 changes: 3 additions & 2 deletions server/internal/adapter/gql/gqlmodel/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gqlmodel
import (
"testing"

"github.com/reearth/reearth/server/pkg/policy"
"github.com/reearth/reearthx/account/accountdomain/workspace"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -33,8 +34,8 @@ func TestToPolicy(t *testing.T) {
AssetStorageSize: lo.ToPtr(int64(5)),
DatasetCount: lo.ToPtr(6),
DatasetSchemaCount: lo.ToPtr(7),
}, ToPolicy(workspace.NewPolicy(workspace.PolicyOption{
ID: workspace.PolicyID("x"),
}, ToPolicy(policy.New(policy.Option{
ID: policy.ID("x"),
Name: "aaa",
ProjectCount: lo.ToPtr(1),
MemberCount: lo.ToPtr(2),
Expand Down
2 changes: 1 addition & 1 deletion server/internal/adapter/gql/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewLoaders(usecases *interfaces.Container) *Loaders {
Dataset: NewDatasetLoader(usecases.Dataset),
Layer: NewLayerLoader(usecases.Layer),
Plugin: NewPluginLoader(usecases.Plugin),
Policy: NewPolicyLoader(usecases.Workspace),
Policy: NewPolicyLoader(usecases.Policy),
Project: NewProjectLoader(usecases.Project),
Property: NewPropertyLoader(usecases.Property),
Scene: NewSceneLoader(usecases.Scene),
Expand Down
14 changes: 7 additions & 7 deletions server/internal/adapter/gql/loader_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ import (

"github.com/reearth/reearth/server/internal/adapter/gql/gqldataloader"
"github.com/reearth/reearth/server/internal/adapter/gql/gqlmodel"
"github.com/reearth/reearthx/account/accountdomain/workspace"
"github.com/reearth/reearthx/account/accountusecase/accountinterfaces"
"github.com/reearth/reearth/server/internal/usecase/interfaces"
"github.com/reearth/reearth/server/pkg/policy"
"github.com/reearth/reearthx/util"
)

type PolicyLoader struct {
usecase accountinterfaces.Workspace
usecase interfaces.Policy
}

func NewPolicyLoader(usecase accountinterfaces.Workspace) *PolicyLoader {
func NewPolicyLoader(usecase interfaces.Policy) *PolicyLoader {
return &PolicyLoader{usecase: usecase}
}

func (c *PolicyLoader) Fetch(ctx context.Context, ids []gqlmodel.ID) ([]*gqlmodel.Policy, []error) {
uids := util.Map(ids, func(i gqlmodel.ID) workspace.PolicyID { return workspace.PolicyID(i) })
res, err := c.usecase.FetchPolicy(ctx, uids, getAcOperator(ctx))
uids := util.Map(ids, func(i gqlmodel.ID) policy.ID { return policy.ID(i) })
res, err := c.usecase.FetchPolicy(ctx, uids)
if err != nil {
return nil, []error{err}
}

return util.Map(res, func(p *workspace.Policy) *gqlmodel.Policy {
return util.Map(res, func(p *policy.Policy) *gqlmodel.Policy {
return gqlmodel.ToPolicy(p)
}), nil
}
Expand Down
4 changes: 2 additions & 2 deletions server/internal/app/config/policy.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import "github.com/reearth/reearthx/account/accountdomain/workspace"
import "github.com/reearth/reearth/server/pkg/policy"

type PolicyConfig struct {
Default *workspace.PolicyID
Default *policy.ID
}
14 changes: 7 additions & 7 deletions server/internal/infrastructure/memory/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ package memory
import (
"context"

"github.com/reearth/reearthx/account/accountdomain/workspace"
"github.com/reearth/reearth/server/pkg/policy"
"github.com/reearth/reearthx/rerror"
"github.com/reearth/reearthx/util"
"golang.org/x/exp/slices"
)

type Policy struct {
m util.SyncMap[workspace.PolicyID, *workspace.Policy]
m util.SyncMap[policy.ID, *policy.Policy]
}

func NewPolicy() *Policy {
return &Policy{}
}

func NewPolicyWith(policies ...*workspace.Policy) *Policy {
func NewPolicyWith(policies ...*policy.Policy) *Policy {
r := NewPolicy()
for _, p := range policies {
if p == nil {
Expand All @@ -28,18 +28,18 @@ func NewPolicyWith(policies ...*workspace.Policy) *Policy {
return r
}

func (r *Policy) FindByID(_ context.Context, id workspace.PolicyID) (*workspace.Policy, error) {
func (r *Policy) FindByID(_ context.Context, id policy.ID) (*policy.Policy, error) {
p, ok := r.m.Load(id)
if !ok {
return nil, rerror.ErrNotFound
}
return p.Clone(), nil
}

func (r *Policy) FindByIDs(_ context.Context, ids []workspace.PolicyID) ([]*workspace.Policy, error) {
func (r *Policy) FindByIDs(_ context.Context, ids []policy.ID) ([]*policy.Policy, error) {
policies := r.m.LoadAll(ids...)
slices.SortStableFunc(policies, func(a, b *workspace.Policy) bool {
slices.SortStableFunc(policies, func(a, b *policy.Policy) bool {
return a.ID() < b.ID()
})
return util.Map(policies, func(p *workspace.Policy) *workspace.Policy { return p.Clone() }), nil
return util.Map(policies, func(p *policy.Policy) *policy.Policy { return p.Clone() }), nil
}
26 changes: 11 additions & 15 deletions server/internal/infrastructure/memory/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,43 @@ import (
"context"
"testing"

"github.com/reearth/reearthx/account/accountdomain/workspace"
"github.com/reearth/reearth/server/pkg/policy"
"github.com/reearth/reearthx/rerror"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
)

func TestPolicy_FindByID(t *testing.T) {
p := workspace.NewPolicy(workspace.PolicyOption{
ID: workspace.PolicyID("a"),
p := policy.New(policy.Option{
ID: policy.ID("a"),
MemberCount: lo.ToPtr(1),
})
r := NewPolicyWith(p)

ctx := context.Background()
got, err := r.FindByID(ctx, workspace.PolicyID("a"))
got, err := r.FindByID(ctx, policy.ID("a"))
assert.Equal(t, p, got)
assert.NotSame(t, p, got)
assert.NoError(t, err)

got, err = r.FindByID(ctx, workspace.PolicyID("x"))
got, err = r.FindByID(ctx, policy.ID("x"))
assert.Nil(t, got)
assert.Equal(t, rerror.ErrNotFound, err)
}

func TestPolicy_FindByIDs(t *testing.T) {
p1 := workspace.NewPolicy(workspace.PolicyOption{
ID: workspace.PolicyID("a"),
p1 := policy.New(policy.Option{
ID: policy.ID("a"),
MemberCount: lo.ToPtr(1),
})
p2 := workspace.NewPolicy(workspace.PolicyOption{
ID: workspace.PolicyID("b"),
p2 := policy.New(policy.Option{
ID: policy.ID("b"),
MemberCount: lo.ToPtr(1),
})
r := NewPolicyWith(p1, p2)

ctx := context.Background()
got, err := r.FindByIDs(ctx, []workspace.PolicyID{
workspace.PolicyID("a"),
workspace.PolicyID("b"),
workspace.PolicyID("c"),
})
assert.Equal(t, []*workspace.Policy{p1, p2}, got)
got, err := r.FindByIDs(ctx, []policy.ID{"a", "b", "c"})
assert.Equal(t, []*policy.Policy{p1, p2}, got)
assert.NoError(t, err)
}
4 changes: 2 additions & 2 deletions server/internal/infrastructure/mongo/mongodoc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package mongodoc

import (
"github.com/reearth/reearth/server/pkg/config"
"github.com/reearth/reearthx/account/accountdomain/workspace"
"github.com/reearth/reearth/server/pkg/policy"
)

type ConfigDocument struct {
Migration int64
Auth *Auth
DefaultPolicy *workspace.PolicyID
DefaultPolicy *policy.ID
}

type Auth struct {
Expand Down
12 changes: 6 additions & 6 deletions server/internal/infrastructure/mongo/mongodoc/policy.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package mongodoc

import (
"github.com/reearth/reearthx/account/accountdomain/workspace"
"github.com/reearth/reearth/server/pkg/policy"
"github.com/reearth/reearthx/mongox"
)

type PolicyDocument struct {
ID workspace.PolicyID
ID policy.ID
Name string `bson:"name,omitempty"`
ProjectCount *int
PublishedProjectCount *int
Expand All @@ -17,8 +17,8 @@ type PolicyDocument struct {
AssetStorageSize *int64
}

func (d PolicyDocument) Model() *workspace.Policy {
return workspace.NewPolicy(workspace.PolicyOption{
func (d PolicyDocument) Model() *policy.Policy {
return policy.New(policy.Option{
ID: d.ID,
Name: d.Name,
ProjectCount: d.ProjectCount,
Expand All @@ -31,10 +31,10 @@ func (d PolicyDocument) Model() *workspace.Policy {
})
}

type PolicyConsumer = mongox.SliceFuncConsumer[*PolicyDocument, *workspace.Policy]
type PolicyConsumer = mongox.SliceFuncConsumer[*PolicyDocument, *policy.Policy]

func NewPolicyConsumer() *PolicyConsumer {
return mongox.NewSliceFuncConsumer(func(d *PolicyDocument) (*workspace.Policy, error) {
return mongox.NewSliceFuncConsumer(func(d *PolicyDocument) (*policy.Policy, error) {
return d.Model(), nil
})
}
12 changes: 6 additions & 6 deletions server/internal/infrastructure/mongo/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"

"github.com/reearth/reearth/server/internal/infrastructure/mongo/mongodoc"
"github.com/reearth/reearthx/account/accountdomain/workspace"
"github.com/reearth/reearth/server/pkg/policy"
"github.com/reearth/reearthx/mongox"
"github.com/reearth/reearthx/util"
"go.mongodb.org/mongo-driver/bson"
Expand All @@ -29,29 +29,29 @@ func (r *Policy) Init(ctx context.Context) error {
return createIndexes(ctx, r.client, policyIndexes, policyUniqueIndexes)
}

func (r *Policy) FindByID(ctx context.Context, id workspace.PolicyID) (*workspace.Policy, error) {
func (r *Policy) FindByID(ctx context.Context, id policy.ID) (*policy.Policy, error) {
return r.findOne(ctx, bson.M{
"id": id.String(),
})
}

func (r *Policy) FindByIDs(ctx context.Context, ids []workspace.PolicyID) ([]*workspace.Policy, error) {
func (r *Policy) FindByIDs(ctx context.Context, ids []policy.ID) ([]*policy.Policy, error) {
return r.find(ctx, bson.M{
"id": bson.M{
"$in": util.Map(ids, func(id workspace.PolicyID) string { return id.String() }),
"$in": util.Map(ids, func(id policy.ID) string { return id.String() }),
},
})
}

func (r *Policy) findOne(ctx context.Context, filter interface{}) (*workspace.Policy, error) {
func (r *Policy) findOne(ctx context.Context, filter interface{}) (*policy.Policy, error) {
c := mongodoc.NewPolicyConsumer()
if err := r.client.FindOne(ctx, filter, c); err != nil {
return nil, err
}
return c.Result[0], nil
}

func (r *Policy) find(ctx context.Context, filter interface{}) ([]*workspace.Policy, error) {
func (r *Policy) find(ctx context.Context, filter interface{}) ([]*policy.Policy, error) {
c := mongodoc.NewPolicyConsumer()
if err := r.client.Find(ctx, filter, c); err != nil {
return nil, err
Expand Down
22 changes: 11 additions & 11 deletions server/internal/infrastructure/mongo/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"testing"

"github.com/reearth/reearthx/account/accountdomain/workspace"
"github.com/reearth/reearth/server/pkg/policy"
"github.com/reearth/reearthx/mongox"
"github.com/reearth/reearthx/mongox/mongotest"
"github.com/reearth/reearthx/rerror"
Expand All @@ -24,14 +24,14 @@ func TestPolicy_FindByID(t *testing.T) {
"membercount": 1,
})

got, err := r.FindByID(ctx, workspace.PolicyID("policy"))
got, err := r.FindByID(ctx, policy.ID("policy"))
assert.NoError(t, err)
assert.Equal(t, workspace.NewPolicy(workspace.PolicyOption{
ID: workspace.PolicyID("policy"),
assert.Equal(t, policy.New(policy.Option{
ID: policy.ID("policy"),
MemberCount: lo.ToPtr(1),
}), got)

got2, err2 := r.FindByID(ctx, workspace.PolicyID("policy2"))
got2, err2 := r.FindByID(ctx, policy.ID("policy2"))
assert.Equal(t, rerror.ErrNotFound, err2)
assert.Nil(t, got2)
}
Expand All @@ -53,17 +53,17 @@ func TestPolicy_FindByIDs(t *testing.T) {
},
})

got, err := r.FindByIDs(ctx, []workspace.PolicyID{"policy1", "policy2"})
got, err := r.FindByIDs(ctx, []policy.ID{"policy1", "policy2"})
assert.NoError(t, err)
assert.Equal(
t,
[]*workspace.Policy{
workspace.NewPolicy(workspace.PolicyOption{
ID: workspace.PolicyID("policy1"),
[]*policy.Policy{
policy.New(policy.Option{
ID: policy.ID("policy1"),
MemberCount: lo.ToPtr(1),
}),
workspace.NewPolicy(workspace.PolicyOption{
ID: workspace.PolicyID("policy2"),
policy.New(policy.Option{
ID: policy.ID("policy2"),
MemberCount: lo.ToPtr(2),
}),
},
Expand Down
1 change: 1 addition & 0 deletions server/internal/usecase/interactor/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func NewContainer(r *repo.Container, g *gateway.Container,
Layer: NewLayer(r),
NLSLayer: NewNLSLayer(r),
Plugin: NewPlugin(r, g),
Policy: NewPolicy(r),
Project: NewProject(r, g),
Property: NewProperty(r, g),
Published: published,
Expand Down
Loading
Loading