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

gen int

上级 50a0b0a7
...@@ -30,7 +30,6 @@ func Generate(file string, total int, fields string, out string, table string) { ...@@ -30,7 +30,6 @@ func Generate(file string, total int, fields string, out string, table string) {
Print(rows) Print(rows)
} }
func Print(rows [][]string) { func Print(rows [][]string) {
width := stringUtils.GetNumbWidth(len(rows)) width := stringUtils.GetNumbWidth(len(rows))
......
...@@ -27,35 +27,55 @@ func Generate(def *model.Definition, total int, fields string, out string, table ...@@ -27,35 +27,55 @@ func Generate(def *model.Definition, total int, fields string, out string, table
continue continue
} }
if field.Loop == 0 {
field.Loop = 1
}
prefix := field.Prefix prefix := field.Prefix
postfix := field.Postfix postfix := field.Postfix
indexOfRow = i % len(fieldMap[field.Name])
if len(rows) == i { rows = append(rows, make([]string, 0)) } if len(rows) == i { rows = append(rows, make([]string, 0)) }
str := "n/a" loopStr := ""
val := fieldMap[field.Name][indexOfRow] for j := 0; j < field.Loop; j++ {
switch val.(type) { if loopStr != "" {
case int64: loopStr = loopStr + field.Loopfix
str = strconv.FormatInt(val.(int64),10) }
case float64: str := GetFieldStr(field, fieldMap, indexOfRow)
precision := 0 loopStr = loopStr + str
if field.Precision > 0 {
precision = field.Precision indexOfRow++
} if indexOfRow == len(fieldMap[field.Name]) { // no enough
str = strconv.FormatFloat(val.(float64), 'f', precision, 64) indexOfRow = 0
case byte: }
str = string(val.(byte))
default:
} }
rows[i] = append(rows[i], prefix + str + postfix) rows[i] = append(rows[i], prefix + loopStr + postfix)
} }
} }
return rows return rows
} }
func GetFieldStr(field model.Field, fieldMap map[string][]interface{}, indexOfRow int) string {
str := "n/a"
val := fieldMap[field.Name][indexOfRow]
switch val.(type) {
case int64:
str = strconv.FormatInt(val.(int64), 10)
case float64:
precision := 0
if field.Precision > 0 {
precision = field.Precision
}
str = strconv.FormatFloat(val.(float64), 'f', precision, 64)
case byte:
str = string(val.(byte))
default:
}
return str
}
func GenerateFieldArr(field *model.Field, total int, fieldMap map[string][]interface{}) { func GenerateFieldArr(field *model.Field, total int, fieldMap map[string][]interface{}) {
datatype := strings.TrimSpace(field.Type) datatype := strings.TrimSpace(field.Type)
if datatype == "" { datatype = "list" } if datatype == "" { datatype = "list" }
......
...@@ -16,6 +16,8 @@ type Field struct { ...@@ -16,6 +16,8 @@ type Field struct {
Range string `yaml:"range"` Range string `yaml:"range"`
Prefix string `yaml:"prefix"` Prefix string `yaml:"prefix"`
Postfix string `yaml:"postfix"` Postfix string `yaml:"postfix"`
Loop int `yaml:"loop"`
Loopfix string `yaml:"loopfix"`
Expect string `yaml:"expect"` Expect string `yaml:"expect"`
Fields []Field `yaml:"fields,flow"` Fields []Field `yaml:"fields,flow"`
......
...@@ -7,9 +7,11 @@ fields: ...@@ -7,9 +7,11 @@ fields:
- name: field1 - name: field1
note: 默认的列表类型,通过逗号隔成若干区段。 note: 默认的列表类型,通过逗号隔成若干区段。
type: list type: list
range: 1-3,x-z:R,10.01-19.20:R,101,102 range: 1-3,x-z:R,10.01-19.20:3,101,102,103
prefix: prefix:
postfix: postfix:
loop: 3
loopfix: "|"
expect: 1,2,3...,10,20,21,22...,25,27,29.30 expect: 1,2,3...,10,20,21,22...,25,27,29.30
- name: field2 - name: field2
note: 区间可以指定步长。 note: 区间可以指定步长。
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册