Fixing unique ID problems on content order with multiple colums (#2071)

* Fixing unique ID problems on content order with multiple colums

* Fixing duplication

* Fix linter errors
This commit is contained in:
Jesús Espino 2022-01-11 21:25:51 +01:00 committed by GitHub
parent de3f7f6103
commit 106a8e5b2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 2 deletions

View file

@ -217,7 +217,15 @@ func GenerateBlockIDs(blocks []Block, logger *mlog.Logger) []Block {
}
for _, blockID := range contentOrder {
referenceIDs[blockID.(string)] = true
switch v := blockID.(type) {
case []interface{}:
for _, columnBlockID := range v {
referenceIDs[columnBlockID.(string)] = true
}
case string:
referenceIDs[v] = true
default:
}
}
}
}
@ -264,7 +272,15 @@ func GenerateBlockIDs(blocks []Block, logger *mlog.Logger) []Block {
)
} else {
for j := range contentOrder {
contentOrder[j] = getExistingOrOldID(contentOrder[j].(string))
switch v := contentOrder[j].(type) {
case string:
contentOrder[j] = getExistingOrOldID(v)
case []interface{}:
subOrder := contentOrder[j].([]interface{})
for k := range v {
subOrder[k] = getExistingOrOldID(v[k].(string))
}
}
}
}
}

View file

@ -184,4 +184,71 @@ func TestGenerateBlockIDs(t *testing.T) {
require.NotEqual(t, blockID1, block2ContentOrder[0].(string))
require.Equal(t, blocks[0].ID, block2ContentOrder[0].(string))
})
t.Run("Should update content order when it contain slices", func(t *testing.T) {
blockID1 := utils.NewID(utils.IDTypeBlock)
rootID1 := utils.NewID(utils.IDTypeBlock)
parentID1 := utils.NewID(utils.IDTypeBlock)
block1 := Block{
ID: blockID1,
RootID: rootID1,
ParentID: parentID1,
}
blockID2 := utils.NewID(utils.IDTypeBlock)
block2 := Block{
ID: blockID2,
RootID: rootID1,
ParentID: parentID1,
}
blockID3 := utils.NewID(utils.IDTypeBlock)
block3 := Block{
ID: blockID3,
RootID: rootID1,
ParentID: parentID1,
}
blockID4 := utils.NewID(utils.IDTypeBlock)
rootID2 := utils.NewID(utils.IDTypeBlock)
parentID2 := utils.NewID(utils.IDTypeBlock)
block4 := Block{
ID: blockID4,
RootID: rootID2,
ParentID: parentID2,
Fields: map[string]interface{}{
"contentOrder": []interface{}{
blockID1,
[]interface{}{
blockID2,
blockID3,
},
},
},
}
blocks := []Block{block1, block2, block3, block4}
blocks = GenerateBlockIDs(blocks, &mlog.Logger{})
require.NotEqual(t, blockID1, blocks[0].ID)
require.Equal(t, rootID1, blocks[0].RootID)
require.Equal(t, parentID1, blocks[0].ParentID)
require.NotEqual(t, blockID4, blocks[3].ID)
require.Equal(t, rootID2, blocks[3].RootID)
require.Equal(t, parentID2, blocks[3].ParentID)
// since block 1 was referenced in block 2,
// the ID should have been changed in content order
block4ContentOrder, ok := block4.Fields["contentOrder"].([]interface{})
require.True(t, ok)
require.NotEqual(t, blockID1, block4ContentOrder[0].(string))
require.NotEqual(t, blockID2, block4ContentOrder[1].([]interface{})[0])
require.NotEqual(t, blockID3, block4ContentOrder[1].([]interface{})[1])
require.Equal(t, blocks[0].ID, block4ContentOrder[0].(string))
require.Equal(t, blocks[1].ID, block4ContentOrder[1].([]interface{})[0])
require.Equal(t, blocks[2].ID, block4ContentOrder[1].([]interface{})[1])
})
}