Skip to content
New issue

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

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

Already on GitHub? # to your account

GO-4346: After exporting Templates, they become unlinked #1993

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions core/block/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,10 @@ func (e *exportContext) collectDerivedObjects(objects map[string]*domain.Details
objectsTypes = lo.Union(objectsTypes, []string{objectTypeId})
setOfList := details.GetStringList(bundle.RelationKeySetOf)
setOf = lo.Union(setOf, setOfList)
targetObjectType := details.GetString(bundle.RelationKeyTargetObjectType)
if targetObjectType != "" {
objectsTypes = lo.Union(objectsTypes, []string{targetObjectType})
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this code could be made more elegant, if we present for-cycle by relation keys SetOf, Type and TargetObjectType. In the iteration-body we can try to GetString first and then GetStringList. Or maybe GetStringList is a fallback for these two methods. Need to consult @deff7 🙂

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but setOf is added to another slice, so I think it makes sense to create such for-cycle for targetObjectType and objectType values

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is better to introduce inner field(s) to exportContext struct, so there would be no necessity to return values?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

return nil
})
if err != nil {
Expand Down
140 changes: 120 additions & 20 deletions core/block/export/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,10 @@ func Test_docsForExport(t *testing.T) {
relationKey := domain.RelationKey("key")
storeFixture.AddObjects(t, spaceId, []objectstore.TestObject{
{
bundle.RelationKeyId: domain.String("id"),
domain.RelationKey(relationKey): domain.String("value"),
bundle.RelationKeyType: domain.String("objectType"),
bundle.RelationKeySpaceId: domain.String(spaceId),
bundle.RelationKeyId: domain.String("id"),
relationKey: domain.String("value"),
bundle.RelationKeyType: domain.String("objectType"),
bundle.RelationKeySpaceId: domain.String(spaceId),
},
})
err := storeFixture.SpaceIndex(spaceId).UpdateObjectLinks(context.Background(), "id", []string{"id1"})
Expand Down Expand Up @@ -409,10 +409,10 @@ func Test_docsForExport(t *testing.T) {

storeFixture.AddObjects(t, spaceId, []objectstore.TestObject{
{
bundle.RelationKeyId: domain.String("id"),
domain.RelationKey(relationKey): domain.String("value"),
bundle.RelationKeyType: domain.String("objectType"),
bundle.RelationKeySpaceId: domain.String(spaceId),
bundle.RelationKeyId: domain.String("id"),
relationKey: domain.String("value"),
bundle.RelationKeyType: domain.String("objectType"),
bundle.RelationKeySpaceId: domain.String(spaceId),
},
{
bundle.RelationKeyId: domain.String(relationKey),
Expand Down Expand Up @@ -486,10 +486,10 @@ func Test_docsForExport(t *testing.T) {

storeFixture.AddObjects(t, spaceId, []objectstore.TestObject{
{
bundle.RelationKeyId: domain.String("id"),
domain.RelationKey(relationKey): domain.String("value"),
bundle.RelationKeyType: domain.String("objectType"),
bundle.RelationKeySpaceId: domain.String(spaceId),
bundle.RelationKeyId: domain.String("id"),
relationKey: domain.String("value"),
bundle.RelationKeyType: domain.String("objectType"),
bundle.RelationKeySpaceId: domain.String(spaceId),
},
{
bundle.RelationKeyId: domain.String(relationKey),
Expand Down Expand Up @@ -565,10 +565,10 @@ func Test_docsForExport(t *testing.T) {

storeFixture.AddObjects(t, spaceId, []objectstore.TestObject{
{
bundle.RelationKeyId: domain.String("id"),
domain.RelationKey(relationKey): domain.String(optionId),
bundle.RelationKeyType: domain.String("objectType"),
bundle.RelationKeySpaceId: domain.String(spaceId),
bundle.RelationKeyId: domain.String("id"),
relationKey: domain.String(optionId),
bundle.RelationKeyType: domain.String("objectType"),
bundle.RelationKeySpaceId: domain.String(spaceId),
},
{
bundle.RelationKeyId: domain.String(relationKey),
Expand Down Expand Up @@ -665,10 +665,10 @@ func Test_docsForExport(t *testing.T) {
linkedObjectId := "linkedObjectId"
storeFixture.AddObjects(t, spaceId, []objectstore.TestObject{
{
bundle.RelationKeyId: domain.String("id"),
domain.RelationKey(relationKey): domain.String("test"),
bundle.RelationKeyType: domain.String(objectTypeKey),
bundle.RelationKeySpaceId: domain.String(spaceId),
bundle.RelationKeyId: domain.String("id"),
relationKey: domain.String("test"),
bundle.RelationKeyType: domain.String(objectTypeKey),
bundle.RelationKeySpaceId: domain.String(spaceId),
},
{
bundle.RelationKeyId: domain.String(relationKey),
Expand Down Expand Up @@ -1361,6 +1361,106 @@ func Test_docsForExport(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, 5, len(expCtx.docs))
})
t.Run("export template", func(t *testing.T) {
// given
storeFixture := objectstore.NewStoreFixture(t)
objectTypeKey := "customObjectType"
objectTypeUniqueKey, err := domain.NewUniqueKey(smartblock.SmartBlockTypeObjectType, objectTypeKey)
assert.Nil(t, err)

templateType := "templateType"
templateObjectTypeUniqueKey, err := domain.NewUniqueKey(smartblock.SmartBlockTypeObjectType, templateType)
assert.Nil(t, err)

objectId := "objectId"
storeFixture.AddObjects(t, spaceId, []objectstore.TestObject{
{
bundle.RelationKeyId: domain.String(objectId),
bundle.RelationKeyName: domain.String("template"),
bundle.RelationKeySpaceId: domain.String(spaceId),
bundle.RelationKeyTargetObjectType: domain.String(objectTypeKey),
bundle.RelationKeyType: domain.String(templateType),
},
{
bundle.RelationKeyId: domain.String(objectTypeKey),
bundle.RelationKeyUniqueKey: domain.String(objectTypeUniqueKey.Marshal()),
bundle.RelationKeyLayout: domain.Int64(int64(model.ObjectType_objectType)),
bundle.RelationKeySpaceId: domain.String(spaceId),
bundle.RelationKeyType: domain.String(objectTypeKey),
},
{
bundle.RelationKeyId: domain.String(templateType),
bundle.RelationKeyUniqueKey: domain.String(templateObjectTypeUniqueKey.Marshal()),
bundle.RelationKeyLayout: domain.Int64(int64(model.ObjectType_objectType)),
bundle.RelationKeySpaceId: domain.String(spaceId),
bundle.RelationKeyType: domain.String(objectTypeKey),
},
})

smartBlockTest := smarttest.New(objectId)
doc := smartBlockTest.NewState().SetDetails(domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{
bundle.RelationKeyId: domain.String(objectId),
bundle.RelationKeyType: domain.String(templateType),
bundle.RelationKeyTargetObjectType: domain.String(objectTypeKey),
}))
doc.AddRelationLinks(&model.RelationLink{
Key: bundle.RelationKeyId.String(),
Format: model.RelationFormat_longtext,
})
smartBlockTest.Doc = doc

objectType := smarttest.New(objectTypeKey)
objectTypeDoc := objectType.NewState().SetDetails(domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{
bundle.RelationKeyId: domain.String(objectTypeKey),
bundle.RelationKeyType: domain.String(objectTypeKey),
}))
objectTypeDoc.AddRelationLinks(&model.RelationLink{
Key: bundle.RelationKeyId.String(),
Format: model.RelationFormat_longtext,
}, &model.RelationLink{
Key: bundle.RelationKeyType.String(),
Format: model.RelationFormat_longtext,
})
objectType.Doc = objectTypeDoc

templateTypeObject := smarttest.New(templateType)
templateTypeObjectDoc := objectType.NewState().SetDetails(domain.NewDetailsFromMap(map[domain.RelationKey]domain.Value{
bundle.RelationKeyId: domain.String(templateType),
bundle.RelationKeyType: domain.String(templateType),
}))
templateTypeObjectDoc.AddRelationLinks(&model.RelationLink{
Key: bundle.RelationKeyId.String(),
Format: model.RelationFormat_longtext,
}, &model.RelationLink{
Key: bundle.RelationKeyType.String(),
Format: model.RelationFormat_longtext,
})
templateTypeObject.Doc = templateTypeObjectDoc

objectGetter := mock_cache.NewMockObjectGetter(t)
objectGetter.EXPECT().GetObject(context.Background(), objectId).Return(smartBlockTest, nil)
objectGetter.EXPECT().GetObject(context.Background(), objectTypeKey).Return(objectType, nil)
objectGetter.EXPECT().GetObject(context.Background(), templateType).Return(templateTypeObject, nil)

e := &export{
objectStore: storeFixture,
picker: objectGetter,
}

expCtx := newExportContext(e, pb.RpcObjectListExportRequest{
SpaceId: spaceId,
ObjectIds: []string{objectId},
IncludeNested: false,
Format: model.Export_Protobuf,
})

// when
err = expCtx.docsForExport()

// then
assert.Nil(t, err)
assert.Equal(t, 3, len(expCtx.docs))
})
}

func Test_provideFileName(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion core/block/import/pb/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *SpaceImport) ProvideCollection(snapshots []*common.Snapshot,
}

func (s *SpaceImport) objectShouldBeSkipped(item *common.Snapshot) bool {
return item.Snapshot.SbType == smartblock.SmartBlockTypeSubObject || item.Snapshot.SbType == smartblock.SmartBlockTypeTemplate ||
return item.Snapshot.SbType == smartblock.SmartBlockTypeSubObject ||
item.Snapshot.SbType == smartblock.SmartBlockTypeRelation || item.Snapshot.SbType == smartblock.SmartBlockTypeObjectType ||
item.Snapshot.SbType == smartblock.SmartBlockTypeRelationOption
}
Expand Down
16 changes: 13 additions & 3 deletions core/block/import/pb/space_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestSpaceImport_ProvideCollection(t *testing.T) {
assert.Nil(t, err)
assert.Nil(t, collection)
})
t.Run("no widget object - add all objects (except template and subobjects) in Protobuf Import collection", func(t *testing.T) {
t.Run("no widget object - add all objects (except subobjects) in Protobuf Import collection", func(t *testing.T) {
// given
p := SpaceImport{}
params := &pb.RpcObjectImportRequestPbParams{NoCollection: false}
Expand Down Expand Up @@ -85,9 +85,10 @@ func TestSpaceImport_ProvideCollection(t *testing.T) {
assert.Len(t, collection, 1)
rootCollectionState := state.NewDocFromSnapshot("", collection[0].Snapshot.ToProto()).(*state.State)
objectsInCollection := rootCollectionState.GetStoreSlice(template.CollectionStoreKey)
assert.Len(t, objectsInCollection, 2)
assert.Len(t, objectsInCollection, 3)
assert.Equal(t, objectsInCollection[0], "id1")
assert.Equal(t, objectsInCollection[1], "id4")
assert.Equal(t, objectsInCollection[1], "id3")
assert.Equal(t, objectsInCollection[2], "id4")
})
t.Run("widget with sets - add only sets in Protobuf Import collection", func(t *testing.T) {
// given
Expand All @@ -106,6 +107,9 @@ func TestSpaceImport_ProvideCollection(t *testing.T) {
Id: "id3",
Snapshot: &common.SnapshotModel{
SbType: smartblock2.SmartBlockTypeTemplate,
Data: &common.StateSnapshot{
ObjectTypes: []string{bundle.TypeKeyTemplate.URL()},
},
},
},
// page
Expand Down Expand Up @@ -191,6 +195,9 @@ func TestSpaceImport_ProvideCollection(t *testing.T) {
Id: "id3",
Snapshot: &common.SnapshotModel{
SbType: smartblock2.SmartBlockTypeTemplate,
Data: &common.StateSnapshot{
ObjectTypes: []string{bundle.TypeKeyTemplate.URL()},
},
},
},
// page
Expand Down Expand Up @@ -277,6 +284,9 @@ func TestSpaceImport_ProvideCollection(t *testing.T) {
Id: "id3",
Snapshot: &common.SnapshotModel{
SbType: smartblock2.SmartBlockTypeTemplate,
Data: &common.StateSnapshot{
ObjectTypes: []string{bundle.TypeKeyTemplate.URL()},
},
},
},
// favorite page
Expand Down
Loading