[MM-28985] Remove pointers to slice (part 1) (#18034)

* Remove pointers to slice (part 1)

* Remove use of pointers to slice from model package (#18045)

* Fix after merge
Этот коммит содержится в:
Claudio Costa
2021-08-17 11:18:33 +02:00
коммит произвёл GitHub
родитель 132f114793
Коммит 04b27ce93c
64 изменённых файлов: 767 добавлений и 771 удалений

Просмотреть файл

@@ -250,14 +250,14 @@ func (db channelMemberWithSchemeRoles) ToModel() *model.ChannelMember {
}
}
func (db channelMemberWithSchemeRolesList) ToModel() *model.ChannelMembers {
func (db channelMemberWithSchemeRolesList) ToModel() model.ChannelMembers {
cms := model.ChannelMembers{}
for _, cm := range db {
cms = append(cms, *cm.ToModel())
}
return &cms
return cms
}
type allChannelMember struct {
@@ -945,7 +945,7 @@ func (s SqlChannelStore) PermanentDeleteMembersByChannel(channelId string) error
return nil
}
func (s SqlChannelStore) GetChannels(teamId string, userId string, includeDeleted bool, lastDeleteAt int) (*model.ChannelList, error) {
func (s SqlChannelStore) GetChannels(teamId string, userId string, includeDeleted bool, lastDeleteAt int) (model.ChannelList, error) {
query := s.getQueryBuilder().
Select("Channels.*").
From("Channels, ChannelMembers").
@@ -975,25 +975,25 @@ func (s SqlChannelStore) GetChannels(teamId string, userId string, includeDelete
query = query.Where(sq.Eq{"DeleteAt": 0})
}
channels := &model.ChannelList{}
var channels model.ChannelList
sql, args, err := query.ToSql()
if err != nil {
return nil, errors.Wrapf(err, "getchannels_tosql")
}
_, err = s.GetReplica().Select(channels, sql, args...)
_, err = s.GetReplica().Select(&channels, sql, args...)
if err != nil {
return nil, errors.Wrapf(err, "failed to get channels with TeamId=%s and UserId=%s", teamId, userId)
}
if len(*channels) == 0 {
if len(channels) == 0 {
return nil, store.NewErrNotFound("Channel", "userId="+userId)
}
return channels, nil
}
func (s SqlChannelStore) GetAllChannels(offset, limit int, opts store.ChannelSearchOpts) (*model.ChannelListWithTeamData, error) {
func (s SqlChannelStore) GetAllChannels(offset, limit int, opts store.ChannelSearchOpts) (model.ChannelListWithTeamData, error) {
query := s.getAllChannelsQuery(opts, false)
query = query.OrderBy("c.DisplayName, Teams.DisplayName").Limit(uint64(limit)).Offset(uint64(offset))
@@ -1003,8 +1003,8 @@ func (s SqlChannelStore) GetAllChannels(offset, limit int, opts store.ChannelSea
return nil, errors.Wrap(err, "failed to create query")
}
data := &model.ChannelListWithTeamData{}
_, err = s.GetReplica().Select(data, queryString, args...)
var data model.ChannelListWithTeamData
_, err = s.GetReplica().Select(&data, queryString, args...)
if err != nil {
return nil, errors.Wrap(err, "failed to get all channels")
@@ -1071,9 +1071,9 @@ func (s SqlChannelStore) getAllChannelsQuery(opts store.ChannelSearchOpts, forCo
return query
}
func (s SqlChannelStore) GetMoreChannels(teamId string, userId string, offset int, limit int) (*model.ChannelList, error) {
channels := &model.ChannelList{}
_, err := s.GetReplica().Select(channels, `
func (s SqlChannelStore) GetMoreChannels(teamId string, userId string, offset int, limit int) (model.ChannelList, error) {
var channels model.ChannelList
_, err := s.GetReplica().Select(&channels, `
SELECT
Channels.*
FROM
@@ -1113,8 +1113,8 @@ func (s SqlChannelStore) GetMoreChannels(teamId string, userId string, offset in
return channels, nil
}
func (s SqlChannelStore) GetPrivateChannelsForTeam(teamId string, offset int, limit int) (*model.ChannelList, error) {
channels := &model.ChannelList{}
func (s SqlChannelStore) GetPrivateChannelsForTeam(teamId string, offset int, limit int) (model.ChannelList, error) {
var channels model.ChannelList
builder := s.getQueryBuilder().
Select("*").
@@ -1129,16 +1129,16 @@ func (s SqlChannelStore) GetPrivateChannelsForTeam(teamId string, offset int, li
return nil, errors.Wrap(err, "channels_tosql")
}
_, err = s.GetReplica().Select(channels, query, args...)
_, err = s.GetReplica().Select(&channels, query, args...)
if err != nil {
return nil, errors.Wrapf(err, "failed to find chaneld with teamId=%s", teamId)
}
return channels, nil
}
func (s SqlChannelStore) GetPublicChannelsForTeam(teamId string, offset int, limit int) (*model.ChannelList, error) {
channels := &model.ChannelList{}
_, err := s.GetReplica().Select(channels, `
func (s SqlChannelStore) GetPublicChannelsForTeam(teamId string, offset int, limit int) (model.ChannelList, error) {
var channels model.ChannelList
_, err := s.GetReplica().Select(&channels, `
SELECT
Channels.*
FROM
@@ -1164,7 +1164,7 @@ func (s SqlChannelStore) GetPublicChannelsForTeam(teamId string, offset int, lim
return channels, nil
}
func (s SqlChannelStore) GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) (*model.ChannelList, error) {
func (s SqlChannelStore) GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) (model.ChannelList, error) {
props := make(map[string]interface{})
props["teamId"] = teamId
@@ -1179,8 +1179,8 @@ func (s SqlChannelStore) GetPublicChannelsByIdsForTeam(teamId string, channelIds
idQuery += ":channelId" + strconv.Itoa(index)
}
data := &model.ChannelList{}
_, err := s.GetReplica().Select(data, `
var data model.ChannelList
_, err := s.GetReplica().Select(&data, `
SELECT
Channels.*
FROM
@@ -1198,7 +1198,7 @@ func (s SqlChannelStore) GetPublicChannelsByIdsForTeam(teamId string, channelIds
return nil, errors.Wrap(err, "failed to find Channels")
}
if len(*data) == 0 {
if len(data) == 0 {
return nil, store.NewErrNotFound("Channel", fmt.Sprintf("teamId=%s, channelIds=%v", teamId, channelIds))
}
@@ -1231,15 +1231,15 @@ func (s SqlChannelStore) GetChannelCounts(teamId string, userId string) (*model.
return counts, nil
}
func (s SqlChannelStore) GetTeamChannels(teamId string) (*model.ChannelList, error) {
data := &model.ChannelList{}
_, err := s.GetReplica().Select(data, "SELECT * FROM Channels WHERE TeamId = :TeamId And Type != 'D' ORDER BY DisplayName", map[string]interface{}{"TeamId": teamId})
func (s SqlChannelStore) GetTeamChannels(teamId string) (model.ChannelList, error) {
var data model.ChannelList
_, err := s.GetReplica().Select(&data, "SELECT * FROM Channels WHERE TeamId = :TeamId And Type != 'D' ORDER BY DisplayName", map[string]interface{}{"TeamId": teamId})
if err != nil {
return nil, errors.Wrapf(err, "failed to find Channels with teamId=%s", teamId)
}
if len(*data) == 0 {
if len(data) == 0 {
return nil, store.NewErrNotFound("Channel", fmt.Sprintf("teamId=%s", teamId))
}
@@ -1364,8 +1364,8 @@ func (s SqlChannelStore) GetDeletedByName(teamId string, name string) (*model.Ch
return &channel, nil
}
func (s SqlChannelStore) GetDeleted(teamId string, offset int, limit int, userId string) (*model.ChannelList, error) {
channels := &model.ChannelList{}
func (s SqlChannelStore) GetDeleted(teamId string, offset int, limit int, userId string) (model.ChannelList, error) {
var channels model.ChannelList
query := `
SELECT * FROM Channels
@@ -1381,7 +1381,7 @@ func (s SqlChannelStore) GetDeleted(teamId string, offset int, limit int, userId
ORDER BY DisplayName LIMIT :Limit OFFSET :Offset
`
if _, err := s.GetReplica().Select(channels, query, map[string]interface{}{"TeamId": teamId, "Limit": limit, "Offset": offset, "UserId": userId}); err != nil {
if _, err := s.GetReplica().Select(&channels, query, map[string]interface{}{"TeamId": teamId, "Limit": limit, "Offset": offset, "UserId": userId}); err != nil {
if err == sql.ErrNoRows {
return nil, store.NewErrNotFound("Channel", fmt.Sprintf("TeamId=%s,UserId=%s", teamId, userId))
}
@@ -1630,7 +1630,7 @@ func (s SqlChannelStore) UpdateMember(member *model.ChannelMember) (*model.Chann
return updatedMembers[0], nil
}
func (s SqlChannelStore) GetMembers(channelId string, offset, limit int) (*model.ChannelMembers, error) {
func (s SqlChannelStore) GetMembers(channelId string, offset, limit int) (model.ChannelMembers, error) {
var dbMembers channelMemberWithSchemeRolesList
_, err := s.GetReplica().Select(&dbMembers, ChannelMembersWithSchemeSelectQuery+"WHERE ChannelId = :ChannelId LIMIT :Limit OFFSET :Offset", map[string]interface{}{"ChannelId": channelId, "Limit": limit, "Offset": offset})
if err != nil {
@@ -2388,7 +2388,7 @@ func (s SqlChannelStore) AnalyticsDeletedTypeCount(teamId string, channelType st
return v, nil
}
func (s SqlChannelStore) GetMembersForUser(teamId string, userId string) (*model.ChannelMembers, error) {
func (s SqlChannelStore) GetMembersForUser(teamId string, userId string) (model.ChannelMembers, error) {
var dbMembers channelMemberWithSchemeRolesList
_, err := s.GetReplica().Select(&dbMembers, ChannelMembersWithSchemeSelectQuery+"WHERE ChannelMembers.UserId = :UserId AND (Teams.Id = :TeamId OR Teams.Id = '' OR Teams.Id IS NULL)", map[string]interface{}{"TeamId": teamId, "UserId": userId})
if err != nil {
@@ -2398,7 +2398,7 @@ func (s SqlChannelStore) GetMembersForUser(teamId string, userId string) (*model
return dbMembers.ToModel(), nil
}
func (s SqlChannelStore) GetMembersForUserWithPagination(teamId, userId string, page, perPage int) (*model.ChannelMembers, error) {
func (s SqlChannelStore) GetMembersForUserWithPagination(teamId, userId string, page, perPage int) (model.ChannelMembers, error) {
var dbMembers channelMemberWithSchemeRolesList
offset := page * perPage
_, err := s.GetReplica().Select(&dbMembers, ChannelMembersWithSchemeSelectQuery+"WHERE ChannelMembers.UserId = :UserId Limit :Limit Offset :Offset", map[string]interface{}{"TeamId": teamId, "UserId": userId, "Limit": perPage, "Offset": offset})
@@ -2410,7 +2410,7 @@ func (s SqlChannelStore) GetMembersForUserWithPagination(teamId, userId string,
return dbMembers.ToModel(), nil
}
func (s SqlChannelStore) AutocompleteInTeam(teamId string, term string, includeDeleted bool) (*model.ChannelList, error) {
func (s SqlChannelStore) AutocompleteInTeam(teamId string, term string, includeDeleted bool) (model.ChannelList, error) {
deleteFilter := "AND Channels.DeleteAt = 0"
if includeDeleted {
deleteFilter = ""
@@ -2451,10 +2451,10 @@ func (s SqlChannelStore) AutocompleteInTeam(teamId string, term string, includeD
sort.Slice(channels, func(a, b int) bool {
return strings.ToLower(channels[a].DisplayName) < strings.ToLower(channels[b].DisplayName)
})
return &channels, nil
return channels, nil
}
func (s SqlChannelStore) AutocompleteInTeamForSearch(teamId string, userId string, term string, includeDeleted bool) (*model.ChannelList, error) {
func (s SqlChannelStore) AutocompleteInTeamForSearch(teamId string, userId string, term string, includeDeleted bool) (model.ChannelList, error) {
deleteFilter := "AND DeleteAt = 0"
if includeDeleted {
deleteFilter = ""
@@ -2503,7 +2503,7 @@ func (s SqlChannelStore) AutocompleteInTeamForSearch(teamId string, userId strin
sort.Slice(channels, func(a, b int) bool {
return strings.ToLower(channels[a].DisplayName) < strings.ToLower(channels[b].DisplayName)
})
return &channels, nil
return channels, nil
}
func (s SqlChannelStore) autocompleteInTeamForSearchDirectMessages(userId string, term string) ([]*model.Channel, error) {
@@ -2548,7 +2548,7 @@ func (s SqlChannelStore) autocompleteInTeamForSearchDirectMessages(userId string
return channels, nil
}
func (s SqlChannelStore) SearchInTeam(teamId string, term string, includeDeleted bool) (*model.ChannelList, error) {
func (s SqlChannelStore) SearchInTeam(teamId string, term string, includeDeleted bool) (model.ChannelList, error) {
deleteFilter := "AND c.DeleteAt = 0"
if includeDeleted {
deleteFilter = ""
@@ -2572,7 +2572,7 @@ func (s SqlChannelStore) SearchInTeam(teamId string, term string, includeDeleted
})
}
func (s SqlChannelStore) SearchArchivedInTeam(teamId string, term string, userId string) (*model.ChannelList, error) {
func (s SqlChannelStore) SearchArchivedInTeam(teamId string, term string, userId string) (model.ChannelList, error) {
publicChannels, publicErr := s.performSearch(`
SELECT
Channels.*
@@ -2621,13 +2621,13 @@ func (s SqlChannelStore) SearchArchivedInTeam(teamId string, term string, userId
return nil, outputErr
}
output := *publicChannels
output = append(output, *privateChannels...)
output := publicChannels
output = append(output, privateChannels...)
return &output, nil
return output, nil
}
func (s SqlChannelStore) SearchForUserInTeam(userId string, teamId string, term string, includeDeleted bool) (*model.ChannelList, error) {
func (s SqlChannelStore) SearchForUserInTeam(userId string, teamId string, term string, includeDeleted bool) (model.ChannelList, error) {
deleteFilter := "AND c.DeleteAt = 0"
if includeDeleted {
deleteFilter = ""
@@ -2758,7 +2758,7 @@ func (s SqlChannelStore) channelSearchQuery(opts *store.ChannelSearchOpts) sq.Se
return query
}
func (s SqlChannelStore) SearchAllChannels(term string, opts store.ChannelSearchOpts) (*model.ChannelListWithTeamData, int64, error) {
func (s SqlChannelStore) SearchAllChannels(term string, opts store.ChannelSearchOpts) (model.ChannelListWithTeamData, int64, error) {
opts.Term = term
opts.IncludeTeamInfo = true
queryString, args, err := s.channelSearchQuery(&opts).ToSql()
@@ -2786,10 +2786,10 @@ func (s SqlChannelStore) SearchAllChannels(term string, opts store.ChannelSearch
totalCount = int64(len(channels))
}
return &channels, totalCount, nil
return channels, totalCount, nil
}
func (s SqlChannelStore) SearchMore(userId string, teamId string, term string) (*model.ChannelList, error) {
func (s SqlChannelStore) SearchMore(userId string, teamId string, term string) (model.ChannelList, error) {
return s.performSearch(`
SELECT
Channels.*
@@ -2882,7 +2882,7 @@ func (s SqlChannelStore) buildFulltextClause(term string, searchColumns string)
return
}
func (s SqlChannelStore) performSearch(searchQuery string, term string, parameters map[string]interface{}) (*model.ChannelList, error) {
func (s SqlChannelStore) performSearch(searchQuery string, term string, parameters map[string]interface{}) (model.ChannelList, error) {
likeClause, likeTerm := s.buildLIKEClause(term, "c.Name, c.DisplayName, c.Purpose")
if likeTerm == "" {
// If the likeTerm is empty after preparing, then don't bother searching.
@@ -2900,7 +2900,7 @@ func (s SqlChannelStore) performSearch(searchQuery string, term string, paramete
return nil, errors.Wrapf(err, "failed to find Channels with term='%s'", term)
}
return &channels, nil
return channels, nil
}
func (s SqlChannelStore) getSearchGroupChannelsQuery(userId, term string, isPostgreSQL bool) (string, map[string]interface{}) {
@@ -2991,7 +2991,7 @@ func (s SqlChannelStore) getSearchGroupChannelsQuery(userId, term string, isPost
return query, args
}
func (s SqlChannelStore) SearchGroupChannels(userId, term string) (*model.ChannelList, error) {
func (s SqlChannelStore) SearchGroupChannels(userId, term string) (model.ChannelList, error) {
isPostgreSQL := s.DriverName() == model.DatabaseDriverPostgres
queryString, args := s.getSearchGroupChannelsQuery(userId, term, isPostgreSQL)
@@ -2999,10 +2999,10 @@ func (s SqlChannelStore) SearchGroupChannels(userId, term string) (*model.Channe
if _, err := s.GetReplica().Select(&groupChannels, queryString, args); err != nil {
return nil, errors.Wrapf(err, "failed to find Channels with term='%s' and userId=%s", term, userId)
}
return &groupChannels, nil
return groupChannels, nil
}
func (s SqlChannelStore) GetMembersByIds(channelId string, userIds []string) (*model.ChannelMembers, error) {
func (s SqlChannelStore) GetMembersByIds(channelId string, userIds []string) (model.ChannelMembers, error) {
var dbMembers channelMemberWithSchemeRolesList
keys, props := MapStringsToQueryParams(userIds, "User")
@@ -3015,7 +3015,7 @@ func (s SqlChannelStore) GetMembersByIds(channelId string, userIds []string) (*m
return dbMembers.ToModel(), nil
}
func (s SqlChannelStore) GetMembersByChannelIds(channelIds []string, userId string) (*model.ChannelMembers, error) {
func (s SqlChannelStore) GetMembersByChannelIds(channelIds []string, userId string) (model.ChannelMembers, error) {
var dbMembers channelMemberWithSchemeRolesList
keys, props := MapStringsToQueryParams(channelIds, "Channel")

Просмотреть файл

@@ -767,14 +767,14 @@ func (s SqlChannelStore) UpdateSidebarCategories(userId, teamId string, categori
// UpdateSidebarChannelsByPreferences is called when the Preference table is being updated to keep SidebarCategories in sync
// At the moment, it's only handling Favorites and NOT DMs/GMs (those will be handled client side)
func (s SqlChannelStore) UpdateSidebarChannelsByPreferences(preferences *model.Preferences) error {
func (s SqlChannelStore) UpdateSidebarChannelsByPreferences(preferences model.Preferences) error {
transaction, err := s.GetMaster().Begin()
if err != nil {
return errors.Wrap(err, "UpdateSidebarChannelsByPreferences: begin_transaction")
}
defer finalizeTransaction(transaction)
for _, preference := range *preferences {
for _, preference := range preferences {
preference := preference
if preference.Category != model.PreferenceCategoryFavoriteChannel {
@@ -918,14 +918,14 @@ func (s SqlChannelStore) addChannelToFavoritesCategoryT(transaction *gorp.Transa
// DeleteSidebarChannelsByPreferences is called when the Preference table is being updated to keep SidebarCategories in sync
// At the moment, it's only handling Favorites and NOT DMs/GMs (those will be handled client side)
func (s SqlChannelStore) DeleteSidebarChannelsByPreferences(preferences *model.Preferences) error {
func (s SqlChannelStore) DeleteSidebarChannelsByPreferences(preferences model.Preferences) error {
transaction, err := s.GetMaster().Begin()
if err != nil {
return errors.Wrap(err, "DeleteSidebarChannelsByPreferences: begin_transaction")
}
defer finalizeTransaction(transaction)
for _, preference := range *preferences {
for _, preference := range preferences {
preference := preference
if preference.Category != model.PreferenceCategoryFavoriteChannel {

Просмотреть файл

@@ -194,7 +194,7 @@ func createPostWithUserId(ss store.Store, id string) *model.Post {
return createPost(ss, model.NewId(), id, "", "")
}
func createPreferences(ss store.Store, userId string) *model.Preferences {
func createPreferences(ss store.Store, userId string) model.Preferences {
preferences := model.Preferences{
{
UserId: userId,
@@ -203,8 +203,8 @@ func createPreferences(ss store.Store, userId string) *model.Preferences {
Value: "somevalue",
},
}
ss.Preference().Save(&preferences)
return &preferences
ss.Preference().Save(preferences)
return preferences
}
func createReaction(ss store.Store, userId, postId string) *model.Reaction {

Просмотреть файл

@@ -50,7 +50,7 @@ func (s SqlPreferenceStore) deleteUnusedFeatures() {
}
}
func (s SqlPreferenceStore) Save(preferences *model.Preferences) error {
func (s SqlPreferenceStore) Save(preferences model.Preferences) error {
// wrap in a transaction so that if one fails, everything fails
transaction, err := s.GetMaster().Begin()
if err != nil {
@@ -58,7 +58,7 @@ func (s SqlPreferenceStore) Save(preferences *model.Preferences) error {
}
defer finalizeTransaction(transaction)
for _, preference := range *preferences {
for _, preference := range preferences {
preference := preference
if upsertErr := s.save(transaction, &preference); upsertErr != nil {
return upsertErr

Просмотреть файл

@@ -52,7 +52,7 @@ func TestDeleteUnusedFeatures(t *testing.T) {
},
}
err := ss.Preference().Save(&features)
err := ss.Preference().Save(features)
require.NoError(t, err)
ss.Preference().(*SqlPreferenceStore).deleteUnusedFeatures()

Просмотреть файл

@@ -42,9 +42,9 @@ func (s SqlProductNoticesStore) Clear(notices []string) error {
return nil
}
func (s SqlProductNoticesStore) ClearOldNotices(currentNotices *model.ProductNotices) error {
func (s SqlProductNoticesStore) ClearOldNotices(currentNotices model.ProductNotices) error {
var notices []string
for _, currentNotice := range *currentNotices {
for _, currentNotice := range currentNotices {
notices = append(notices, currentNotice.ID)
}
sql, args, _ := s.getQueryBuilder().Delete("ProductNoticeViewState").Where(sq.NotEq{"NoticeId": notices}).ToSql()

Просмотреть файл

@@ -329,7 +329,7 @@ func upgradeDatabaseToVersion33(sqlStore *SqlStore) {
data[i].Value = strings.Replace(data[i].Value, "solarized_", "solarized-", -1)
}
sqlStore.Preference().Save(&data)
sqlStore.Preference().Save(data)
}
}

Просмотреть файл

@@ -241,7 +241,7 @@ func TestMsgCountRootMigration(t *testing.T) {
members, err := ss.Channel().GetMembersByIds(channel.Id, userIds)
require.NoError(t, err)
for _, m := range *members {
for _, m := range members {
for i, uid := range userIds {
if m.UserId == uid {
assert.Equal(t, testChannel.ExpectedMembershipMsgCountRoot[i], m.MsgCountRoot)