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

refactor: pkg/id, use ID aliases, move JSON schemas #97

Merged
merged 5 commits into from
Jan 14, 2022
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
8 changes: 4 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"yaml.validate": true,
"yaml.hover": true,
"yaml.schemas": {
"./plugin_manifest_schema.json": [
"./schemas/plugin_manifest.json": [
"/pkg/builtin/manifest.yml"
],
"./plugin_manifest_schema_translation.json": [
"./schemas/plugin_manifest_translation.json": [
"/pkg/builtin/manifest_*.yml"
]
},
Expand All @@ -18,13 +18,13 @@
"fileMatch": [
"/pkg/builtin/manifest.json"
],
"url": "./plugin_manifest_schema.json"
"url": "./schemas/plugin_manifest.json"
},
{
"fileMatch": [
"/pkg/builtin/manifest_*.json"
],
"url": "./plugin_manifest_schema_translation.json"
"url": "./schemas/plugin_manifest_translation.json"
}
]
}
1 change: 0 additions & 1 deletion internal/infrastructure/memory/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/reearth/reearth-backend/internal/usecase/repo"
)

// InitRepos _
func InitRepos(c *repo.Container) *repo.Container {
if c == nil {
c = &repo.Container{}
Expand Down
7 changes: 3 additions & 4 deletions internal/infrastructure/memory/scene_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"context"
"sync"

"github.com/reearth/reearth-backend/internal/usecase/repo"
"github.com/reearth/reearth-backend/pkg/id"
"github.com/reearth/reearth-backend/pkg/scene"

"github.com/reearth/reearth-backend/internal/usecase/repo"
)

type sceneLock struct {
Expand All @@ -19,7 +18,7 @@ func NewSceneLock() repo.SceneLock {
}

func (r *sceneLock) GetLock(ctx context.Context, sceneID id.SceneID) (scene.LockMode, error) {
if id.ID(sceneID).IsNil() {
if sceneID.IsNil() {
return "", id.ErrInvalidID
}
if v, ok := r.lock.Load(sceneID); ok {
Expand All @@ -33,7 +32,7 @@ func (r *sceneLock) GetLock(ctx context.Context, sceneID id.SceneID) (scene.Lock
func (r *sceneLock) GetAllLock(ctx context.Context, sceneID []id.SceneID) ([]scene.LockMode, error) {
res := make([]scene.LockMode, 0, len(sceneID))
for _, si := range sceneID {
if id.ID(si).IsNil() {
if si.IsNil() {
return nil, id.ErrInvalidID
}
if v, ok := r.lock.Load(si); ok {
Expand Down
4 changes: 2 additions & 2 deletions internal/infrastructure/mongo/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (r *assetRepo) FindByID(ctx context.Context, id id.AssetID, teams []id.Team

func (r *assetRepo) FindByIDs(ctx context.Context, ids []id.AssetID, teams []id.TeamID) ([]*asset.Asset, error) {
filter := assetFilter(bson.M{
"id": bson.M{"$in": id.AssetIDToKeys(ids)},
"id": bson.M{"$in": id.AssetIDsToStrings(ids)},
}, teams)
dst := make([]*asset.Asset, 0, len(ids))
res, err := r.find(ctx, dst, filter)
Expand Down Expand Up @@ -112,6 +112,6 @@ func filterAssets(ids []id.AssetID, rows []*asset.Asset) []*asset.Asset {
}

func assetFilter(filter bson.M, teams []id.TeamID) bson.M {
filter["team"] = bson.M{"$in": id.TeamIDToKeys(teams)}
filter["team"] = bson.M{"$in": id.TeamIDsToStrings(teams)}
return filter
}
10 changes: 5 additions & 5 deletions internal/infrastructure/mongo/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (r *datasetRepo) FindByID(ctx context.Context, id2 id.DatasetID, f []id.Sce
func (r *datasetRepo) FindByIDs(ctx context.Context, ids []id.DatasetID, f []id.SceneID) (dataset.List, error) {
filter := r.sceneFilter(bson.D{
{Key: "id", Value: bson.D{
{Key: "$in", Value: id.DatasetIDToKeys(ids)},
{Key: "$in", Value: id.DatasetIDsToStrings(ids)},
}},
}, f)
dst := make([]*dataset.Dataset, 0, len(ids))
Expand Down Expand Up @@ -75,14 +75,14 @@ func (r *datasetRepo) FindGraph(ctx context.Context, did id.DatasetID, f []id.Sc
return dataset.List{d}, nil
}

fieldsstr := id.DatasetSchemaFieldIDToKeys(fields)
fieldsstr := id.DatasetSchemaFieldIDsToStrings(fields)
firstField := fieldsstr[0]

aggfilter := bson.D{}
if f != nil {
aggfilter = append(aggfilter, bson.E{Key: "$in", Value: []interface{}{
"$$g.scene",
id.SceneIDToKeys(f),
id.SceneIDsToStrings(f),
}})
}

Expand Down Expand Up @@ -271,7 +271,7 @@ func (r *datasetRepo) RemoveAll(ctx context.Context, ids []id.DatasetID) error {
if len(ids) == 0 {
return nil
}
return r.client.RemoveAll(ctx, id.DatasetIDToKeys(ids))
return r.client.RemoveAll(ctx, id.DatasetIDsToStrings(ids))
}

func (r *datasetRepo) RemoveByScene(ctx context.Context, sceneID id.SceneID) error {
Expand Down Expand Up @@ -336,7 +336,7 @@ func (*datasetRepo) sceneFilter(filter bson.D, scenes []id.SceneID) bson.D {
}
filter = append(filter, bson.E{
Key: "scene",
Value: bson.D{{Key: "$in", Value: id.SceneIDToKeys(scenes)}},
Value: bson.D{{Key: "$in", Value: id.SceneIDsToStrings(scenes)}},
})
return filter
}
6 changes: 3 additions & 3 deletions internal/infrastructure/mongo/dataset_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (r *datasetSchemaRepo) FindByID(ctx context.Context, id2 id.DatasetSchemaID
func (r *datasetSchemaRepo) FindByIDs(ctx context.Context, ids []id.DatasetSchemaID, f []id.SceneID) (dataset.SchemaList, error) {
filter := r.sceneFilter(bson.D{
{Key: "id", Value: bson.D{
{Key: "$in", Value: id.DatasetSchemaIDToKeys(ids)},
{Key: "$in", Value: id.DatasetSchemaIDsToStrings(ids)},
}},
}, f)
dst := make([]*dataset.Schema, 0, len(ids))
Expand Down Expand Up @@ -111,7 +111,7 @@ func (r *datasetSchemaRepo) RemoveAll(ctx context.Context, ids []id.DatasetSchem
if len(ids) == 0 {
return nil
}
return r.client.RemoveAll(ctx, id.DatasetSchemaIDToKeys(ids))
return r.client.RemoveAll(ctx, id.DatasetSchemaIDsToStrings(ids))
}

func (r *datasetSchemaRepo) RemoveByScene(ctx context.Context, sceneID id.SceneID) error {
Expand Down Expand Up @@ -176,7 +176,7 @@ func (*datasetSchemaRepo) sceneFilter(filter bson.D, scenes []id.SceneID) bson.D
}
filter = append(filter, bson.E{
Key: "scene",
Value: bson.D{{Key: "$in", Value: id.SceneIDToKeys(scenes)}},
Value: bson.D{{Key: "$in", Value: id.SceneIDsToStrings(scenes)}},
})
return filter
}
14 changes: 7 additions & 7 deletions internal/infrastructure/mongo/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (r *layerRepo) FindByID(ctx context.Context, id id.LayerID, f []id.SceneID)
func (r *layerRepo) FindByIDs(ctx context.Context, ids []id.LayerID, f []id.SceneID) (layer.List, error) {
filter := r.sceneFilterD(bson.D{
{Key: "id", Value: bson.D{
{Key: "$in", Value: id.LayerIDToKeys(ids)},
{Key: "$in", Value: id.LayerIDsToStrings(ids)},
}},
}, f)
dst := make([]*layer.Layer, 0, len(ids))
Expand Down Expand Up @@ -68,7 +68,7 @@ func (r *layerRepo) FindItemByID(ctx context.Context, id id.LayerID, f []id.Scen
func (r *layerRepo) FindItemByIDs(ctx context.Context, ids []id.LayerID, f []id.SceneID) (layer.ItemList, error) {
filter := r.sceneFilterD(bson.D{
{Key: "id", Value: bson.D{
{Key: "$in", Value: id.LayerIDToKeys(ids)},
{Key: "$in", Value: id.LayerIDsToStrings(ids)},
}},
}, f)
dst := make([]*layer.Item, 0, len(ids))
Expand All @@ -89,7 +89,7 @@ func (r *layerRepo) FindGroupByID(ctx context.Context, id id.LayerID, f []id.Sce
func (r *layerRepo) FindGroupByIDs(ctx context.Context, ids []id.LayerID, f []id.SceneID) (layer.GroupList, error) {
filter := r.sceneFilterD(bson.D{
{Key: "id", Value: bson.D{
{Key: "$in", Value: id.LayerIDToKeys(ids)},
{Key: "$in", Value: id.LayerIDsToStrings(ids)},
}},
}, f)
dst := make([]*layer.Group, 0, len(ids))
Expand Down Expand Up @@ -154,7 +154,7 @@ func (r *layerRepo) RemoveAll(ctx context.Context, ids []id.LayerID) error {
if len(ids) == 0 {
return nil
}
return r.client.RemoveAll(ctx, id.LayerIDToKeys(ids))
return r.client.RemoveAll(ctx, id.LayerIDsToStrings(ids))
}

func (r *layerRepo) RemoveByScene(ctx context.Context, sceneID id.SceneID) error {
Expand All @@ -170,7 +170,7 @@ func (r *layerRepo) RemoveByScene(ctx context.Context, sceneID id.SceneID) error

func (r *layerRepo) FindByTag(ctx context.Context, tagID id.TagID, f []id.SceneID) (layer.List, error) {
ids := []id.TagID{tagID}
tags := id.TagIDToKeys(ids)
tags := id.TagIDsToStrings(ids)
filter := r.sceneFilter(bson.M{
"$or": []bson.M{
{"tags.id": bson.M{"$in": tags}},
Expand Down Expand Up @@ -331,7 +331,7 @@ func (*layerRepo) sceneFilterD(filter bson.D, scenes []id.SceneID) bson.D {
}
filter = append(filter, bson.E{
Key: "scene",
Value: bson.D{{Key: "$in", Value: id.SceneIDToKeys(scenes)}},
Value: bson.D{{Key: "$in", Value: id.SceneIDsToStrings(scenes)}},
})
return filter
}
Expand All @@ -340,6 +340,6 @@ func (*layerRepo) sceneFilter(filter bson.M, scenes []id.SceneID) bson.M {
if scenes == nil {
return filter
}
filter["scene"] = bson.M{"$in": id.SceneIDToKeys(scenes)}
filter["scene"] = bson.M{"$in": id.SceneIDsToStrings(scenes)}
return filter
}
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/mongodoc/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func NewLayer(l layer.Layer) (*LayerDocument, string) {

if lg := layer.GroupFromLayer(l); lg != nil {
group = &LayerGroupDocument{
Layers: id.LayerIDToKeys(lg.Layers().Layers()),
Layers: id.LayerIDsToStrings(lg.Layers().Layers()),
LinkedDatasetSchema: lg.LinkedDatasetSchema().StringRef(),
Root: lg.IsRoot(),
}
Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/mongodoc/scene_align.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func NewWidgetArea(a *scene.WidgetArea) *WidgetAreaDocument {
}

return &WidgetAreaDocument{
WidgetIDs: id.WidgetIDToKeys(a.WidgetIDs()),
WidgetIDs: id.WidgetIDsToStrings(a.WidgetIDs()),
Align: string(a.Alignment()),
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/mongodoc/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewTag(t tag.Tag) (*TagDocument, string) {
ids := tags.Tags()

group = &TagGroupDocument{
Tags: id.TagIDToKeys(ids),
Tags: id.TagIDsToStrings(ids),
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/infrastructure/mongo/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (r *pluginRepo) FindByID(ctx context.Context, pid id.PluginID, sids []id.Sc
{
"id": pids,
"scene": bson.M{
"$in": id.SceneIDToKeys(sids),
"$in": id.SceneIDsToStrings(sids),
},
},
},
Expand All @@ -76,7 +76,7 @@ func (r *pluginRepo) FindByIDs(ctx context.Context, ids []id.PluginID, sids []id
var err error

if len(ids2) > 0 {
keys := id.PluginIDToKeys(ids2)
keys := id.PluginIDsToStrings(ids2)
filter := bson.M{
"$or": []bson.M{
{
Expand All @@ -90,7 +90,7 @@ func (r *pluginRepo) FindByIDs(ctx context.Context, ids []id.PluginID, sids []id
{
"id": bson.M{"$in": keys},
"scene": bson.M{
"$in": id.SceneIDToKeys(sids),
"$in": id.SceneIDsToStrings(sids),
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions internal/infrastructure/mongo/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (r *projectRepo) init() {
func (r *projectRepo) FindByIDs(ctx context.Context, ids []id.ProjectID, f []id.TeamID) ([]*project.Project, error) {
filter := r.teamFilter(bson.D{
{Key: "id", Value: bson.D{
{Key: "$in", Value: id.ProjectIDToKeys(ids)},
{Key: "$in", Value: id.ProjectIDsToStrings(ids)},
}},
}, f)
dst := make([]*project.Project, 0, len(ids))
Expand Down Expand Up @@ -143,7 +143,7 @@ func (*projectRepo) teamFilter(filter bson.D, teams []id.TeamID) bson.D {
}
filter = append(filter, bson.E{
Key: "team",
Value: bson.D{{Key: "$in", Value: id.TeamIDToKeys(teams)}},
Value: bson.D{{Key: "$in", Value: id.TeamIDsToStrings(teams)}},
})
return filter
}
6 changes: 3 additions & 3 deletions internal/infrastructure/mongo/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (r *propertyRepo) FindByID(ctx context.Context, id2 id.PropertyID, f []id.S

func (r *propertyRepo) FindByIDs(ctx context.Context, ids []id.PropertyID, f []id.SceneID) (property.List, error) {
filter := r.sceneFilter(bson.D{{Key: "id", Value: bson.D{{
Key: "$in", Value: id.PropertyIDToKeys(ids),
Key: "$in", Value: id.PropertyIDsToStrings(ids),
}}}}, f)
dst := make(property.List, 0, len(ids))
res, err := r.find(ctx, dst, filter)
Expand Down Expand Up @@ -99,7 +99,7 @@ func (r *propertyRepo) RemoveAll(ctx context.Context, ids []id.PropertyID) error
if len(ids) == 0 {
return nil
}
return r.client.RemoveAll(ctx, id.PropertyIDToKeys(ids))
return r.client.RemoveAll(ctx, id.PropertyIDsToStrings(ids))
}

func (r *propertyRepo) RemoveByScene(ctx context.Context, sceneID id.SceneID) error {
Expand Down Expand Up @@ -164,7 +164,7 @@ func (*propertyRepo) sceneFilter(filter bson.D, scenes []id.SceneID) bson.D {
}
filter = append(filter, bson.E{
Key: "scene",
Value: bson.D{{Key: "$in", Value: id.SceneIDToKeys(scenes)}},
Value: bson.D{{Key: "$in", Value: id.SceneIDsToStrings(scenes)}},
})
return filter
}
4 changes: 2 additions & 2 deletions internal/infrastructure/mongo/property_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (r *propertySchemaRepo) FindByIDs(ctx context.Context, ids []id.PropertySch

if len(ids2) > 0 {
filter := bson.D{{Key: "id", Value: bson.D{{
Key: "$in", Value: id.PropertySchemaIDToKeys(ids2),
Key: "$in", Value: id.PropertySchemaIDsToStrings(ids2),
}}}}
dst := make(property.SchemaList, 0, len(ids2))
res, err = r.find(ctx, dst, filter)
Expand Down Expand Up @@ -120,7 +120,7 @@ func (r *propertySchemaRepo) RemoveAll(ctx context.Context, ids []id.PropertySch
if len(ids) == 0 {
return nil
}
return r.client.RemoveAll(ctx, id.PropertySchemaIDToKeys(ids))
return r.client.RemoveAll(ctx, id.PropertySchemaIDsToStrings(ids))
}

func (r *propertySchemaRepo) find(ctx context.Context, dst property.SchemaList, filter bson.D) (property.SchemaList, error) {
Expand Down
12 changes: 6 additions & 6 deletions internal/infrastructure/mongo/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (r *sceneRepo) FindByID(ctx context.Context, id id.SceneID, f []id.TeamID)
func (r *sceneRepo) FindByIDs(ctx context.Context, ids []id.SceneID, f []id.TeamID) ([]*scene.Scene, error) {
filter := r.teamFilter(bson.D{
{Key: "id", Value: bson.D{
{Key: "$in", Value: id.SceneIDToKeys(ids)},
{Key: "$in", Value: id.SceneIDsToStrings(ids)},
}},
}, f)
dst := make([]*scene.Scene, 0, len(ids))
Expand All @@ -62,7 +62,7 @@ func (r *sceneRepo) FindByProject(ctx context.Context, id id.ProjectID, f []id.T
func (r *sceneRepo) FindIDsByTeam(ctx context.Context, teams []id.TeamID) ([]id.SceneID, error) {
filter := bson.D{
{Key: "team", Value: bson.D{
{Key: "$in", Value: id.TeamIDToKeys(teams)},
{Key: "$in", Value: id.TeamIDsToStrings(teams)},
}},
}
c := mongodoc.SceneIDConsumer{
Expand All @@ -79,7 +79,7 @@ func (r *sceneRepo) FindIDsByTeam(ctx context.Context, teams []id.TeamID) ([]id.
func (r *sceneRepo) HasSceneTeam(ctx context.Context, sceneID id.SceneID, temaIDs []id.TeamID) (bool, error) {
filter := bson.D{
{Key: "id", Value: sceneID.String()},
{Key: "team", Value: bson.D{{Key: "$in", Value: id.TeamIDToKeys(temaIDs)}}},
{Key: "team", Value: bson.D{{Key: "$in", Value: id.TeamIDsToStrings(temaIDs)}}},
}
res, err2 := r.client.Collection().CountDocuments(ctx, filter)
if err2 != nil {
Expand All @@ -90,8 +90,8 @@ func (r *sceneRepo) HasSceneTeam(ctx context.Context, sceneID id.SceneID, temaID

func (r *sceneRepo) HasScenesTeam(ctx context.Context, sceneIDs []id.SceneID, teamIDs []id.TeamID) ([]bool, error) {
cursor, err2 := r.client.Collection().Find(ctx, bson.D{
{Key: "id", Value: bson.D{{Key: "$in", Value: id.SceneIDToKeys(sceneIDs)}}},
{Key: "team", Value: bson.D{{Key: "$in", Value: id.TeamIDToKeys(teamIDs)}}},
{Key: "id", Value: bson.D{{Key: "$in", Value: id.SceneIDsToStrings(sceneIDs)}}},
{Key: "team", Value: bson.D{{Key: "$in", Value: id.TeamIDsToStrings(teamIDs)}}},
}, &options.FindOptions{
Projection: bson.D{{Key: "id", Value: 1}, {Key: "_id", Value: 0}},
})
Expand Down Expand Up @@ -181,7 +181,7 @@ func (*sceneRepo) teamFilter(filter bson.D, teams []id.TeamID) bson.D {
}
filter = append(filter, bson.E{
Key: "team",
Value: bson.D{{Key: "$in", Value: id.TeamIDToKeys(teams)}},
Value: bson.D{{Key: "$in", Value: id.TeamIDsToStrings(teams)}},
})
return filter
}
2 changes: 1 addition & 1 deletion internal/infrastructure/mongo/scene_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (r *sceneLockRepo) GetLock(ctx context.Context, sceneID id.SceneID) (scene.
func (r *sceneLockRepo) GetAllLock(ctx context.Context, ids []id.SceneID) ([]scene.LockMode, error) {
filter := bson.D{
{Key: "scene", Value: bson.D{
{Key: "$in", Value: id.SceneIDToKeys(ids)},
{Key: "$in", Value: id.SceneIDsToStrings(ids)},
}},
}
c := mongodoc.SceneLockConsumer{
Expand Down
Loading