提交 33c735a1 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

close task#7906

上级 32631ab8
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
......@@ -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:
}
......
......@@ -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 }
}
......
......@@ -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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册