Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
fix: scene exporter should export layers and tags while maintaining t…
Browse files Browse the repository at this point in the history
…he tree structure (#104)

* export layers and tags while maintaining the tree structure

* fix

* export invislbe layers
  • Loading branch information
rot1024 committed Feb 1, 2022
1 parent b398db3 commit 805d788
Show file tree
Hide file tree
Showing 31 changed files with 1,061 additions and 283 deletions.
18 changes: 9 additions & 9 deletions internal/infrastructure/memory/tag.go
Expand Up @@ -13,7 +13,7 @@ import (

type Tag struct {
lock sync.Mutex
data map[id.TagID]tag.Tag
data tag.Map
}

func NewTag() repo.Tag {
Expand Down Expand Up @@ -50,6 +50,13 @@ func (t *Tag) FindByIDs(ctx context.Context, tids []id.TagID, ids []id.SceneID)
return res, nil
}

func (t *Tag) FindByScene(ctx context.Context, sceneID id.SceneID) ([]*tag.Tag, error) {
t.lock.Lock()
defer t.lock.Unlock()

return t.data.All().FilterByScene(sceneID).Refs(), nil
}

func (t *Tag) FindItemByID(ctx context.Context, tagID id.TagID, ids []id.SceneID) (*tag.Item, error) {
t.lock.Lock()
defer t.lock.Unlock()
Expand Down Expand Up @@ -116,14 +123,7 @@ func (t *Tag) FindRootsByScene(ctx context.Context, sceneID id.SceneID) ([]*tag.
t.lock.Lock()
defer t.lock.Unlock()

var res []*tag.Tag
for _, tag := range t.data {
tag := tag
if tag.Scene() == sceneID {
res = append(res, &tag)
}
}
return res, nil
return t.data.All().FilterByScene(sceneID).Roots().Refs(), nil
}

func (t *Tag) Save(ctx context.Context, tag tag.Tag) error {
Expand Down
20 changes: 9 additions & 11 deletions internal/infrastructure/memory/tag_test.go
Expand Up @@ -4,11 +4,9 @@ import (
"context"
"testing"

"github.com/reearth/reearth-backend/pkg/rerror"

"github.com/reearth/reearth-backend/pkg/id"
"github.com/reearth/reearth-backend/pkg/rerror"
"github.com/reearth/reearth-backend/pkg/tag"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -41,7 +39,7 @@ func TestTag_FindByIDs(t *testing.T) {
sid2 := id.NewSceneID()
sl := []id.SceneID{sid}
t1, _ := tag.NewItem().NewID().Scene(sid).Label("item").Build()
tl := tag.NewListFromTags([]id.TagID{t1.ID()})
tl := tag.IDListFrom([]id.TagID{t1.ID()})
t2, _ := tag.NewGroup().NewID().Scene(sid).Label("group").Tags(tl).Build()
t3, _ := tag.NewItem().NewID().Scene(sid2).Label("item2").Build()
tti := tag.Tag(t1)
Expand All @@ -64,7 +62,7 @@ func TestTag_FindRootsByScene(t *testing.T) {
sid := id.NewSceneID()
sid2 := id.NewSceneID()
t1, _ := tag.NewItem().NewID().Scene(sid).Label("item").Build()
tl := tag.NewListFromTags([]id.TagID{t1.ID()})
tl := tag.IDListFrom([]id.TagID{t1.ID()})
t2, _ := tag.NewGroup().NewID().Scene(sid).Label("group").Tags(tl).Build()
t3, _ := tag.NewItem().NewID().Scene(sid2).Label("item2").Build()
tti := tag.Tag(t1)
Expand All @@ -87,7 +85,7 @@ func TestTag_FindGroupByID(t *testing.T) {
sid := id.NewSceneID()
sl := []id.SceneID{sid}
t1, _ := tag.NewItem().NewID().Scene(sid).Label("item").Build()
tl := tag.NewListFromTags([]id.TagID{t1.ID()})
tl := tag.IDListFrom([]id.TagID{t1.ID()})
t2, _ := tag.NewGroup().NewID().Scene(sid).Label("group").Tags(tl).Build()
tti := tag.Tag(t1)
ttg := tag.Tag(t2)
Expand All @@ -110,7 +108,7 @@ func TestTag_FindItemByID(t *testing.T) {
sid := id.NewSceneID()
sl := []id.SceneID{sid}
t1, _ := tag.NewItem().NewID().Scene(sid).Label("item").Build()
tl := tag.NewListFromTags([]id.TagID{t1.ID()})
tl := tag.IDListFrom([]id.TagID{t1.ID()})
t2, _ := tag.NewGroup().NewID().Scene(sid).Label("group").Tags(tl).Build()
tti := tag.Tag(t1)
ttg := tag.Tag(t2)
Expand Down Expand Up @@ -208,7 +206,7 @@ func TestTag_Remove(t *testing.T) {
ctx := context.Background()
sid := id.NewSceneID()
t1, _ := tag.NewItem().NewID().Scene(sid).Label("item").Build()
tl := tag.NewListFromTags([]id.TagID{t1.ID()})
tl := tag.IDListFrom([]id.TagID{t1.ID()})
t2, _ := tag.NewGroup().NewID().Scene(sid).Label("group").Tags(tl).Build()
tti := tag.Tag(t1)
ttg := tag.Tag(t2)
Expand All @@ -228,7 +226,7 @@ func TestTag_RemoveAll(t *testing.T) {
ctx := context.Background()
sid := id.NewSceneID()
t1, _ := tag.NewItem().NewID().Scene(sid).Label("item").Build()
tl := tag.NewListFromTags([]id.TagID{t1.ID()})
tl := tag.IDListFrom([]id.TagID{t1.ID()})
t2, _ := tag.NewItem().NewID().Scene(sid).Label("item").Build()
t3, _ := tag.NewGroup().NewID().Scene(sid).Label("group").Tags(tl).Build()
tti := tag.Tag(t1)
Expand All @@ -252,7 +250,7 @@ func TestTag_RemoveByScene(t *testing.T) {
sid := id.NewSceneID()
sid2 := id.NewSceneID()
t1, _ := tag.NewItem().NewID().Scene(sid).Label("item").Build()
tl := tag.NewListFromTags([]id.TagID{t1.ID()})
tl := tag.IDListFrom([]id.TagID{t1.ID()})
t2, _ := tag.NewItem().NewID().Scene(sid2).Label("item").Build()
t3, _ := tag.NewGroup().NewID().Scene(sid).Label("group").Tags(tl).Build()
tti := tag.Tag(t1)
Expand All @@ -276,7 +274,7 @@ func TestTag_FindGroupByItem(t *testing.T) {
sid := id.NewSceneID()
sl := []id.SceneID{sid}
t1, _ := tag.NewItem().NewID().Scene(sid).Label("item").Build()
tl := tag.NewListFromTags([]id.TagID{t1.ID()})
tl := tag.IDListFrom([]id.TagID{t1.ID()})
t2, _ := tag.NewGroup().NewID().Scene(sid).Label("group").Tags(tl).Build()
tti := tag.Tag(t1)
ttg := tag.Tag(t2)
Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/mongodoc/tag.go
Expand Up @@ -177,6 +177,6 @@ func (d *TagDocument) ModelGroup() (*tag.Group, error) {
ID(tid).
Label(d.Label).
Scene(sid).
Tags(tag.NewListFromTags(ids)).
Tags(tag.IDListFrom(ids)).
Build()
}
8 changes: 4 additions & 4 deletions internal/infrastructure/mongo/mongodoc/tag_test.go
Expand Up @@ -25,7 +25,7 @@ func TestNewTag(t *testing.T) {
tg, _ := tag.NewGroup().
NewID().
Label("group").
Tags(tag.NewListFromTags([]id.TagID{ti.ID()})).
Tags(tag.IDListFrom([]id.TagID{ti.ID()})).
Scene(sid).
Build()
type args struct {
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestNewTags(t *testing.T) {
tg, _ := tag.NewGroup().
NewID().
Label("group").
Tags(tag.NewListFromTags([]id.TagID{ti.ID()})).
Tags(tag.IDListFrom([]id.TagID{ti.ID()})).
Scene(sid).
Build()
tgi := tag.Tag(tg)
Expand Down Expand Up @@ -252,7 +252,7 @@ func TestTagDocument_Model(t *testing.T) {
tg, _ := tag.NewGroup().
NewID().
Label("group").
Tags(tag.NewListFromTags([]id.TagID{ti.ID()})).
Tags(tag.IDListFrom([]id.TagID{ti.ID()})).
Scene(sid).
Build()
type fields struct {
Expand Down Expand Up @@ -341,7 +341,7 @@ func TestTagDocument_ModelGroup(t *testing.T) {
tg, _ := tag.NewGroup().
NewID().
Label("group").
Tags(tag.NewListFromTags([]id.TagID{ti.ID()})).
Tags(tag.IDListFrom([]id.TagID{ti.ID()})).
Scene(sid).
Build()
type fields struct {
Expand Down
7 changes: 7 additions & 0 deletions internal/infrastructure/mongo/tag.go
Expand Up @@ -51,6 +51,13 @@ func (r *tagRepo) FindByIDs(ctx context.Context, ids []id.TagID, f []id.SceneID)
return filterTags(ids, res), nil
}

func (r *tagRepo) FindByScene(ctx context.Context, id id.SceneID) ([]*tag.Tag, error) {
filter := bson.M{
"scene": id.String(),
}
return r.find(ctx, nil, filter)
}

func (r *tagRepo) FindItemByID(ctx context.Context, id id.TagID, f []id.SceneID) (*tag.Item, error) {
filter := r.sceneFilter(bson.D{
{Key: "id", Value: id.String()},
Expand Down
4 changes: 4 additions & 0 deletions internal/usecase/interactor/project.go
Expand Up @@ -29,6 +29,7 @@ type Project struct {
layerRepo repo.Layer
datasetRepo repo.Dataset
datasetSchemaRepo repo.DatasetSchema
tagRepo repo.Tag
transaction repo.Transaction
file gateway.File
}
Expand All @@ -46,6 +47,7 @@ func NewProject(r *repo.Container, gr *gateway.Container) interfaces.Project {
layerRepo: r.Layer,
datasetRepo: r.Dataset,
datasetSchemaRepo: r.DatasetSchema,
tagRepo: r.Tag,
transaction: r.Transaction,
file: gr.File,
}
Expand Down Expand Up @@ -315,6 +317,8 @@ func (i *Project) Publish(ctx context.Context, params interfaces.PublishProjectP
repo.LayerLoaderFrom(i.layerRepo, scenes),
repo.PropertyLoaderFrom(i.propertyRepo, scenes),
repo.DatasetGraphLoaderFrom(i.datasetRepo, scenes),
repo.TagLoaderFrom(i.tagRepo, scenes),
repo.TagSceneLoaderFrom(i.tagRepo, scenes),
).BuildScene(ctx, w, s, time.Now())
}()

Expand Down
2 changes: 1 addition & 1 deletion internal/usecase/interactor/tag.go
Expand Up @@ -103,7 +103,7 @@ func (i *Tag) CreateGroup(ctx context.Context, inp interfaces.CreateTagGroupPara
return nil, interfaces.ErrOperationDenied
}

list := tag.NewListFromTags(inp.Tags)
list := tag.IDListFrom(inp.Tags)
group, err := tag.NewGroup().
NewID().
Label(inp.Label).
Expand Down
23 changes: 23 additions & 0 deletions internal/usecase/repo/tag.go
Expand Up @@ -10,6 +10,7 @@ import (
type Tag interface {
FindByID(context.Context, id.TagID, []id.SceneID) (tag.Tag, error)
FindByIDs(context.Context, []id.TagID, []id.SceneID) ([]*tag.Tag, error)
FindByScene(context.Context, id.SceneID) ([]*tag.Tag, error)
FindItemByID(context.Context, id.TagID, []id.SceneID) (*tag.Item, error)
FindItemByIDs(context.Context, []id.TagID, []id.SceneID) ([]*tag.Item, error)
FindGroupByID(context.Context, id.TagID, []id.SceneID) (*tag.Group, error)
Expand All @@ -22,3 +23,25 @@ type Tag interface {
RemoveAll(context.Context, []id.TagID) error
RemoveByScene(context.Context, id.SceneID) error
}

func TagLoaderFrom(r Tag, scenes []id.SceneID) tag.Loader {
return func(ctx context.Context, ids ...id.TagID) ([]*tag.Tag, error) {
return r.FindByIDs(ctx, ids, scenes)
}
}

func TagSceneLoaderFrom(r Tag, scenes []id.SceneID) tag.SceneLoader {
return func(ctx context.Context, id id.SceneID) ([]*tag.Tag, error) {
found := false
for _, s := range scenes {
if id == s {
found = true
break
}
}
if !found {
return nil, nil
}
return r.FindByScene(ctx, id)
}
}
3 changes: 3 additions & 0 deletions pkg/layer/group.go
Expand Up @@ -180,6 +180,9 @@ func (l *Group) ValidateProperties(pm property.Map) error {
}

func (l *Group) Tags() *TagList {
if l == nil {
return nil
}
if l.layerBase.tags == nil {
l.layerBase.tags = NewTagList(nil)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/layer/id.go
Expand Up @@ -60,10 +60,12 @@ var DatasetSchemaIDFromRefID = id.DatasetSchemaIDFromRefID
type IDSet = id.LayerIDSet
type InfoboxFIeldIDSet = id.InfoboxFieldIDSet
type DatasetIDSet = id.DatasetIDSet
type TagIDSet = id.TagIDSet

var NewIDSet = id.NewLayerIDSet
var NewInfoboxFIeldIDSet = id.NewInfoboxFieldIDSet
var NewDatasetIDSet = id.NewDatasetIDSet
var NewTagIDSet = id.NewTagIDSet

var OfficialPluginID = id.OfficialPluginID
var ErrInvalidID = id.ErrInvalidID
Expand Down
59 changes: 58 additions & 1 deletion pkg/layer/merged.go
Expand Up @@ -14,6 +14,14 @@ type Merged struct {
Infobox *MergedInfobox
PluginID *PluginID
ExtensionID *PluginExtensionID
IsVisible bool
Tags []MergedTag
}

// MergedTag represents a merged tag from two layers
type MergedTag struct {
ID TagID
Tags []MergedTag
}

// MergedInfobox represents a merged info box from two layers
Expand Down Expand Up @@ -48,8 +56,37 @@ func Merge(o Layer, p *Group) *Merged {
Parent: p.Property(),
LinkedDataset: ToLayerItem(o).LinkedDataset(),
},
Infobox: MergeInfobox(o.Infobox(), p.Infobox(), ToLayerItem(o).LinkedDataset()),
IsVisible: o.IsVisible(),
Tags: MergeTags(o.Tags(), p.Tags()),
Infobox: MergeInfobox(o.Infobox(), p.Infobox(), ToLayerItem(o).LinkedDataset()),
}
}

// MergeInfobox merges two tag lists
func MergeTags(o, _p *TagList) []MergedTag {
// Currently parent tags are ignored
tags := o.Tags()
if len(tags) == 0 {
return nil
}
res := make([]MergedTag, 0, len(tags))
for _, t := range tags {
tags := TagGroupFrom(t).Children()

var tags2 []MergedTag
if len(tags) > 0 {
tags2 = make([]MergedTag, 0, len(tags))
for _, t := range tags {
tags2 = append(tags2, MergedTag{ID: t.ID()})
}
}

res = append(res, MergedTag{
ID: t.ID(),
Tags: tags2,
})
}
return res
}

// MergeInfobox merges two infoboxes
Expand Down Expand Up @@ -149,3 +186,23 @@ func (m *Merged) Properties() []PropertyID {
}
return result
}

func (m *Merged) AllTags() (res []MergedTag) {
if m == nil {
return nil
}
for _, t := range m.Tags {
res = append(res, append([]MergedTag{t}, t.Tags...)...)
}
return res
}

func (m *Merged) AllTagIDs() (res []TagID) {
if m == nil {
return nil
}
for _, t := range m.AllTags() {
res = append(res, t.ID)
}
return res
}

0 comments on commit 805d788

Please sign in to comment.