github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/internal/pkg/helper/format.go (about) 1 package helper 2 3 import ( 4 "crypto/md5" 5 "crypto/sha1" 6 "encoding/base64" 7 "encoding/hex" 8 "fmt" 9 "math/rand" 10 "net/url" 11 "regexp" 12 "strconv" 13 "strings" 14 "time" 15 16 "github.com/Chain-Zhang/pinyin" 17 "github.com/easysoft/zendata/internal/pkg/domain" 18 "github.com/mattn/go-runewidth" 19 "gopkg.in/yaml.v2" 20 ) 21 22 func FormatStr(format string, val interface{}, precision int) (ret string, pass bool) { 23 format = strings.TrimSpace(format) 24 if format == "" { 25 return val.(string), true 26 } 27 28 if strings.Index(format, "md5") == 0 { 29 str := interfaceToStr(val, precision) 30 ret = Md5(str) 31 pass = true 32 return 33 34 } else if strings.Index(format, "sha1") == 0 { 35 str := interfaceToStr(val, precision) 36 ret = Sha1(str) 37 pass = true 38 return 39 40 } else if strings.Index(format, "base64") == 0 { 41 str := interfaceToStr(val, precision) 42 ret = Base64(str) 43 pass = true 44 return 45 46 } else if strings.Index(format, "urlencode") == 0 { 47 str := interfaceToStr(val, precision) 48 ret = UrlEncode(str) 49 pass = true 50 return 51 52 } else if strings.Index(format, "uuid") == 0 { 53 ret = GenerateUuid(format) 54 55 pass = true 56 return 57 } else if strings.Index(format, "ulid") == 0 { 58 ret = GenerateUlid(format) 59 60 pass = true 61 return 62 } else if strings.Index(format, "password") == 0 { 63 length := 8 64 regx := regexp.MustCompile(`password\(\s*(\d+)\s*\)`) 65 arr := regx.FindStringSubmatch(format) 66 if len(arr) > 1 { 67 length, _ = strconv.Atoi(arr[1]) 68 } 69 70 if length == 0 { 71 length = 8 72 } 73 ret = RandPassword(length) 74 75 pass = true 76 return 77 } else if strings.Index(format, "binary") == 0 { 78 ret = ImgBindata 79 80 pass = true 81 return 82 } else if strings.Index(format, "id_card") == 0 { 83 ret = GenerateIdCard() 84 85 pass = true 86 return 87 } else if strings.Index(format, "credit_card") == 0 { 88 cardType := "" 89 90 regx := regexp.MustCompile(`credit_card\(\s*(\S+)\s*\)`) 91 arr := regx.FindStringSubmatch(format) 92 if len(arr) > 1 { 93 cardType = strings.Trim(arr[1], "'") 94 } 95 96 ret = GenerateCreditCard(cardType) 97 98 pass = true 99 return 100 } else if strings.Index(format, "mac") == 0 { 101 ret = GenerateMac() 102 103 pass = true 104 return 105 } else if strings.Index(format, "token") == 0 { 106 ret = GenerateToken(format) 107 108 pass = true 109 return 110 } else if strings.Index(format, "json") == 0 { 111 ret = Json 112 113 pass = true 114 return 115 } 116 117 str := fmt.Sprintf(format, val) 118 if strings.Index(str, "%!") == 0 { 119 return "", false 120 } 121 122 return str, true 123 } 124 125 func AddPad(str string, field domain.DefField) string { 126 if field.Length > 0 && field.Length > runewidth.StringWidth(str) { 127 gap := field.Length - len(str) 128 if field.LeftPad != "" { 129 field.LeftPad = field.LeftPad[:1] 130 pads := strings.Repeat(field.LeftPad, gap) 131 str = pads + str 132 } else if field.RightPad != "" { 133 field.RightPad = field.RightPad[:1] 134 pads := strings.Repeat(field.RightPad, gap) 135 str = str + pads 136 } else { 137 field.LeftPad = " " 138 pads := strings.Repeat(field.LeftPad, gap) 139 str = pads + str 140 } 141 } 142 143 return str 144 } 145 146 func ConvertForSql(str string) (ret string) { 147 arr := []rune(str) 148 149 count := 0 150 for i, item := range arr { 151 if count%2 == 1 && string(item) != "'" { 152 ret = ret + "'" 153 } else if i == len(arr)-1 && count%2 == 0 && string(item) == "'" { 154 ret = ret + "'" 155 } 156 157 if string(item) != "'" { 158 count = 0 159 } 160 161 ret = ret + string(item) 162 163 if string(item) == "'" { 164 count++ 165 } 166 } 167 168 return 169 } 170 171 func Escape(in string, shouldEscape []rune) (out string) { 172 out = "" 173 escapeChar := shouldEscape[0] 174 175 for _, v := range in { 176 for _, se := range shouldEscape { 177 if v == se { 178 out += string(escapeChar) 179 break 180 } 181 } 182 183 out += string(v) 184 } 185 186 return 187 } 188 189 func EscapeValueOfMysql(in string) string { 190 return Escape(in, []rune{'\\', '\'', '"'}) 191 } 192 193 func EscapeValueOfSqlServer(in string) string { 194 195 return Escape(in, []rune{'\''}) 196 } 197 198 func EscapeValueOfOracle(in string) string { 199 200 return Escape(in, []rune{'\''}) 201 } 202 203 func EscapeColumnOfMysql(in string) string { 204 205 return Escape(in, []rune{'`'}) 206 } 207 208 func EscapeColumnOfSqlServer(in string) string { 209 return Escape(in, []rune{']'}) 210 } 211 212 // oracle limit 213 //func EscapeColumnOfOracle(in string) string 214 215 func GetPinyin(word string) string { 216 p, _ := pinyin.New(word).Split("").Mode(pinyin.WithoutTone).Convert() 217 218 return p 219 } 220 221 func interfaceToStr(intf interface{}, precision int) (ret string) { 222 switch intf.(type) { 223 case int64: 224 return strconv.FormatInt(intf.(int64), 10) 225 case float64: 226 return strconv.FormatFloat(intf.(float64), 'f', precision, 64) 227 case byte: 228 return string(intf.(byte)) 229 case string: 230 return intf.(string) 231 default: 232 return intf.(string) 233 } 234 } 235 236 func Md5(str string) (ret string) { 237 h := md5.New() 238 h.Write([]byte(str)) 239 ret = hex.EncodeToString(h.Sum(nil)) 240 241 return 242 } 243 func Sha1(str string) (ret string) { 244 h := sha1.New() 245 h.Write([]byte(str)) 246 bs := h.Sum(nil) 247 ret = fmt.Sprintf("%x", bs) 248 249 return 250 } 251 func Base64(str string) (ret string) { 252 ret = base64.StdEncoding.EncodeToString([]byte(str)) 253 254 return 255 } 256 func UrlEncode(str string) (ret string) { 257 ret = url.QueryEscape(str) 258 259 return 260 } 261 262 func ReplaceSpecialChars(bytes []byte) []byte { 263 str := string(bytes) 264 265 inRanges := false // for ranges yaml only 266 ret := "" 267 for _, line := range strings.Split(str, "\n") { 268 if strings.Index(strings.TrimSpace(line), "ranges") == 0 { // ranges in res 269 inRanges = true 270 } else if len(line) > 0 && string(line[0]) != " " { // not begin with space, ranges end 271 inRanges = false 272 } 273 274 if strings.Index(strings.TrimSpace(line), "range") == 0 || inRanges { 275 regx1 := regexp.MustCompile("(?P<x>[^`])\\[") 276 line = regx1.ReplaceAllString(line, "${x}(") 277 278 regx2 := regexp.MustCompile("\\](?P<x>[^`]*)") 279 line = regx2.ReplaceAllString(line, ")${x}") 280 } 281 282 ret += line + "\n" 283 } 284 285 return []byte(ret) 286 } 287 288 func ConvertYamlStringToMapFormat(bytes []byte) (ret string) { 289 m := yaml.MapSlice{} 290 yaml.Unmarshal(bytes, &m) 291 bytesReturn, _ := yaml.Marshal(&m) 292 ret = string(bytesReturn) 293 294 // replace '"test"' to "test" 295 reg := regexp.MustCompile(`([:\s]+?)'"(.*)"'`) 296 //if reg.MatchString(ret) { 297 ret = reg.ReplaceAllString(ret, `${1}"${2}"`) 298 //} 299 return 300 } 301 302 func ParseInt(str string) (ret int) { 303 ret, _ = strconv.Atoi(str) 304 305 return 306 } 307 func ParseBool(str string) (ret bool) { 308 ret, _ = strconv.ParseBool(str) 309 310 return 311 } 312 313 func RandPassword(length int) string { 314 rand.Seed(time.Now().UnixNano()) 315 var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.*_+%$#@") 316 b := make([]rune, length) 317 for i := range b { 318 b[i] = letterRunes[rand.Intn(len(letterRunes))] 319 } 320 return string(b) 321 }