From 33c735a1c356a02bc3e2a13e4a04ab46e2026e97 Mon Sep 17 00:00:00 2001 From: aaron <462826@qq.com> Date: Thu, 17 Sep 2020 15:33:52 +0800 Subject: [PATCH] close task#7906 --- demo/test/test_format.yaml | 31 ++++++++++++++++ src/gen/generator.go | 10 +++--- src/gen/rand.go | 6 ++-- src/utils/string/string.go | 72 ++++++++++++++++++++++++++++++++++++-- 4 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 demo/test/test_format.yaml diff --git a/demo/test/test_format.yaml b/demo/test/test_format.yaml new file mode 100644 index 00000000..13341afd --- /dev/null +++ b/demo/test/test_format.yaml @@ -0,0 +1,31 @@ +title: 测试 +desc: +author: zentao +version: 1.0 + +fields: + + - field: field_format_exp + range: 1-10 + format: "passwd%02d" + postfix: "\t" + + - field: field_format_func_md5 + range: a-z + format: md5 + postfix: "\t" + + - field: field_format_func_sha1 + range: password + format: sha1 + postfix: "\t" + + - field: field_format_func_base64 + range: pass/?&=word+ + format: base64 + postfix: "\t" + + - field: field_format_func_urlencode + range: pass/?&=word+ + format: urlencode + postfix: "\t" \ No newline at end of file diff --git a/src/gen/generator.go b/src/gen/generator.go index 559708b2..7557f956 100644 --- a/src/gen/generator.go +++ b/src/gen/generator.go @@ -226,7 +226,7 @@ func GetFieldValStr(field model.DefField, val interface{}) string { switch val.(type) { case int64: if field.Format != "" { - str, success = stringUtils.FormatStr(field.Format, val.(int64)) + str, success = stringUtils.FormatStr(field.Format, val.(int64), 0) } if !success { str = strconv.FormatInt(val.(int64), 10) @@ -237,7 +237,7 @@ func GetFieldValStr(field model.DefField, val interface{}) string { precision = field.Precision } if field.Format != "" { - str, success = stringUtils.FormatStr(field.Format, val.(float64)) + str, success = stringUtils.FormatStr(field.Format, val.(float64), precision) } if !success { str = strconv.FormatFloat(val.(float64), 'f', precision, 64) @@ -245,7 +245,7 @@ func GetFieldValStr(field model.DefField, val interface{}) string { case byte: str = string(val.(byte)) if field.Format != "" { - str, success = stringUtils.FormatStr(field.Format, str) + str, success = stringUtils.FormatStr(field.Format, str, 0) } if !success { str = string(val.(byte)) @@ -257,8 +257,10 @@ func GetFieldValStr(field model.DefField, val interface{}) string { if match { valInt, err := strconv.Atoi(str) if err == nil { - str, success = stringUtils.FormatStr(field.Format, valInt) + str, success = stringUtils.FormatStr(field.Format, valInt, 0) } + } else { + str, success = stringUtils.FormatStr(field.Format, str, 0) } default: } diff --git a/src/gen/rand.go b/src/gen/rand.go index 44106a37..41096749 100644 --- a/src/gen/rand.go +++ b/src/gen/rand.go @@ -53,7 +53,7 @@ func GetRandFromRange(dataType, start, end, step, repeatStr, precisionStr string items := make([]string, 0) item := strconv.FormatInt(val, 10) if format != "" { - formatVal, success := stringUtils.FormatStr(format, val) + formatVal, success := stringUtils.FormatStr(format, val, 0) if success { item = formatVal } } @@ -85,7 +85,7 @@ func GetRandFromRange(dataType, start, end, step, repeatStr, precisionStr string item := string(val) if format != "" { - formatVal, success := stringUtils.FormatStr(format, val) + formatVal, success := stringUtils.FormatStr(format, val, 0) if success { item = formatVal } } @@ -120,7 +120,7 @@ func GetRandFromRange(dataType, start, end, step, repeatStr, precisionStr string item := strconv.FormatFloat(val, 'f', precision, 64) if format != "" { - formatVal, success := stringUtils.FormatStr(format, val) + formatVal, success := stringUtils.FormatStr(format, val, precision) if success { item = formatVal } } diff --git a/src/utils/string/string.go b/src/utils/string/string.go index 9378ffb1..88835b26 100644 --- a/src/utils/string/string.go +++ b/src/utils/string/string.go @@ -2,6 +2,9 @@ package stringUtils import ( "bytes" + "crypto/md5" + "crypto/sha1" + "encoding/base64" "encoding/binary" "encoding/hex" "fmt" @@ -104,13 +107,35 @@ func GetNumbWidth(numb int) int { return width } -func FormatStr(format string, val interface{}) (string, bool) { - str := fmt.Sprintf(format, val) +func FormatStr(format string, val interface{}, precision int) (ret string, pass bool) { + format = strings.ToLower(strings.TrimSpace(format)) + + if strings.Index(format, "md5") == 0 { + str := interfaceToStr(val, precision) + ret = Md5(str) + pass = true + return + } else if strings.Index(format, "sha1") == 0 { + str := interfaceToStr(val, precision) + ret = Sha1(str) + pass = true + return + } else if strings.Index(format, "base64") == 0 { + str := interfaceToStr(val, precision) + ret = Base64(str) + pass = true + return + } else if strings.Index(format, "urlencode") == 0 { + str := interfaceToStr(val, precision) + ret = UrlEncode(str) + pass = true + return + } + str := fmt.Sprintf(format, val) if strings.Index(str,"%!") == 0 { return "", false } - return str, true } @@ -171,4 +196,45 @@ func GetPinyin(word string) string { p, _ := pinyin.New(word).Split("").Mode(pinyin.WithoutTone).Convert() return p +} + +func interfaceToStr(intf interface{}, precision int) (ret string) { + switch intf.(type) { + case int64: + return strconv.FormatInt(intf.(int64), 10) + case float64: + return strconv.FormatFloat(intf.(float64), 'f', precision, 64) + case byte: + return string(intf.(byte)) + case string: + return intf.(string) + default: + return intf.(string) + } +} + +func Md5(str string) (ret string) { + h := md5.New() + h.Write([]byte(str)) + ret = hex.EncodeToString(h.Sum(nil)) + + return +} +func Sha1(str string) (ret string) { + h := sha1.New() + h.Write([]byte(str)) + bs := h.Sum(nil) + ret = fmt.Sprintf("%x", bs) + + return +} +func Base64(str string) (ret string) { + ret = base64.StdEncoding.EncodeToString([]byte(str)) + + return +} +func UrlEncode(str string) (ret string) { + ret = base64.URLEncoding.EncodeToString([]byte(str)) + + return } \ No newline at end of file -- GitLab