2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2020-10-20 21:57:29 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-10-20 23:14:03 +08:00
|
|
|
package util
|
2020-10-20 21:57:29 +08:00
|
|
|
|
2020-10-20 23:14:03 +08:00
|
|
|
import (
|
2021-12-20 00:26:46 +08:00
|
|
|
"bytes"
|
2021-05-08 22:04:45 +08:00
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
2020-10-20 23:14:03 +08:00
|
|
|
"errors"
|
2021-04-29 19:51:03 +08:00
|
|
|
"fmt"
|
2023-05-01 23:15:51 +08:00
|
|
|
"math/rand"
|
2022-08-09 16:50:49 +08:00
|
|
|
"os"
|
2023-06-21 13:55:20 +03:00
|
|
|
"path/filepath"
|
2021-05-09 15:44:12 +08:00
|
|
|
"strconv"
|
2020-10-20 23:14:03 +08:00
|
|
|
"strings"
|
2022-03-06 22:46:02 +08:00
|
|
|
"time"
|
2021-12-27 18:55:25 +08:00
|
|
|
"unicode"
|
2021-02-13 20:10:41 +08:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2020-10-20 23:14:03 +08:00
|
|
|
)
|
2020-10-20 21:57:29 +08:00
|
|
|
|
2021-05-09 15:44:12 +08:00
|
|
|
func ParseInt(s string) int {
|
2021-12-19 22:30:54 +08:00
|
|
|
if s == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-05-09 15:44:12 +08:00
|
|
|
i, err := strconv.Atoi(s)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2022-03-07 00:33:45 +08:00
|
|
|
func ParseFloat(s string) float64 {
|
|
|
|
f, err := strconv.ParseFloat(s, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2021-12-19 22:30:54 +08:00
|
|
|
func ParseBool(s string) bool {
|
2022-07-16 12:14:35 +08:00
|
|
|
if s == "\x01" || s == "true" {
|
2022-04-06 20:38:14 +08:00
|
|
|
return true
|
2022-07-16 12:14:35 +08:00
|
|
|
} else if s == "false" {
|
|
|
|
return false
|
2022-04-06 20:38:14 +08:00
|
|
|
}
|
|
|
|
|
2021-12-19 22:30:54 +08:00
|
|
|
i := ParseInt(s)
|
|
|
|
return i != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func BoolToString(b bool) string {
|
|
|
|
if b {
|
|
|
|
return "1"
|
|
|
|
}
|
2022-02-15 10:56:59 -03:00
|
|
|
return "0"
|
2021-12-19 22:30:54 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 12:26:14 +08:00
|
|
|
// CamelToSnakeCase This function transform camelcase in snakecase LoremIpsum in lorem_ipsum
|
2021-12-20 00:26:46 +08:00
|
|
|
func CamelToSnakeCase(camel string) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for _, c := range camel {
|
|
|
|
if 'A' <= c && c <= 'Z' {
|
|
|
|
// just convert [A-Z] to _[a-z]
|
|
|
|
if buf.Len() > 0 {
|
|
|
|
buf.WriteRune('_')
|
|
|
|
}
|
|
|
|
buf.WriteRune(c - 'A' + 'a')
|
2022-02-15 10:56:59 -03:00
|
|
|
continue
|
2021-12-20 00:26:46 +08:00
|
|
|
}
|
2022-02-15 10:56:59 -03:00
|
|
|
buf.WriteRune(c)
|
2021-12-20 00:26:46 +08:00
|
|
|
}
|
2022-02-15 10:56:59 -03:00
|
|
|
return strings.ReplaceAll(buf.String(), " ", "")
|
2021-12-20 00:26:46 +08:00
|
|
|
}
|
|
|
|
|
2020-10-20 23:14:03 +08:00
|
|
|
func GetOwnerAndNameFromId(id string) (string, string) {
|
|
|
|
tokens := strings.Split(id, "/")
|
|
|
|
if len(tokens) != 2 {
|
|
|
|
panic(errors.New("GetOwnerAndNameFromId() error, wrong token count for ID: " + id))
|
|
|
|
}
|
2020-10-20 21:57:29 +08:00
|
|
|
|
2020-10-20 23:14:03 +08:00
|
|
|
return tokens[0], tokens[1]
|
2020-10-20 21:57:29 +08:00
|
|
|
}
|
2021-02-13 20:10:41 +08:00
|
|
|
|
2023-04-06 23:06:18 +08:00
|
|
|
func GetOwnerFromId(id string) string {
|
|
|
|
tokens := strings.Split(id, "/")
|
|
|
|
if len(tokens) != 2 {
|
|
|
|
panic(errors.New("GetOwnerAndNameFromId() error, wrong token count for ID: " + id))
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokens[0]
|
|
|
|
}
|
|
|
|
|
2021-09-05 23:38:37 +08:00
|
|
|
func GetOwnerAndNameFromIdNoCheck(id string) (string, string) {
|
|
|
|
tokens := strings.SplitN(id, "/", 2)
|
|
|
|
return tokens[0], tokens[1]
|
|
|
|
}
|
|
|
|
|
2023-02-12 09:33:24 +08:00
|
|
|
func GetOwnerAndNameAndOtherFromId(id string) (string, string, string) {
|
|
|
|
tokens := strings.Split(id, "/")
|
|
|
|
if len(tokens) != 3 {
|
|
|
|
panic(errors.New("GetOwnerAndNameAndOtherFromId() error, wrong token count for ID: " + id))
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokens[0], tokens[1], tokens[2]
|
|
|
|
}
|
|
|
|
|
2021-02-13 20:10:41 +08:00
|
|
|
func GenerateId() string {
|
|
|
|
return uuid.NewString()
|
|
|
|
}
|
2021-04-29 19:51:03 +08:00
|
|
|
|
2022-03-06 22:46:02 +08:00
|
|
|
func GenerateTimeId() string {
|
|
|
|
timestamp := time.Now().Unix()
|
|
|
|
tm := time.Unix(timestamp, 0)
|
|
|
|
t := tm.Format("20060102_150405")
|
|
|
|
|
|
|
|
random := uuid.NewString()[0:7]
|
|
|
|
|
|
|
|
res := fmt.Sprintf("%s_%s", t, random)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-03-14 00:12:02 +08:00
|
|
|
func GenerateSimpleTimeId() string {
|
|
|
|
timestamp := time.Now().Unix()
|
|
|
|
tm := time.Unix(timestamp, 0)
|
|
|
|
t := tm.Format("20060102150405")
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2023-05-01 23:15:51 +08:00
|
|
|
func GetRandomName() string {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
const charset = "0123456789abcdefghijklmnopqrstuvwxyz"
|
|
|
|
result := make([]byte, 6)
|
|
|
|
for i := range result {
|
|
|
|
result[i] = charset[rand.Intn(len(charset))]
|
|
|
|
}
|
|
|
|
return string(result)
|
|
|
|
}
|
|
|
|
|
2022-11-25 09:36:47 +08:00
|
|
|
func GetId(owner, name string) string {
|
|
|
|
return fmt.Sprintf("%s/%s", owner, name)
|
2021-04-29 19:51:03 +08:00
|
|
|
}
|
2021-05-08 22:04:45 +08:00
|
|
|
|
2023-02-12 09:33:24 +08:00
|
|
|
func GetSessionId(owner, name, application string) string {
|
|
|
|
return fmt.Sprintf("%s/%s/%s", owner, name, application)
|
|
|
|
}
|
|
|
|
|
2021-05-08 22:04:45 +08:00
|
|
|
func GetMd5Hash(text string) string {
|
|
|
|
hash := md5.Sum([]byte(text))
|
|
|
|
return hex.EncodeToString(hash[:])
|
|
|
|
}
|
2021-07-17 14:13:00 +08:00
|
|
|
|
2023-02-18 09:31:58 +08:00
|
|
|
func IsStringsEmpty(strs ...string) bool {
|
2021-07-17 14:13:00 +08:00
|
|
|
for _, str := range strs {
|
|
|
|
if len(str) == 0 {
|
2021-07-26 11:39:49 +08:00
|
|
|
return true
|
2021-07-17 14:13:00 +08:00
|
|
|
}
|
|
|
|
}
|
2021-07-26 11:39:49 +08:00
|
|
|
return false
|
2021-07-17 14:13:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetMaxLenStr(strs ...string) string {
|
|
|
|
m := 0
|
|
|
|
i := 0
|
|
|
|
for j, str := range strs {
|
|
|
|
l := len(str)
|
|
|
|
if l > m {
|
|
|
|
m = l
|
|
|
|
i = j
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strs[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetMinLenStr(strs ...string) string {
|
|
|
|
m := int(^uint(0) >> 1)
|
|
|
|
i := 0
|
|
|
|
for j, str := range strs {
|
|
|
|
l := len(str)
|
2022-02-15 10:56:59 -03:00
|
|
|
if l < m {
|
2021-07-17 14:13:00 +08:00
|
|
|
m = l
|
|
|
|
i = j
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strs[i]
|
|
|
|
}
|
2021-10-15 16:27:10 +08:00
|
|
|
|
|
|
|
func ReadStringFromPath(path string) string {
|
2023-06-21 13:55:20 +03:00
|
|
|
data, err := os.ReadFile(filepath.Clean(path))
|
2021-10-15 16:27:10 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteStringToPath(s string, path string) {
|
2022-08-09 16:50:49 +08:00
|
|
|
err := os.WriteFile(path, []byte(s), 0o644)
|
2021-10-15 16:27:10 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-15 10:56:59 -03:00
|
|
|
// SnakeString transform XxYy to xx_yy
|
2021-12-25 10:55:10 +08:00
|
|
|
func SnakeString(s string) string {
|
|
|
|
data := make([]byte, 0, len(s)*2)
|
|
|
|
j := false
|
|
|
|
num := len(s)
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
d := s[i]
|
|
|
|
if i > 0 && d >= 'A' && d <= 'Z' && j {
|
|
|
|
data = append(data, '_')
|
|
|
|
}
|
|
|
|
if d != '_' {
|
|
|
|
j = true
|
|
|
|
}
|
|
|
|
data = append(data, d)
|
|
|
|
}
|
2022-02-15 10:56:59 -03:00
|
|
|
result := strings.ToLower(string(data[:]))
|
|
|
|
return strings.ReplaceAll(result, " ", "")
|
2021-12-25 10:55:10 +08:00
|
|
|
}
|
2021-12-27 18:55:25 +08:00
|
|
|
|
|
|
|
func IsChinese(str string) bool {
|
|
|
|
var flag bool
|
|
|
|
for _, v := range str {
|
|
|
|
if unicode.Is(unicode.Han, v) {
|
|
|
|
flag = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return flag
|
|
|
|
}
|
2022-03-15 12:54:57 +08:00
|
|
|
|
|
|
|
func GetMaskedPhone(phone string) string {
|
2023-02-16 22:53:28 +08:00
|
|
|
return rePhone.ReplaceAllString(phone, "$1****$2")
|
2022-03-15 12:54:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetMaskedEmail(email string) string {
|
|
|
|
if email == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens := strings.Split(email, "@")
|
|
|
|
username := maskString(tokens[0])
|
|
|
|
domain := tokens[1]
|
|
|
|
domainTokens := strings.Split(domain, ".")
|
2022-03-20 23:21:09 +08:00
|
|
|
domainTokens[len(domainTokens)-2] = maskString(domainTokens[len(domainTokens)-2])
|
2022-03-15 12:54:57 +08:00
|
|
|
return fmt.Sprintf("%s@%s", username, strings.Join(domainTokens, "."))
|
|
|
|
}
|
|
|
|
|
|
|
|
func maskString(str string) string {
|
|
|
|
if len(str) <= 2 {
|
|
|
|
return str
|
|
|
|
} else {
|
2022-03-20 23:21:09 +08:00
|
|
|
return fmt.Sprintf("%c%s%c", str[0], strings.Repeat("*", len(str)-2), str[len(str)-1])
|
2022-03-15 12:54:57 +08:00
|
|
|
}
|
2022-03-20 23:21:09 +08:00
|
|
|
}
|
2023-04-18 21:30:46 +08:00
|
|
|
|
|
|
|
// GetEndPoint remove scheme from url
|
|
|
|
func GetEndPoint(endpoint string) string {
|
|
|
|
for _, prefix := range []string{"https://", "http://"} {
|
|
|
|
endpoint = strings.TrimPrefix(endpoint, prefix)
|
|
|
|
}
|
|
|
|
return endpoint
|
|
|
|
}
|
2023-06-16 21:44:21 +07:00
|
|
|
|
|
|
|
// HasString reports if slice has input string.
|
|
|
|
func HasString(strs []string, str string) bool {
|
|
|
|
for _, i := range strs {
|
|
|
|
if i == str {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|