[MM-57356] Make use of go1.21 features (#26620)

Этот коммит содержится в:
Ben Schumacher
2024-04-04 13:44:03 +02:00
коммит произвёл GitHub
родитель 3c45c44cb1
Коммит 1e0de8f559
24 изменённых файлов: 59 добавлений и 507 удалений

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

@@ -8,11 +8,10 @@ import (
"net/url"
"path"
"reflect"
"slices"
"strings"
"github.com/pkg/errors"
"github.com/mattermost/mattermost/server/public/utils"
)
// AutocompleteArgType describes autocomplete argument type
@@ -235,7 +234,7 @@ func (ad *AutocompleteData) IsValid() error {
return errors.New("Command should be lowercase")
}
roles := []string{SystemAdminRoleId, SystemUserRoleId, ""}
if !utils.Contains(roles, ad.RoleID) {
if !slices.Contains(roles, ad.RoleID) {
return errors.New("Wrong role in the autocomplete data")
}
if len(ad.Arguments) > 0 && len(ad.SubCommands) > 0 {

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

@@ -5,10 +5,9 @@ package model
import (
"net/http"
"slices"
"strconv"
"time"
pUtils "github.com/mattermost/mattermost/server/public/utils"
)
const (
@@ -137,7 +136,7 @@ func (u *UserReportOptions) IsValid() *AppError {
}
// Validate against the columns we allow sorting for
if !pUtils.Contains(UserReportSortColumns, u.SortColumn) {
if !slices.Contains(UserReportSortColumns, u.SortColumn) {
return NewAppError("UserReportOptions.IsValid", "model.user_report_options.is_valid.invalid_sort_column", nil, "", http.StatusBadRequest)
}

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

@@ -6,6 +6,7 @@ package model
import (
"encoding/json"
"io"
"maps"
"strconv"
)
@@ -272,7 +273,7 @@ func (ev *WebSocketEvent) Copy() *WebSocketEvent {
func (ev *WebSocketEvent) DeepCopy() *WebSocketEvent {
evCopy := &WebSocketEvent{
event: ev.event,
data: copyMap(ev.data),
data: maps.Clone(ev.data),
broadcast: ev.broadcast.copy(),
sequence: ev.sequence,
precomputedJSON: ev.precomputedJSON.copy(),
@@ -280,14 +281,6 @@ func (ev *WebSocketEvent) DeepCopy() *WebSocketEvent {
return evCopy
}
func copyMap[K comparable, V any](m map[K]V) map[K]V {
dataCopy := make(map[K]V, len(m))
for k, v := range m {
dataCopy[k] = v
}
return dataCopy
}
func (ev *WebSocketEvent) GetData() map[string]any {
return ev.data
}

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

@@ -3,7 +3,7 @@ package pluginapi
import (
"bytes"
"encoding/json"
"sort"
"slices"
"strings"
"sync"
"time"
@@ -193,8 +193,7 @@ func (s *MemoryStore) ListKeys(page int, count int, options ...ListKeysOption) (
return []string{}, nil
}
// TODO: Use slices.Sort once the toolchain got updated to go1.21
sort.Strings(allKeys)
slices.Sort(allKeys)
pageKeys := paginateSlice(allKeys, page, count)

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

@@ -1,14 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package utils
// Contains returns true if the slice contains the item.
func Contains[T comparable](slice []T, item T) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}

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

@@ -1,388 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package utils_test
import (
"testing"
"github.com/mattermost/mattermost/server/public/utils"
)
func TestContains(t *testing.T) {
testCasesStr := []struct {
name string
slice []string
item string
expected bool
}{
{
name: "empty slice",
slice: []string{},
item: "foo",
expected: false,
},
{
name: "slice with item",
slice: []string{"foo"},
item: "foo",
expected: true,
},
{
name: "slice without item",
slice: []string{"bar"},
item: "foo",
expected: false,
},
{
name: "slice with multiple items",
slice: []string{"foo", "bar"},
item: "foo",
expected: true,
},
{
name: "slice with multiple items without item",
slice: []string{"foo", "bar"},
item: "baz",
expected: false,
},
}
for _, tc := range testCasesStr {
t.Run(tc.name, func(t *testing.T) {
actual := utils.Contains(tc.slice, tc.item)
if actual != tc.expected {
t.Errorf("Expected Contains(%v, %v) to be %v, but got %v", tc.slice, tc.item, tc.expected, actual)
}
})
}
testCasesInt := []struct {
name string
slice []int
item int
expected bool
}{
{
name: "empty slice",
slice: []int{},
item: 1,
expected: false,
},
{
name: "slice with item",
slice: []int{1},
item: 1,
expected: true,
},
{
name: "slice without item",
slice: []int{2},
item: 1,
expected: false,
},
{
name: "slice with multiple items",
slice: []int{1, 2},
item: 1,
expected: true,
},
{
name: "slice with multiple items without item",
slice: []int{1, 2},
item: 3,
expected: false,
},
}
for _, tc := range testCasesInt {
t.Run(tc.name, func(t *testing.T) {
actual := utils.Contains(tc.slice, tc.item)
if actual != tc.expected {
t.Errorf("Expected Contains(%v, %v) to be %v, but got %v", tc.slice, tc.item, tc.expected, actual)
}
})
}
testCasesFloat := []struct {
name string
slice []float64
item float64
expected bool
}{
{
name: "empty slice",
slice: []float64{},
item: 1.0,
expected: false,
},
{
name: "slice with item",
slice: []float64{1.0},
item: 1.0,
expected: true,
},
{
name: "slice without item",
slice: []float64{2.0},
item: 1.0,
expected: false,
},
{
name: "slice with multiple items",
slice: []float64{1.0, 2.0},
item: 1.0,
expected: true,
},
{
name: "slice with multiple items without item",
slice: []float64{1.0, 2.0},
item: 3.0,
expected: false,
},
}
for _, tc := range testCasesFloat {
t.Run(tc.name, func(t *testing.T) {
actual := utils.Contains(tc.slice, tc.item)
if actual != tc.expected {
t.Errorf("Expected Contains(%v, %v) to be %v, but got %v", tc.slice, tc.item, tc.expected, actual)
}
})
}
testCasesBool := []struct {
name string
slice []bool
item bool
expected bool
}{
{
name: "empty slice",
slice: []bool{},
item: true,
expected: false,
},
{
name: "slice with item",
slice: []bool{true},
item: true,
expected: true,
},
{
name: "slice without item",
slice: []bool{false},
item: true,
expected: false,
},
{
name: "slice with multiple items",
slice: []bool{true, false},
item: true,
expected: true,
},
{
name: "slice with multiple items without item",
slice: []bool{true, false},
item: false,
expected: true,
},
}
for _, tc := range testCasesBool {
t.Run(tc.name, func(t *testing.T) {
actual := utils.Contains(tc.slice, tc.item)
if actual != tc.expected {
t.Errorf("Expected Contains(%v, %v) to be %v, but got %v", tc.slice, tc.item, tc.expected, actual)
}
})
}
testCasesByte := []struct {
name string
slice []byte
item byte
expected bool
}{
{
name: "empty slice",
slice: []byte{},
item: 1,
expected: false,
},
{
name: "slice with item",
slice: []byte{1},
item: 1,
expected: true,
},
{
name: "slice without item",
slice: []byte{2},
item: 1,
expected: false,
},
{
name: "slice with multiple items",
slice: []byte{1, 2},
item: 1,
expected: true,
},
{
name: "slice with multiple items without item",
slice: []byte{1, 2},
item: 3,
expected: false,
},
}
for _, tc := range testCasesByte {
t.Run(tc.name, func(t *testing.T) {
actual := utils.Contains(tc.slice, tc.item)
if actual != tc.expected {
t.Errorf("Expected Contains(%v, %v) to be %v, but got %v", tc.slice, tc.item, tc.expected, actual)
}
})
}
testCasesRune := []struct {
name string
slice []rune
item rune
expected bool
}{
{
name: "empty slice",
slice: []rune{},
item: 1,
expected: false,
},
{
name: "slice with item",
slice: []rune{1},
item: 1,
expected: true,
},
{
name: "slice without item",
slice: []rune{2},
item: 1,
expected: false,
},
{
name: "slice with multiple items",
slice: []rune{1, 2},
item: 1,
expected: true,
},
{
name: "slice with multiple items without item",
slice: []rune{1, 2},
item: 3,
expected: false,
},
}
for _, tc := range testCasesRune {
t.Run(tc.name, func(t *testing.T) {
actual := utils.Contains(tc.slice, tc.item)
if actual != tc.expected {
t.Errorf("Expected Contains(%v, %v) to be %v, but got %v", tc.slice, tc.item, tc.expected, actual)
}
})
}
testCasesComplex := []struct {
name string
slice []complex128
item complex128
expected bool
}{
{
name: "empty slice",
slice: []complex128{},
item: 1,
expected: false,
},
{
name: "slice with item",
slice: []complex128{1},
item: 1,
expected: true,
},
{
name: "slice without item",
slice: []complex128{2},
item: 1,
expected: false,
},
{
name: "slice with multiple items",
slice: []complex128{1, 2},
item: 1,
expected: true,
},
{
name: "slice with multiple items without item",
slice: []complex128{1, 2},
item: 3,
expected: false,
},
}
for _, tc := range testCasesComplex {
t.Run(tc.name, func(t *testing.T) {
actual := utils.Contains(tc.slice, tc.item)
if actual != tc.expected {
t.Errorf("Expected Contains(%v, %v) to be %v, but got %v", tc.slice, tc.item, tc.expected, actual)
}
})
}
testCasesUint := []struct {
name string
slice []uint
item uint
expected bool
}{
{
name: "empty slice",
slice: []uint{},
item: 1,
expected: false,
},
{
name: "slice with item",
slice: []uint{1},
item: 1,
expected: true,
},
{
name: "slice without item",
slice: []uint{2},
item: 1,
expected: false,
},
{
name: "slice with multiple items",
slice: []uint{1, 2},
item: 1,
expected: true,
},
{
name: "slice with multiple items without item",
slice: []uint{1, 2},
item: 3,
expected: false,
},
}
for _, tc := range testCasesUint {
t.Run(tc.name, func(t *testing.T) {
actual := utils.Contains(tc.slice, tc.item)
if actual != tc.expected {
t.Errorf("Expected Contains(%v, %v) to be %v, but got %v", tc.slice, tc.item, tc.expected, actual)
}
})
}
}