94
pkg/utils/idutils/id_utils.go
Normal file
94
pkg/utils/idutils/id_utils.go
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package idutils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/golang/example/stringutil"
|
||||
"github.com/sony/sonyflake"
|
||||
hashids "github.com/speps/go-hashids"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/utils/stringutils"
|
||||
)
|
||||
|
||||
var sf *sonyflake.Sonyflake
|
||||
|
||||
func init() {
|
||||
var st sonyflake.Settings
|
||||
if len(os.Getenv("DEVOPSPHERE_IP")) != 0 {
|
||||
st.MachineID = machineID
|
||||
}
|
||||
sf = sonyflake.NewSonyflake(st)
|
||||
if sf == nil {
|
||||
panic("failed to initialize sonyflake")
|
||||
}
|
||||
}
|
||||
|
||||
func GetIntId() uint64 {
|
||||
id, err := sf.NextID()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// format likes: B6BZVN3mOPvx
|
||||
func GetUuid(prefix string) string {
|
||||
id := GetIntId()
|
||||
hd := hashids.NewData()
|
||||
h, err := hashids.NewWithData(hd)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
i, err := h.Encode([]int{int(id)})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return prefix + stringutils.Reverse(i)
|
||||
}
|
||||
|
||||
const Alphabet36 = "abcdefghijklmnopqrstuvwxyz1234567890"
|
||||
|
||||
// format likes: 300m50zn91nwz5
|
||||
func GetUuid36(prefix string) string {
|
||||
id := GetIntId()
|
||||
hd := hashids.NewData()
|
||||
hd.Alphabet = Alphabet36
|
||||
h, err := hashids.NewWithData(hd)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
i, err := h.Encode([]int{int(id)})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return prefix + stringutil.Reverse(i)
|
||||
}
|
||||
|
||||
func machineID() (uint16, error) {
|
||||
ipStr := os.Getenv("DEVOPSPHERE_IP")
|
||||
if len(ipStr) == 0 {
|
||||
return 0, errors.New("'DEVOPSPHERE_IP' environment variable not set")
|
||||
}
|
||||
ip := net.ParseIP(ipStr)
|
||||
if len(ip) < 4 {
|
||||
return 0, errors.New("invalid IP")
|
||||
}
|
||||
return uint16(ip[2])<<8 + uint16(ip[3]), nil
|
||||
}
|
||||
24
pkg/utils/idutils/id_utils_test.go
Normal file
24
pkg/utils/idutils/id_utils_test.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package idutils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetUuid(t *testing.T) {
|
||||
fmt.Println(GetUuid(""))
|
||||
}
|
||||
|
||||
func TestGetUuid36(t *testing.T) {
|
||||
fmt.Println(GetUuid36(""))
|
||||
}
|
||||
|
||||
func TestGetManyUuid(t *testing.T) {
|
||||
var strSlice []string
|
||||
for i := 0; i < 10000; i++ {
|
||||
testId := GetUuid("")
|
||||
strSlice = append(strSlice, testId)
|
||||
}
|
||||
sort.Strings(strSlice)
|
||||
}
|
||||
35
pkg/utils/reflectutils/reflect.go
Normal file
35
pkg/utils/reflectutils/reflect.go
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package reflectutils
|
||||
|
||||
import "reflect"
|
||||
|
||||
func In(value interface{}, container interface{}) bool {
|
||||
containerValue := reflect.ValueOf(container)
|
||||
switch reflect.TypeOf(container).Kind() {
|
||||
case reflect.Slice, reflect.Array:
|
||||
for i := 0; i < containerValue.Len(); i++ {
|
||||
if containerValue.Index(i).Interface() == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
case reflect.Map:
|
||||
if containerValue.MapIndex(reflect.ValueOf(value)).IsValid() {
|
||||
return true
|
||||
}
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
77
pkg/utils/stringutils/string.go
Normal file
77
pkg/utils/stringutils/string.go
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
Copyright 2018 The KubeSphere Authors.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package stringutils
|
||||
|
||||
import (
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/asaskevich/govalidator"
|
||||
)
|
||||
|
||||
// Creates an slice of slice values not included in the other given slice.
|
||||
func Diff(base, exclude []string) (result []string) {
|
||||
excludeMap := make(map[string]bool)
|
||||
for _, s := range exclude {
|
||||
excludeMap[s] = true
|
||||
}
|
||||
for _, s := range base {
|
||||
if !excludeMap[s] {
|
||||
result = append(result, s)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func Unique(ss []string) (result []string) {
|
||||
smap := make(map[string]bool)
|
||||
for _, s := range ss {
|
||||
smap[s] = true
|
||||
}
|
||||
for s := range smap {
|
||||
result = append(result, s)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func CamelCaseToUnderscore(str string) string {
|
||||
return govalidator.CamelCaseToUnderscore(str)
|
||||
}
|
||||
|
||||
func UnderscoreToCamelCase(str string) string {
|
||||
return govalidator.UnderscoreToCamelCase(str)
|
||||
}
|
||||
|
||||
func FindString(array []string, str string) int {
|
||||
for index, s := range array {
|
||||
if str == s {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func StringIn(str string, array []string) bool {
|
||||
return FindString(array, str) > -1
|
||||
}
|
||||
|
||||
func Reverse(s string) string {
|
||||
size := len(s)
|
||||
buf := make([]byte, size)
|
||||
for start := 0; start < size; {
|
||||
r, n := utf8.DecodeRuneInString(s[start:])
|
||||
start += n
|
||||
utf8.EncodeRune(buf[size-start:], r)
|
||||
}
|
||||
return string(buf)
|
||||
}
|
||||
Reference in New Issue
Block a user