1
0
mirror of https://github.com/mattermost/focalboard.git synced 2025-09-16 08:56:19 +02:00

Replace Block references to use pointers throughout all application layers (#3934)

* Replace Block references to use pointers throughout all application layers

* lint fixes

* gofmt file, lint fix

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Scott Bishel <scott.bishel@mattermost.com>
This commit is contained in:
Miguel de la Cruz
2022-10-25 22:46:43 +02:00
committed by GitHub
parent e3ae682eea
commit 91f9f71bf7
49 changed files with 443 additions and 446 deletions

View File

@@ -118,14 +118,14 @@ type BoardModifier func(board *Board, cache map[string]interface{}) bool
// Return true to import the block or false to skip import.
type BlockModifier func(block *Block, cache map[string]interface{}) bool
func BlocksFromJSON(data io.Reader) []Block {
var blocks []Block
func BlocksFromJSON(data io.Reader) []*Block {
var blocks []*Block
_ = json.NewDecoder(data).Decode(&blocks)
return blocks
}
// LogClone implements the `mlog.LogCloner` interface to provide a subset of Block fields for logging.
func (b Block) LogClone() interface{} {
func (b *Block) LogClone() interface{} {
return struct {
ID string
ParentID string
@@ -199,7 +199,7 @@ type QueryBoardHistoryOptions struct {
Descending bool // if true then the records are sorted by insert_at in descending order
}
func StampModificationMetadata(userID string, blocks []Block, auditRec *audit.Record) {
func StampModificationMetadata(userID string, blocks []*Block, auditRec *audit.Record) {
if userID == SingleUser {
userID = ""
}
@@ -215,15 +215,15 @@ func StampModificationMetadata(userID string, blocks []Block, auditRec *audit.Re
}
}
func (b Block) ShouldBeLimited(cardLimitTimestamp int64) bool {
func (b *Block) ShouldBeLimited(cardLimitTimestamp int64) bool {
return b.Type == TypeCard &&
b.UpdateAt < cardLimitTimestamp
}
// Returns a limited version of the block that doesn't contain the
// contents of the block, only its IDs and type.
func (b Block) GetLimited() Block {
newBlock := Block{
func (b *Block) GetLimited() *Block {
newBlock := &Block{
Title: b.Title,
ID: b.ID,
ParentID: b.ParentID,

View File

@@ -15,7 +15,7 @@ import (
func TestGenerateBlockIDs(t *testing.T) {
t.Run("Should generate a new ID for a single block with no references", func(t *testing.T) {
blockID := utils.NewID(utils.IDTypeBlock)
blocks := []Block{{ID: blockID}}
blocks := []*Block{{ID: blockID}}
blocks = GenerateBlockIDs(blocks, &mlog.Logger{})
@@ -28,7 +28,7 @@ func TestGenerateBlockIDs(t *testing.T) {
blockID := utils.NewID(utils.IDTypeBlock)
boardID := utils.NewID(utils.IDTypeBlock)
parentID := utils.NewID(utils.IDTypeBlock)
blocks := []Block{{ID: blockID, BoardID: boardID, ParentID: parentID}}
blocks := []*Block{{ID: blockID, BoardID: boardID, ParentID: parentID}}
blocks = GenerateBlockIDs(blocks, &mlog.Logger{})
@@ -41,14 +41,14 @@ func TestGenerateBlockIDs(t *testing.T) {
blockID1 := utils.NewID(utils.IDTypeBlock)
boardID1 := utils.NewID(utils.IDTypeBlock)
parentID1 := utils.NewID(utils.IDTypeBlock)
block1 := Block{ID: blockID1, BoardID: boardID1, ParentID: parentID1}
block1 := &Block{ID: blockID1, BoardID: boardID1, ParentID: parentID1}
blockID2 := utils.NewID(utils.IDTypeBlock)
boardID2 := blockID1
parentID2 := utils.NewID(utils.IDTypeBlock)
block2 := Block{ID: blockID2, BoardID: boardID2, ParentID: parentID2}
block2 := &Block{ID: blockID2, BoardID: boardID2, ParentID: parentID2}
blocks := []Block{block1, block2}
blocks := []*Block{block1, block2}
blocks = GenerateBlockIDs(blocks, &mlog.Logger{})
@@ -69,14 +69,14 @@ func TestGenerateBlockIDs(t *testing.T) {
blockID1 := utils.NewID(utils.IDTypeBlock)
boardID1 := ""
parentID1 := utils.NewID(utils.IDTypeBlock)
block1 := Block{ID: blockID1, BoardID: boardID1, ParentID: parentID1}
block1 := &Block{ID: blockID1, BoardID: boardID1, ParentID: parentID1}
blockID2 := utils.NewID(utils.IDTypeBlock)
boardID2 := utils.NewID(utils.IDTypeBlock)
parentID2 := ""
block2 := Block{ID: blockID2, BoardID: boardID2, ParentID: parentID2}
block2 := &Block{ID: blockID2, BoardID: boardID2, ParentID: parentID2}
blocks := []Block{block1, block2}
blocks := []*Block{block1, block2}
blocks = GenerateBlockIDs(blocks, &mlog.Logger{})
@@ -94,28 +94,28 @@ func TestGenerateBlockIDs(t *testing.T) {
blockID1 := utils.NewID(utils.IDTypeBlock)
boardID1 := utils.NewID(utils.IDTypeBlock)
parentID1 := utils.NewID(utils.IDTypeBlock)
block1 := Block{ID: blockID1, BoardID: boardID1, ParentID: parentID1}
block1 := &Block{ID: blockID1, BoardID: boardID1, ParentID: parentID1}
// linked to 1
blockID2 := utils.NewID(utils.IDTypeBlock)
boardID2 := blockID1
parentID2 := utils.NewID(utils.IDTypeBlock)
block2 := Block{ID: blockID2, BoardID: boardID2, ParentID: parentID2}
block2 := &Block{ID: blockID2, BoardID: boardID2, ParentID: parentID2}
// linked to 2
blockID3 := utils.NewID(utils.IDTypeBlock)
boardID3 := blockID2
parentID3 := utils.NewID(utils.IDTypeBlock)
block3 := Block{ID: blockID3, BoardID: boardID3, ParentID: parentID3}
block3 := &Block{ID: blockID3, BoardID: boardID3, ParentID: parentID3}
// linked to 1
blockID4 := utils.NewID(utils.IDTypeBlock)
boardID4 := blockID1
parentID4 := utils.NewID(utils.IDTypeBlock)
block4 := Block{ID: blockID4, BoardID: boardID4, ParentID: parentID4}
block4 := &Block{ID: blockID4, BoardID: boardID4, ParentID: parentID4}
// blocks are shuffled
blocks := []Block{block4, block2, block1, block3}
blocks := []*Block{block4, block2, block1, block3}
blocks = GenerateBlockIDs(blocks, &mlog.Logger{})
@@ -147,7 +147,7 @@ func TestGenerateBlockIDs(t *testing.T) {
blockID1 := utils.NewID(utils.IDTypeBlock)
boardID1 := utils.NewID(utils.IDTypeBlock)
parentID1 := utils.NewID(utils.IDTypeBlock)
block1 := Block{
block1 := &Block{
ID: blockID1,
BoardID: boardID1,
ParentID: parentID1,
@@ -156,7 +156,7 @@ func TestGenerateBlockIDs(t *testing.T) {
blockID2 := utils.NewID(utils.IDTypeBlock)
boardID2 := utils.NewID(utils.IDTypeBlock)
parentID2 := utils.NewID(utils.IDTypeBlock)
block2 := Block{
block2 := &Block{
ID: blockID2,
BoardID: boardID2,
ParentID: parentID2,
@@ -167,7 +167,7 @@ func TestGenerateBlockIDs(t *testing.T) {
},
}
blocks := []Block{block1, block2}
blocks := []*Block{block1, block2}
blocks = GenerateBlockIDs(blocks, &mlog.Logger{})
@@ -191,21 +191,21 @@ func TestGenerateBlockIDs(t *testing.T) {
blockID1 := utils.NewID(utils.IDTypeBlock)
boardID1 := utils.NewID(utils.IDTypeBlock)
parentID1 := utils.NewID(utils.IDTypeBlock)
block1 := Block{
block1 := &Block{
ID: blockID1,
BoardID: boardID1,
ParentID: parentID1,
}
blockID2 := utils.NewID(utils.IDTypeBlock)
block2 := Block{
block2 := &Block{
ID: blockID2,
BoardID: boardID1,
ParentID: parentID1,
}
blockID3 := utils.NewID(utils.IDTypeBlock)
block3 := Block{
block3 := &Block{
ID: blockID3,
BoardID: boardID1,
ParentID: parentID1,
@@ -215,7 +215,7 @@ func TestGenerateBlockIDs(t *testing.T) {
boardID2 := utils.NewID(utils.IDTypeBlock)
parentID2 := utils.NewID(utils.IDTypeBlock)
block4 := Block{
block4 := &Block{
ID: blockID4,
BoardID: boardID2,
ParentID: parentID2,
@@ -230,7 +230,7 @@ func TestGenerateBlockIDs(t *testing.T) {
},
}
blocks := []Block{block1, block2, block3, block4}
blocks := []*Block{block1, block2, block3, block4}
blocks = GenerateBlockIDs(blocks, &mlog.Logger{})
@@ -258,7 +258,7 @@ func TestGenerateBlockIDs(t *testing.T) {
blockID1 := utils.NewID(utils.IDTypeBlock)
boardID1 := utils.NewID(utils.IDTypeBlock)
parentID1 := utils.NewID(utils.IDTypeBlock)
block1 := Block{
block1 := &Block{
ID: blockID1,
BoardID: boardID1,
ParentID: parentID1,
@@ -267,7 +267,7 @@ func TestGenerateBlockIDs(t *testing.T) {
blockID2 := utils.NewID(utils.IDTypeBlock)
boardID2 := utils.NewID(utils.IDTypeBlock)
parentID2 := utils.NewID(utils.IDTypeBlock)
block2 := Block{
block2 := &Block{
ID: blockID2,
BoardID: boardID2,
ParentID: parentID2,
@@ -276,7 +276,7 @@ func TestGenerateBlockIDs(t *testing.T) {
},
}
blocks := []Block{block1, block2}
blocks := []*Block{block1, block2}
blocks = GenerateBlockIDs(blocks, &mlog.Logger{})
@@ -297,8 +297,8 @@ func TestGenerateBlockIDs(t *testing.T) {
func TestStampModificationMetadata(t *testing.T) {
t.Run("base case", func(t *testing.T) {
block := Block{}
blocks := []Block{block}
block := &Block{}
blocks := []*Block{block}
assert.Empty(t, block.ModifiedBy)
assert.Empty(t, block.UpdateAt)

View File

@@ -12,7 +12,7 @@ import (
// keeping consistent any references that other blocks would made to
// the original IDs, so a tree of blocks can get new IDs and maintain
// its shape.
func GenerateBlockIDs(blocks []Block, logger mlog.LoggerIFace) []Block {
func GenerateBlockIDs(blocks []*Block, logger mlog.LoggerIFace) []*Block {
blockIDs := map[string]BlockType{}
referenceIDs := map[string]bool{}
for _, block := range blocks {
@@ -93,7 +93,7 @@ func GenerateBlockIDs(blocks []Block, logger mlog.LoggerIFace) []Block {
return utils.NewID(BlockType2IDType(blockIDs[id]))
}
newBlocks := make([]Block, len(blocks))
newBlocks := make([]*Block, len(blocks))
for i, block := range blocks {
block.ID = getExistingOrNewID(block.ID)
block.BoardID = getExistingOrOldID(block.BoardID)
@@ -101,11 +101,11 @@ func GenerateBlockIDs(blocks []Block, logger mlog.LoggerIFace) []Block {
blockMod := block
if _, ok := blockMod.Fields["contentOrder"]; ok {
fixFieldIDs(&blockMod, "contentOrder", getExistingOrOldID, logger)
fixFieldIDs(blockMod, "contentOrder", getExistingOrOldID, logger)
}
if _, ok := blockMod.Fields["cardOrder"]; ok {
fixFieldIDs(&blockMod, "cardOrder", getExistingOrOldID, logger)
fixFieldIDs(blockMod, "cardOrder", getExistingOrOldID, logger)
}
if _, ok := blockMod.Fields["defaultTemplateId"]; ok {

View File

@@ -35,7 +35,7 @@ type BoardsAndBlocks struct {
// The blocks
// required: false
Blocks []Block `json:"blocks"`
Blocks []*Block `json:"blocks"`
}
func (bab *BoardsAndBlocks) IsValid() error {
@@ -139,13 +139,13 @@ func GenerateBoardsAndBlocksIDs(bab *BoardsAndBlocks, logger mlog.LoggerIFace) (
return nil, err
}
blocksByBoard := map[string][]Block{}
blocksByBoard := map[string][]*Block{}
for _, block := range bab.Blocks {
blocksByBoard[block.BoardID] = append(blocksByBoard[block.BoardID], block)
}
boards := []*Board{}
blocks := []Block{}
blocks := []*Block{}
for _, board := range bab.Boards {
newID := utils.NewID(utils.IDTypeBoard)
for _, block := range blocksByBoard[board.ID] {

View File

@@ -11,7 +11,7 @@ import (
func TestIsValidBoardsAndBlocks(t *testing.T) {
t.Run("no boards", func(t *testing.T) {
bab := &BoardsAndBlocks{
Blocks: []Block{
Blocks: []*Block{
{ID: "block-id-1", BoardID: "board-id-1", Type: TypeCard},
{ID: "block-id-2", BoardID: "board-id-2", Type: TypeCard},
},
@@ -37,7 +37,7 @@ func TestIsValidBoardsAndBlocks(t *testing.T) {
{ID: "board-id-1", Type: BoardTypeOpen},
{ID: "board-id-2", Type: BoardTypePrivate},
},
Blocks: []Block{
Blocks: []*Block{
{ID: "block-id-1", BoardID: "board-id-1", Type: TypeCard},
{ID: "block-id-3", BoardID: "board-id-3", Type: TypeCard},
{ID: "block-id-2", BoardID: "board-id-2", Type: TypeCard},
@@ -53,7 +53,7 @@ func TestIsValidBoardsAndBlocks(t *testing.T) {
{ID: "board-id-1", Type: BoardTypeOpen},
{ID: "board-id-2", Type: BoardTypePrivate},
},
Blocks: []Block{
Blocks: []*Block{
{ID: "block-id-1", BoardID: "board-id-1", Type: TypeCard},
{ID: "block-id-3", BoardID: "board-id-2", Type: TypeCard},
{ID: "block-id-2", BoardID: "board-id-2", Type: TypeCard},
@@ -68,13 +68,13 @@ func TestGenerateBoardsAndBlocksIDs(t *testing.T) {
logger, err := mlog.NewLogger()
require.NoError(t, err)
getBlockByType := func(blocks []Block, blockType BlockType) Block {
getBlockByType := func(blocks []*Block, blockType BlockType) *Block {
for _, b := range blocks {
if b.Type == blockType {
return b
}
}
return Block{}
return &Block{}
}
getBoardByTitle := func(boards []*Board, title string) *Board {
@@ -88,7 +88,7 @@ func TestGenerateBoardsAndBlocksIDs(t *testing.T) {
t.Run("invalid boards and blocks", func(t *testing.T) {
bab := &BoardsAndBlocks{
Blocks: []Block{
Blocks: []*Block{
{ID: "block-id-1", BoardID: "board-id-1", Type: TypeCard},
{ID: "block-id-2", BoardID: "board-id-2", Type: TypeCard},
},
@@ -106,7 +106,7 @@ func TestGenerateBoardsAndBlocksIDs(t *testing.T) {
{ID: "board-id-2", Type: BoardTypePrivate, Title: "board2"},
{ID: "board-id-3", Type: BoardTypeOpen, Title: "board3"},
},
Blocks: []Block{
Blocks: []*Block{
{ID: "block-id-1", BoardID: "board-id-1", Type: TypeCard},
{ID: "block-id-2", BoardID: "board-id-2", Type: TypeView},
{ID: "block-id-3", BoardID: "board-id-2", Type: TypeText},