Skip to content

Commit

Permalink
Merge branch 'main' into chore-increase-package-ut-coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkarel authored Nov 26, 2024
2 parents c256267 + 63a0652 commit 29ca006
Show file tree
Hide file tree
Showing 31 changed files with 387 additions and 180 deletions.
2 changes: 2 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2602,6 +2602,8 @@ github.com/reearth/reearthx v0.0.0-20241023075926-e29bdd6c4ae3 h1:aFm6QNDFs08EKl
github.com/reearth/reearthx v0.0.0-20241023075926-e29bdd6c4ae3/go.mod h1:d1WXkdCVzSoc8pl3vW9/9yKfk4fdoZQZhX8Ot8jqgnc=
github.com/reearth/reearthx v0.0.0-20241025125329-f01a05daf443 h1:r3bAWyEVAMX60W70OPeWd0uA+2sLhXgox41rQb2XKDY=
github.com/reearth/reearthx v0.0.0-20241025125329-f01a05daf443/go.mod h1:d1WXkdCVzSoc8pl3vW9/9yKfk4fdoZQZhX8Ot8jqgnc=
github.com/reearth/reearthx v0.0.0-20241121115830-a7f7b61afe26 h1:epJj+4FT1OkHr4uOugcGU70pT3SKK1VquLi1z8aE2wk=
github.com/reearth/reearthx v0.0.0-20241121115830-a7f7b61afe26/go.mod h1:/ByvE9o0WANHL2nhOyZjOXWwY8cCgze0OmwyNzxcYoA=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/robertkrimen/godocdown v0.0.0-20130622164427-0bfa04905481 h1:jMxcLa+VjJKhpCwbLUXAD15wJ+hhvXMLujCl3MkXpfM=
Expand Down
2 changes: 1 addition & 1 deletion server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/oapi-codegen/runtime v1.1.1
github.com/paulmach/go.geojson v1.5.0
github.com/ravilushqa/otelgqlgen v0.17.0
github.com/reearth/reearthx v0.0.0-20241120070128-4c4377743b26
github.com/reearth/reearthx v0.0.0-20241121115830-a7f7b61afe26
github.com/robbiet480/go.sns v0.0.0-20230523235941-e8d832c79d68
github.com/samber/lo v1.47.0
github.com/sendgrid/sendgrid-go v3.16.0+incompatible
Expand Down
4 changes: 2 additions & 2 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSg
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/ravilushqa/otelgqlgen v0.17.0 h1:bLwQfKqtj9P24QpjM2sc1ipBm5Fqv2u7DKN5LIpj3g8=
github.com/ravilushqa/otelgqlgen v0.17.0/go.mod h1:orOIikuYsay1y3CmLgd5gsHcT9EsnXwNKmkAplzzYXQ=
github.com/reearth/reearthx v0.0.0-20241120070128-4c4377743b26 h1:vww2kw1q6as/FZ0SJwdD8o2URCeEw7HcIQWtI3ezMLY=
github.com/reearth/reearthx v0.0.0-20241120070128-4c4377743b26/go.mod h1:/ByvE9o0WANHL2nhOyZjOXWwY8cCgze0OmwyNzxcYoA=
github.com/reearth/reearthx v0.0.0-20241121115830-a7f7b61afe26 h1:epJj+4FT1OkHr4uOugcGU70pT3SKK1VquLi1z8aE2wk=
github.com/reearth/reearthx v0.0.0-20241121115830-a7f7b61afe26/go.mod h1:/ByvE9o0WANHL2nhOyZjOXWwY8cCgze0OmwyNzxcYoA=
github.com/robbiet480/go.sns v0.0.0-20230523235941-e8d832c79d68 h1:Jknsfy5cqCH6qAuoU1qNZ51hfBJfMSJYwsH9j9mdVnw=
github.com/robbiet480/go.sns v0.0.0-20230523235941-e8d832c79d68/go.mod h1:9CDhL7uDVy8vEVDNPJzxq89dPaPBWP6hxQcC8woBHus=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
Expand Down
36 changes: 25 additions & 11 deletions server/internal/infrastructure/memory/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ type Item struct {
err error
}

