提交 9a64c743 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

fix some issues

上级 18f81227
......@@ -93,7 +93,7 @@ fields:
postfix: "\t"
- field: field_format # 通过格式化字符串输出。
range: 1-10 # passwd 1,passwd 2,passwd 3 ... passwd10。
range: 1-10:R # passwd 1,passwd 2,passwd 3 ... passwd10。
format: "passwd%02d" # 用%2d补零,使密码整体保持8位,%2d默认补空格。
postfix: "\t"
......
......@@ -166,7 +166,7 @@ func CreateValuesFromLiteral(field *model.DefField, desc string, stepStr string,
if field.Path != "" && stepStr == "r" {
items = append(items, Placeholder(field.Path))
mp := placeholderMapForRandValues("list", elemArr, "", "", "", "")
mp := placeholderMapForRandValues("list", elemArr, "", "", "", "", field.Format)
vari.RandFieldNameToValuesMap[field.Path] = mp
return
......@@ -208,7 +208,7 @@ func CreateValuesFromInterval(field *model.DefField, desc, stepStr string, repea
if field.Path != "" && dataType != "string" && rand { // random
items = append(items, Placeholder(field.Path))
mp := placeholderMapForRandValues(dataType, []string{}, startStr, endStr, stepStr, strconv.Itoa(precision))
mp := placeholderMapForRandValues(dataType, []string{}, startStr, endStr, stepStr, strconv.Itoa(precision), field.Format)
vari.RandFieldNameToValuesMap[field.Path] = mp
return
......@@ -274,7 +274,7 @@ func Placeholder(str string) string {
return "${" + str + "}"
}
func placeholderMapForRandValues(tp string, list []string, start, end, step, precision string) map[string]interface{} {
func placeholderMapForRandValues(tp string, list []string, start, end, step, precision, format string) map[string]interface{} {
ret := map[string]interface{}{}
ret["type"] = tp
......@@ -285,6 +285,7 @@ func placeholderMapForRandValues(tp string, list []string, start, end, step, pre
ret["end"] = end
ret["step"] = step
ret["precision"] = precision
ret["format"] = format
return ret
}
......@@ -195,20 +195,23 @@ func getValForPlaceholder(placeholderStr string, count int) []string {
start := mp["start"].(string)
end := mp["end"].(string)
precision := mp["precision"].(string)
format := mp["format"].(string)
strs = GetRandFromRange("int", start, end, "1", repeat, precision, count)
strs = GetRandFromRange("int", start, end, "1", repeat, precision, count, format)
} else if tp == "float" {
start := mp["start"].(string)
end := mp["end"].(string)
precision := mp["precision"].(string)
format := mp["format"].(string)
strs = GetRandFromRange("float", start, end, "1", repeat, precision, count)
strs = GetRandFromRange("float", start, end, "1", repeat, precision, count, format)
} else if tp == "char" {
start := mp["start"].(string)
end := mp["end"].(string)
precision := mp["precision"].(string)
format := mp["format"].(string)
strs = GetRandFromRange("char", start, end, "1", repeat, precision, count)
strs = GetRandFromRange("char", start, end, "1", repeat, precision, count, format)
} else if tp == "list" {
list := mp["list"].([]string)
strs = GetRandFromList(list, repeat, count)
......
......@@ -2,6 +2,7 @@ package gen
import (
commonUtils "github.com/easysoft/zendata/src/utils/common"
stringUtils "github.com/easysoft/zendata/src/utils/string"
"strconv"
"strings"
)
......@@ -25,7 +26,7 @@ func GetRandFromList(list []string, repeatStr string, count int) []string {
return ret
}
func GetRandFromRange(dataType, start, end, step, repeatStr, precisionStr string, count int) []string {
func GetRandFromRange(dataType, start, end, step, repeatStr, precisionStr string, count int, format string) []string {
repeat, _ := strconv.Atoi(repeatStr)
precision, _ := strconv.Atoi(precisionStr)
......@@ -50,8 +51,14 @@ func GetRandFromRange(dataType, start, end, step, repeatStr, precisionStr string
val := startInt + rand
items := make([]string, 0)
item := strconv.FormatInt(val, 10)
if format != "" {
formatVal, success := stringUtils.FormatStr(format, val)
if success { item = formatVal }
}
for round := 0; round < repeat; round++ {
items = append(items, strconv.FormatInt(val, 10))
items = append(items, item)
}
temp := strings.Join(items, "")
......@@ -75,8 +82,15 @@ func GetRandFromRange(dataType, start, end, step, repeatStr, precisionStr string
}
val := startChar + byte(rand)
items := make([]string, 0)
item := string(val)
if format != "" {
formatVal, success := stringUtils.FormatStr(format, val)
if success { item = formatVal }
}
for round := 0; round < repeat; round++ {
items = append(items, string(val))
items = append(items, item)
}
temp := strings.Join(items, "")
......@@ -103,9 +117,15 @@ func GetRandFromRange(dataType, start, end, step, repeatStr, precisionStr string
val := startFloat + float64(rand)*stepFloat
items := make([]string, 0)
item := strconv.FormatFloat(val, 'f', precision, 64)
if format != "" {
formatVal, success := stringUtils.FormatStr(format, val)
if success { item = formatVal }
}
for round := 0; round < repeat; round++ {
str := strconv.FormatFloat(val, 'f', precision, 64)
items = append(items, str)
items = append(items, item)
}
temp := strings.Join(items, "")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册