github.com/polarismesh/polaris@v1.17.8/common/utils/funcs.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package utils 19 20 import ( 21 "encoding/hex" 22 "encoding/json" 23 "strings" 24 25 "github.com/google/uuid" 26 ) 27 28 var emptyVal = struct{}{} 29 30 // ConvertFilter map[string]string to map[string][]string 31 func ConvertFilter(filters map[string]string) map[string][]string { 32 newFilters := make(map[string][]string) 33 34 for k, v := range filters { 35 val := make([]string, 0) 36 val = append(val, v) 37 newFilters[k] = val 38 } 39 40 return newFilters 41 } 42 43 // CollectMapKeys collect filters key to slice 44 func CollectMapKeys(filters map[string]string) []string { 45 fields := make([]string, 0, len(filters)) 46 for k := range filters { 47 fields = append(fields, k) 48 if k != "" { 49 fields = append(fields, strings.ToUpper(string(k[:1]))+k[1:]) 50 } 51 } 52 53 return fields 54 } 55 56 // IsPrefixWildName 判断名字是否为通配名字,只支持前缀索引(名字最后为*) 57 func IsPrefixWildName(name string) bool { 58 length := len(name) 59 return length >= 1 && name[length-1:length] == "*" 60 } 61 62 // IsWildName 判断名字是否为通配名字,前缀或者后缀 63 func IsWildName(name string) bool { 64 return IsPrefixWildName(name) || IsSuffixWildName(name) 65 } 66 67 // ParseWildNameForSql 如果 name 是通配字符串,将通配字符*替换为sql中的% 68 func ParseWildNameForSql(name string) string { 69 if IsPrefixWildName(name) { 70 name = name[:len(name)-1] + "%" 71 } 72 if IsSuffixWildName(name) { 73 name = "%" + name[1:] 74 } 75 return name 76 } 77 78 // IsSuffixWildName 判断名字是否为通配名字,只支持后缀索引(名字第一个字符为*) 79 func IsSuffixWildName(name string) bool { 80 length := len(name) 81 return length >= 1 && name[0:1] == "*" 82 } 83 84 // ParseWildName 判断是否为格式化查询条件并且返回真正的查询信息 85 func ParseWildName(name string) (string, bool) { 86 length := len(name) 87 ok := length >= 1 && name[length-1:length] == "*" 88 89 if ok { 90 return name[:len(name)-1], ok 91 } 92 93 return name, false 94 } 95 96 // IsWildMatchIgnoreCase 判断 name 是否匹配 pattern,pattern 可以是前缀或者后缀,忽略大小写 97 func IsWildMatchIgnoreCase(name, pattern string) bool { 98 return IsWildMatch(strings.ToLower(name), strings.ToLower(pattern)) 99 } 100 101 // IsWildNotMatch . 102 func IsWildNotMatch(name, pattern string) bool { 103 return !IsWildMatch(name, pattern) 104 } 105 106 // IsWildMatch 判断 name 是否匹配 pattern,pattern 可以是前缀或者后缀 107 func IsWildMatch(name, pattern string) bool { 108 if IsPrefixWildName(pattern) { 109 pattern = strings.TrimRight(pattern, "*") 110 if strings.HasPrefix(name, pattern) { 111 return true 112 } 113 if IsSuffixWildName(pattern) { 114 pattern = strings.TrimLeft(pattern, "*") 115 return strings.Contains(name, pattern) 116 } 117 return false 118 } else if IsSuffixWildName(pattern) { 119 pattern = strings.TrimLeft(pattern, "*") 120 if strings.HasSuffix(name, pattern) { 121 return true 122 } 123 return false 124 } 125 return pattern == name 126 } 127 128 // NewUUID 返回一个随机的UUID 129 func NewUUID() string { 130 uuidBytes := uuid.New() 131 return hex.EncodeToString(uuidBytes[:]) 132 } 133 134 // NewUUID 返回一个随机的UUID 135 func NewRoutingV2UUID() string { 136 uuidBytes := uuid.New() 137 return hex.EncodeToString(uuidBytes[:]) 138 } 139 140 // NewV2Revision 返回一个随机的UUID 141 func NewV2Revision() string { 142 uuidBytes := uuid.New() 143 return "v2-" + hex.EncodeToString(uuidBytes[:]) 144 } 145 146 // StringSliceDeDuplication 字符切片去重 147 func StringSliceDeDuplication(s []string) []string { 148 m := make(map[string]struct{}, len(s)) 149 res := make([]string, 0, len(s)) 150 for k := range s { 151 if _, ok := m[s[k]]; !ok { 152 m[s[k]] = emptyVal 153 res = append(res, s[k]) 154 } 155 } 156 157 return res 158 } 159 160 func MustJson(v interface{}) string { 161 data, err := json.Marshal(v) 162 _ = err 163 return string(data) 164 } 165 166 // IsNotEqualMap metadata need update 167 func IsNotEqualMap(req map[string]string, old map[string]string) bool { 168 if req == nil { 169 return false 170 } 171 172 if len(req) != len(old) { 173 return true 174 } 175 176 needUpdate := false 177 for key, value := range req { 178 oldValue, ok := old[key] 179 if !ok { 180 needUpdate = true 181 break 182 } 183 if value != oldValue { 184 needUpdate = true 185 break 186 } 187 } 188 if needUpdate { 189 return needUpdate 190 } 191 192 for key, value := range old { 193 newValue, ok := req[key] 194 if !ok { 195 needUpdate = true 196 break 197 } 198 if value != newValue { 199 needUpdate = true 200 break 201 } 202 } 203 204 return needUpdate 205 }