func (r *Item) FindByAssets(ctx context.Context, list id.AssetIDList, ref *version.Ref) (item.VersionedList, error) {
func NewItem() repo.Item {
return &Item{
data: memorygit.NewVersionedSyncMap[item.ID, *item.Item](),
}
}

func (r *Item) FindByAssets(_ context.Context, list id.AssetIDList, ref *version.Ref) (item.VersionedList, error) {
if r.err != nil {
return nil, r.err
}
Expand All @@ -41,12 +47,6 @@ func (r *Item) FindByAssets(ctx context.Context, list id.AssetIDList, ref *versi
return res, nil
}

func NewItem() repo.Item {
return &Item{
data: memorygit.NewVersionedSyncMap[item.ID, *item.Item](),
}
}

func (r *Item) Filtered(filter repo.ProjectFilter) repo.Item {
return &Item{
data: r.data,
Expand Down Expand Up @@ -109,7 +109,7 @@ func (r *Item) FindByIDs(_ context.Context, list id.ItemIDList, ref *version.Ref
return r.data.LoadAll(list, lo.ToPtr(ref.OrLatest().OrVersion())), nil
}

func (r *Item) FindVersionByID(ctx context.Context, itemID id.ItemID, ver version.VersionOrRef) (item.Versioned, error) {
func (r *Item) FindVersionByID(_ context.Context, itemID id.ItemID, ver version.VersionOrRef) (item.Versioned, error) {
if r.err != nil {
return nil, r.err
}
Expand All @@ -133,7 +133,7 @@ func (r *Item) FindAllVersionsByID(_ context.Context, id id.ItemID) (item.Versio
}), nil
}

func (r *Item) FindAllVersionsByIDs(ctx context.Context, ids id.ItemIDList) (item.VersionedList, error) {
func (r *Item) FindAllVersionsByIDs(_ context.Context, ids id.ItemIDList) (item.VersionedList, error) {
if r.err != nil {
return nil, r.err
}
Expand All @@ -145,7 +145,7 @@ func (r *Item) FindAllVersionsByIDs(ctx context.Context, ids id.ItemIDList) (ite
}), nil
}

func (r *Item) LastModifiedByModel(ctx context.Context, modelID id.ModelID) (time.Time, error) {
func (r *Item) LastModifiedByModel(_ context.Context, modelID id.ModelID) (time.Time, error) {
if r.err != nil {
return time.Time{}, r.err
}
Expand Down Expand Up @@ -175,7 +175,21 @@ func (r *Item) Save(_ context.Context, t *item.Item) error {
return nil
}

func (r *Item) UpdateRef(ctx context.Context, item id.ItemID, ref version.Ref, vr *version.VersionOrRef) error {
func (r *Item) SaveAll(_ context.Context, il item.List) error {
if r.err != nil {
return r.err
}

for _, t := range il {
if !r.f.CanWrite(t.Project()) {
return repo.ErrOperationDenied
}
}
r.data.SaveAll(il.IDs(), il, nil)
return nil
}

func (r *Item) UpdateRef(_ context.Context, item id.ItemID, ref version.Ref, vr *version.VersionOrRef) error {
if r.err != nil {
return r.err
}
Expand Down
12 changes: 12 additions & 0 deletions server/internal/infrastructure/memory/memorygit/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ func (m *VersionedSyncMap[K, V]) SaveOne(key K, value V, parent *version.Version
}
}

func (m *VersionedSyncMap[K, V]) SaveAll(key []K, value []V, parent []*version.VersionOrRef) {
if len(key) != len(value) || (parent != nil && len(key) != len(parent)) {
return
}
if len(key) == 0 {
return
}
for i := 0; i < len(key); i++ {
m.SaveOne(key[i], value[i], parent[i])
}
}

func (m *VersionedSyncMap[K, V]) UpdateRef(key K, ref version.Ref, vr *version.VersionOrRef) {
m.Range(func(k K, v *version.Values[V]) bool {
if k == key {
Expand Down
14 changes: 14 additions & 0 deletions server/internal/infrastructure/memory/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ func (r *Thread) Save(_ context.Context, th *thread.Thread) error {
return nil
}

func (r *Thread) SaveAll(_ context.Context, th thread.List) error {
if r.err != nil {
return r.err
}

for _, t := range th {
if !r.f.CanWrite(t.Workspace()) {
return repo.ErrOperationDenied
}
}
r.data.StoreAll(th.ToMap())
return nil
}

func (r *Thread) Filtered(f repo.WorkspaceFilter) repo.Thread {
return &Thread{
data: r.data,
Expand Down
15 changes: 15 additions & 0 deletions server/internal/infrastructure/mongo/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/reearth/reearthx/mongox"
"github.com/reearth/reearthx/rerror"
"github.com/reearth/reearthx/usecasex"
"github.com/samber/lo"
"go.mongodb.org/mongo-driver/bson"
)

Expand Down Expand Up @@ -228,6 +229,20 @@ func (r *Item) Save(ctx context.Context, item *item.Item) error {
return r.client.SaveOne(ctx, id, doc, nil)
}

func (r *Item) SaveAll(ctx context.Context, items item.List) error {
if len(items) == 0 {
return nil
}

for _, itm := range items {
if !r.f.CanWrite(itm.Project()) {
return repo.ErrOperationDenied
}
}
docs, ids := mongodoc.NewItems(items)
return r.client.SaveAll(ctx, ids, lo.ToAnySlice(docs), nil)
}

func (r *Item) UpdateRef(ctx context.Context, item id.ItemID, ref version.Ref, vr *version.VersionOrRef) error {
return r.client.UpdateRef(ctx, item.String(), ref, vr)
}
Expand Down
14 changes: 14 additions & 0 deletions server/internal/infrastructure/mongo/mongodoc/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ func NewThread(a *thread.Thread) (*ThreadDocument, string) {
return thd, id
}

func NewThreads(a thread.List) ([]ThreadDocument, []string) {
res := make([]ThreadDocument, 0, len(a))
ids := make([]string, 0, len(a))
for _, th := range a {
if th == nil {
continue
}
thDoc, thId := NewThread(th)
res = append(res, *thDoc)
ids = append(ids, thId)
}
return res, ids
}

func (d *ThreadDocument) Model() (*thread.Thread, error) {
thid, err := id.ThreadIDFrom(d.ID)
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions server/internal/infrastructure/mongo/mongogit/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,27 @@ func (c *Collection) SaveOne(ctx context.Context, id string, d any, parent *vers
return nil
}

func (c *Collection) SaveAll(ctx context.Context, ids []string, docs []any, parents []*version.VersionOrRef) error {
// TODO: optimize to use bulk write
if len(ids) != len(docs) || (parents != nil && len(ids) != len(parents)) {
return rerror.ErrInvalidParams
}
if len(ids) == 0 {
return nil
}
for i := 0; i < len(ids); i++ {
var parent *version.VersionOrRef = nil
if parents != nil {
parent = parents[i]
}
err := c.SaveOne(ctx, ids[i], docs[i], parent)
if err != nil {
return err
}
}
return nil
}

func (c *Collection) UpdateRef(ctx context.Context, id string, ref version.Ref, dest *version.VersionOrRef) error {
if _, err := c.client.Client().UpdateMany(ctx, bson.M{
"id": id,
Expand Down
14 changes: 14 additions & 0 deletions server/internal/infrastructure/mongo/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/reearth/reearth-cms/server/pkg/thread"
"github.com/reearth/reearthx/mongox"
"github.com/reearth/reearthx/rerror"
"github.com/samber/lo"
"go.mongodb.org/mongo-driver/bson"
)

Expand Down Expand Up @@ -39,6 +40,19 @@ func (r *ThreadRepo) Save(ctx context.Context, thread *thread.Thread) error {
return r.client.SaveOne(ctx, id, doc)
}

func (r *ThreadRepo) SaveAll(ctx context.Context, threads thread.List) error {
if len(threads) == 0 {
return nil
}
for _, t := range threads {
if !r.f.CanWrite(t.Workspace()) {
return repo.ErrOperationDenied
}
}
docs, ids := mongodoc.NewThreads(threads)
return r.client.SaveAll(ctx, ids, lo.ToAnySlice(docs))
}

func (r *ThreadRepo) Filtered(f repo.WorkspaceFilter) repo.Thread {
return &ThreadRepo{
client: r.client,
Expand Down
Loading

0 comments on commit 29ca006

Please sign in to comment.