Skip to content

Commit

Permalink
fix(server): plugin migrator did not update layers
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Jun 5, 2023
1 parent 96c4730 commit d0524ef
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 3 deletions.
2 changes: 1 addition & 1 deletion server/internal/usecase/repo/plugin.go
Expand Up @@ -16,7 +16,7 @@ type Plugin interface {
}

func PluginLoaderFrom(r Plugin) plugin.Loader {
return func(ctx context.Context, ids []id.PluginID) ([]*plugin.Plugin, error) {
return func(ctx context.Context, ids []id.PluginID) (plugin.List, error) {
return r.FindByIDs(ctx, ids)
}
}
6 changes: 6 additions & 0 deletions server/pkg/layer/list.go
@@ -1,7 +1,13 @@
package layer

import "github.com/samber/lo"

type List []*Layer

func ListFrom(l []Layer) []*Layer {
return lo.ToSlicePtr(l)
}

func (ll List) Last() *Layer {
if len(ll) == 0 {
return nil
Expand Down
11 changes: 11 additions & 0 deletions server/pkg/layer/loader.go
Expand Up @@ -3,6 +3,8 @@ package layer
import (
"context"
"errors"

"github.com/samber/lo"
)

type Loader func(context.Context, ...ID) (List, error)
Expand Down Expand Up @@ -70,3 +72,12 @@ func (l Loader) Walk(ctx context.Context, walker func(Layer, GroupList) error, i
}
return walk(init, nil)
}

func LoaderBySceneFrom(data ...Layer) LoaderByScene {
return func(ctx context.Context, id SceneID) (List, error) {
res := lo.Filter(data, func(l Layer, _ int) bool {
return l.Scene() == id
})
return ListFrom(res), nil
}
}
13 changes: 13 additions & 0 deletions server/pkg/layer/loader_test.go
Expand Up @@ -77,3 +77,16 @@ func TestLoader_Walk3(t *testing.T) {
assert.Equal(t, []Layer{l5, l3, l4}, layers)
assert.Equal(t, []GroupList{nil, {l5}, {l5}}, parents)
}

func TestLoaderBySceneFrom(t *testing.T) {
sid := NewSceneID()
l1 := NewItem().NewID().Scene(sid).MustBuild()
l2 := NewItem().NewID().Scene(sid).MustBuild()
l3 := NewItem().NewID().Scene(sid).MustBuild()
l4 := NewGroup().NewID().Scene(sid).Layers(NewIDList([]ID{l1.ID(), l2.ID()})).MustBuild()
l5 := NewGroup().NewID().Scene(sid).Layers(NewIDList([]ID{l3.ID(), l4.ID()})).MustBuild()
res, err := LoaderFrom([]Layer{l1, l2, l3, l4, l5})(context.Background(), l1.ID(), l5.ID())

assert.NoError(t, err)
assert.Equal(t, List{l1.LayerRef(), l5.LayerRef()}, res)
}
36 changes: 35 additions & 1 deletion server/pkg/plugin/loader.go
Expand Up @@ -4,4 +4,38 @@ import (
"context"
)

type Loader func(context.Context, []ID) ([]*Plugin, error)
type Loader func(context.Context, []ID) (List, error)

func LoaderFrom(data ...*Plugin) Loader {
return func(ctx context.Context, ids []ID) (List, error) {
res := make(List, 0, len(ids))
for _, i := range ids {
found := false
for _, d := range data {
if i == d.ID() {
res = append(res, d)
found = true
break
}
}
if !found {
res = append(res, nil)
}
}
return res, nil
}
}

func LoaderFromMap(data map[ID]*Plugin) Loader {
return func(ctx context.Context, ids []ID) (List, error) {
res := make(List, 0, len(ids))
for _, i := range ids {
if d, ok := data[i]; ok {
res = append(res, d)
} else {
res = append(res, nil)
}
}
return res, nil
}
}
27 changes: 27 additions & 0 deletions server/pkg/plugin/loader_test.go
@@ -0,0 +1,27 @@
package plugin

import (
"context"
"testing"

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

func TestLoaderFrom(t *testing.T) {
pid1 := MustID("plugin~1.1.1")
pid2 := MustID("plugin~1.1.2")
p1 := New().ID(pid1).MustBuild()
p2 := New().ID(pid2).MustBuild()

pl := LoaderFrom(p1, p2)
res, err := pl(context.Background(), []ID{pid1})

assert.Equal(t, List{p1}, res)
assert.NoError(t, err)

pml := LoaderFromMap(List{p1, p2}.Map())
res2, err2 := pml(context.Background(), []ID{pid1})

assert.Equal(t, List{p1}, res2)
assert.NoError(t, err2)
}
20 changes: 20 additions & 0 deletions server/pkg/property/loader.go
Expand Up @@ -42,6 +42,26 @@ func LoaderFromMap(data map[ID]*Property) Loader {
}
}

func SchemaLoaderFrom(data ...*Schema) SchemaLoader {
return func(ctx context.Context, ids ...SchemaID) (SchemaList, error) {
res := make([]*Schema, 0, len(ids))
for _, i := range ids {
found := false
for _, d := range data {
if i == d.ID() {
res = append(res, d)
found = true
break
}
}
if !found {
res = append(res, nil)
}
}
return res, nil
}
}

func SchemaLoaderFromMap(data map[SchemaID]*Schema) SchemaLoader {
return func(ctx context.Context, ids ...SchemaID) (SchemaList, error) {
res := make([]*Schema, 0, len(ids))
Expand Down
12 changes: 12 additions & 0 deletions server/pkg/property/loader_test.go
Expand Up @@ -43,6 +43,18 @@ func TestLoaderFromMap(t *testing.T) {
assert.NoError(t, err)
}

func TestSchemaLoaderFrom(t *testing.T) {
pid1 := MustSchemaID("xxx~1.1.1/aa")
pid2 := MustSchemaID("xxx~1.1.1/bb")
p1 := NewSchema().ID(pid1).MustBuild()
p2 := NewSchema().ID(pid2).MustBuild()
pl := SchemaLoaderFrom(p1, p2)
res, err := pl(context.Background(), pid1, pid2)

assert.Equal(t, SchemaList{p1, p2}, res)
assert.NoError(t, err)
}

func TestSchemaLoaderFromMap(t *testing.T) {
psid1 := MustSchemaID("xxx~1.1.1/aa")
psid2 := MustSchemaID("xxx~1.1.1/bb")
Expand Down
3 changes: 2 additions & 1 deletion server/pkg/scene/sceneops/plugin_migrator.go
Expand Up @@ -112,7 +112,8 @@ func (s *PluginMigrator) MigratePlugins(ctx context.Context, sc *scene.Scene, ol
if !f.Plugin().Equal(oldPlugin.ID()) {
continue
}
modifiedLayers.AddUnique(l)

modifiedLayers = modifiedLayers.AddUnique(l)
if newPlugin.Extension(f.Extension()) == nil {
ll.Infobox().Remove(f.ID())
removedPropertyIDs = append(removedPropertyIDs, f.Property())
Expand Down
70 changes: 70 additions & 0 deletions server/pkg/scene/sceneops/plugin_migrator_test.go
@@ -0,0 +1,70 @@
package sceneops

import (
"context"
"testing"

"github.com/reearth/reearth/server/pkg/dataset"
"github.com/reearth/reearth/server/pkg/id"
"github.com/reearth/reearth/server/pkg/layer"
"github.com/reearth/reearth/server/pkg/plugin"
"github.com/reearth/reearth/server/pkg/property"
"github.com/reearth/reearth/server/pkg/scene"
"github.com/stretchr/testify/assert"
)

func TestPluginMigrator_MigratePlugins(t *testing.T) {
assert := assert.New(t)
ctx := context.Background()

sid := scene.NewID()
pid1 := plugin.MustID("plugin~1.0.0")
pid2 := plugin.MustID("plugin~1.0.1")

pl1ps := property.NewSchema().ID(id.NewPropertySchemaID(pid1, "@")).MustBuild()
pl2ps := property.NewSchema().ID(id.NewPropertySchemaID(pid2, "@")).MustBuild()

pl1 := plugin.New().ID(pid1).Schema(pl1ps.ID().Ref()).Extensions([]*plugin.Extension{
plugin.NewExtension().ID("a").Type(plugin.ExtensionTypeBlock).Schema(pl1ps.ID()).MustBuild(),
}).MustBuild()
pl2 := plugin.New().ID(pid2).Schema(pl2ps.ID().Ref()).Extensions([]*plugin.Extension{
plugin.NewExtension().ID("a").Type(plugin.ExtensionTypeBlock).Schema(pl2ps.ID()).MustBuild(),
}).MustBuild()

pl1p := property.New().NewID().Scene(sid).Schema(*pl1.Schema()).MustBuild()
pl2p := property.New().NewID().Scene(sid).Schema(*pl1.Schema()).MustBuild()

ibf1 := layer.NewInfoboxField().NewID().Plugin(plugin.OfficialPluginID).Extension("textblock").Property(id.NewPropertyID()).MustBuild()
ibf2 := layer.NewInfoboxField().NewID().Plugin(pid1).Extension("a").Property(pl2p.ID()).MustBuild()
ib := layer.NewInfobox([]*layer.InfoboxField{ibf1, ibf2}, id.NewPropertyID())
l1 := layer.New().NewID().Plugin(plugin.OfficialPluginID.Ref()).Scene(sid).Infobox(ib).Item().MustBuild()
l2 := layer.New().NewID().Plugin(plugin.OfficialPluginID.Ref()).Scene(sid).Group().Layers(layer.NewIDList([]layer.ID{l1.ID()})).MustBuild()

tid := id.NewWorkspaceID()
sc := scene.New().ID(sid).RootLayer(id.NewLayerID()).Workspace(tid).MustBuild()
sc.Plugins().Add(scene.NewPlugin(pid1, pl1p.ID().Ref()))

pm := PluginMigrator{
Dataset: dataset.LoaderFrom(nil),
Plugin: plugin.LoaderFrom(pl1, pl2),
Property: property.LoaderFrom([]*property.Property{pl1p, pl2p}),
Layer: layer.LoaderBySceneFrom(l1, l2),
PropertySchema: property.SchemaLoaderFrom(pl1ps, pl2ps),
}

result, err := pm.MigratePlugins(ctx, sc, pid1, pid2)
assert.NoError(err)
assert.Equal(MigratePluginsResult{
Scene: sc,
Layers: layer.ListFrom([]layer.Layer{l1}),
Properties: property.List{pl1p, pl2p},
RemovedProperties: []id.PropertyID{},
}, result)

assert.NotNil(l1.Infobox().Field(ibf1.ID()))
assert.Equal(id.OfficialPluginID, l1.Infobox().Field(ibf1.ID()).Plugin())
assert.NotNil(l1.Infobox().Field(ibf2.ID()))
assert.Equal(id.OfficialPluginID, l1.Infobox().Field(ibf1.ID()).Plugin())
assert.Equal(pid2, l1.Infobox().Field(ibf2.ID()).Plugin())
assert.Equal(pid2, pl2p.Schema().Plugin())
}

0 comments on commit d0524ef

Please sign in to comment.