Move tag parser related codes as a standalone sub package #1547

Merged
lunny merged 3 commits from lunny/tag into master 2020-02-27 03:58:37 +00:00
4 changed files with 30 additions and 18 deletions
Showing only changes of commit 2c255690fd - Show all commits

View File

@ -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
View 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
}

View File

@ -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
}

View File

@ -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)
}
}