Move tag parser related codes as a standalone sub package #1547
15
helpers.go
15
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)
|
||||
}
|
||||
|
|
22
internal/utils/slice.go
Normal file
22
internal/utils/slice.go
Normal file
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user