diff --git a/helpers.go b/helpers.go index e9a3a646..1401cbf2 100644 --- a/helpers.go +++ b/helpers.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "reflect" - "sort" "strconv" "strings" "time" @@ -184,20 +183,6 @@ func structName(v reflect.Type) string { return v.Name() } -func sliceEq(left, right []string) bool { - if len(left) != len(right) { - return false - } - sort.Sort(sort.StringSlice(left)) - sort.Sort(sort.StringSlice(right)) - for i := 0; i < len(left); i++ { - if left[i] != right[i] { - return false - } - } - return true -} - func indexName(tableName, idxName string) string { return fmt.Sprintf("IDX_%v_%v", tableName, idxName) } diff --git a/internal/utils/slice.go b/internal/utils/slice.go new file mode 100644 index 00000000..89685706 --- /dev/null +++ b/internal/utils/slice.go @@ -0,0 +1,22 @@ +// Copyright 2020 The Xorm Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package utils + +import "sort" + +// SliceEq return true if two slice have the same elements even if different sort. +func SliceEq(left, right []string) bool { + if len(left) != len(right) { + return false + } + sort.Sort(sort.StringSlice(left)) + sort.Sort(sort.StringSlice(right)) + for i := 0; i < len(left); i++ { + if left[i] != right[i] { + return false + } + } + return true +} diff --git a/session_schema.go b/session_schema.go index 629b127d..809f158f 100644 --- a/session_schema.go +++ b/session_schema.go @@ -9,6 +9,7 @@ import ( "fmt" "strings" + "xorm.io/xorm/internal/utils" "xorm.io/xorm/schemas" ) @@ -188,7 +189,7 @@ func (session *Session) isIndexExist2(tableName string, cols []string, unique bo } for _, index := range indexes { - if sliceEq(index.Cols, cols) { + if utils.SliceEq(index.Cols, cols) { if unique { return index.Type == schemas.UniqueType, nil } diff --git a/tags/tag_test.go b/tags/tag_test.go index f4a79379..5775b40a 100644 --- a/tags/tag_test.go +++ b/tags/tag_test.go @@ -4,7 +4,11 @@ package tags -import "testing" +import ( + "testing" + + "xorm.io/xorm/internal/utils" +) func TestSplitTag(t *testing.T) { var cases = []struct { @@ -19,7 +23,7 @@ func TestSplitTag(t *testing.T) { for _, kase := range cases { tags := splitTag(kase.tag) - if !sliceEq(tags, kase.tags) { + if !utils.SliceEq(tags, kase.tags) { t.Fatalf("[%d]%v is not equal [%d]%v", len(tags), tags, len(kase.tags), kase.tags) } }