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

read items and definition from another yaml file

上级 abd4d160
......@@ -6,25 +6,12 @@ import (
"github.com/easysoft/zendata/src/model"
logUtils "github.com/easysoft/zendata/src/utils/log"
stringUtils "github.com/easysoft/zendata/src/utils/string"
"gopkg.in/yaml.v2"
"io/ioutil"
"strconv"
)
func Generate(file string, total int, fieldsToExport string, out string, table string) {
definition := model.Definition{}
yamlContent, err := ioutil.ReadFile(file)
if err != nil {
logUtils.Screen("fail to read " + file)
return
}
err = yaml.Unmarshal(yamlContent, &definition)
if err != nil {
logUtils.Screen("fail to parse " + file)
return
}
gen.LoadDefinitionFromFile(file, &definition)
rows := gen.GenerateForDefinition(&definition, total, fieldsToExport, out, table)
Print(rows)
......
package gen
import (
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const"
logUtils "github.com/easysoft/zendata/src/utils/log"
"gopkg.in/yaml.v2"
"io/ioutil"
)
func LoadDefinitionFromFile(file string) {
def := model.Definition{}
yamlContent, err := ioutil.ReadFile(file)
if err != nil {
logUtils.Screen("fail to read " + file)
return
}
err = yaml.Unmarshal(yamlContent, &def)
if err != nil {
logUtils.Screen("fail to parse " + file)
return
}
if constant.Definition.Title == "" { // only add the fields in first level yaml file
constant.Definition = def
} else {
for _, field := range def.Fields {
constant.LoadedFields[field.Name] = field // add to a map
}
}
}
......@@ -3,12 +3,16 @@ package gen
import (
"github.com/easysoft/zendata/src/model"
constant "github.com/easysoft/zendata/src/utils/const"
logUtils "github.com/easysoft/zendata/src/utils/log"
stringUtils "github.com/easysoft/zendata/src/utils/string"
"io/ioutil"
"strconv"
"strings"
)
func GenerateForDefinition(def *model.Definition, total int, fieldsToExport string, out string, table string) [][]string {
func GenerateForDefinition(total int, fieldsToExport string, out string, table string) [][]string {
def := constant.Definition
fieldsToExportArr := strings.Split(fieldsToExport, ",")
fieldNameToValues := map[string][]string{}
......@@ -42,14 +46,19 @@ func GenerateForDefinition(def *model.Definition, total int, fieldsToExport stri
return rows
}
func GenerateForField(field *model.Field, total int) []string {
func GenerateForField(field *model.Field, total int) []string {
if field.Loop == 0 {field.Loop = 1}
values := make([]string, 0)
if len(field.Fields) == 0 {
values = GenerateForFieldLevel(field, total)
} else {
if field.Type == "custom" && field.Range != "" { // customized
LoadDefinitionFromFile(field.Range)
values = GenerateFieldItemsFromDefinition(field, total)
} else if strings.Index(field.Range, ".txt:") > -1 { // load list from file
values = GenerateFieldItemsFromTextFile(field.Range, total)
} else if len(field.Fields) > 0 { // nested definition
arr := make([][]string, 0)
for _, child := range field.Fields {
childValues := GenerateForField(&child, total)
......@@ -65,15 +74,43 @@ func GenerateForField(field *model.Field, total int) []string {
concat = field.Prefix + concat + field.Postfix
values = append(values, concat)
}
} else {
values = GenerateFieldItemsFromDefinition(field, total)
}
return values
}
func GenerateForFieldLevel(field *model.Field, total int) []string {
func GenerateFieldItemsFromTextFile(file string, total int) []string {
list := make([]string, 0)
content, err := ioutil.ReadFile(file)
if err != nil {
logUtils.Screen("fail to read " + file)
return list
}
str := string(content)
str = strings.Replace(str, "\\r\\n", "\\n", -1)
list = strings.Split(str, "\\n")
return list
}
func GenerateFieldItemsFromDefinition(field *model.Field, total int) []string {
values := make([]string, 0)
// 整理出值的列表
fieldValue := GenerateFieldItems(field, total)
datatype := strings.TrimSpace(field.Type)
if datatype == "" { datatype = "list" }
fieldValue := model.FieldValue{}
switch datatype {
case constant.LIST.String():
fieldValue = GenerateList(field, total)
default:
}
index := 0
count := 0
......@@ -91,22 +128,6 @@ func GenerateForFieldLevel(field *model.Field, total int) []string {
return values
}
func GenerateFieldItems(field *model.Field, total int) model.FieldValue {
datatype := strings.TrimSpace(field.Type)
if datatype == "" { datatype = "list" }
fieldValue := model.FieldValue{}
switch datatype {
case constant.LIST.String():
fieldValue = GenerateList(field, total)
default:
}
return fieldValue
}
func GenerateFieldValWithLoop(field model.Field, fieldValue model.FieldValue, indexOfRow *int) string {
prefix := field.Prefix
postfix := field.Postfix
......@@ -172,5 +193,4 @@ func GetFieldValStr(field model.Field, val interface{}) string {
}
return str
}
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package constant
import (
"fmt"
"github.com/easysoft/zendata/src/model"
"os"
)
......@@ -42,4 +43,7 @@ var (
Power3 = 255 * 255 * 255
Power2 = 255 * 255
Power1 = 255
Definition model.Definition = model.Definition{}
LoadedFields map[string]interface{} = map[string]interface{}{}
)
title: buildin
desc: 內置数据
author: zentao
version: 1.0
fields:
- name: domain
note: 域名
type: list
range: domain.txt:R
- name: first_name
note: 英文名
type: list
loop: 6
prefix: user_
expect:
- name: last_name
note: 英文姓
type: custom
config: custom.yaml
loop: 6
prefix: user_
expect:
\ No newline at end of file
......@@ -14,30 +14,28 @@ fields:
loopfix: "|"
format: "%.5f"
expect: 1,2,3...,10,20,21,22...,25,27,29.30
- name: field2
note: 区间可以指定步长。
type: list
range: 1-10:2, 1-2:0.1
expect: 1,3,5,7,9,1,1.1,1.2...,2
- name: field3
note: 通过R属性指定随机。R属性和步长不能同时出现。
type: list
range: 1-10:R
expect: 1,5,8...
- name: field4
note: 从一个文件中读取列表,并指定随机。
type: list
range: field4.txt:R
expect: 1,3,9,10
- name: field5
note: 循环的字段。
type: list
range: a-z
loop: 3
loopfix: |
postfix: ^
expect: a|b|c^d|e|f^g|h|i
range: ${user_name}@${domain}.com
expect: aaron@gmail.com
note: 测试引用和组合,含內置、同文件、列表文件、组合等特性。
- name: user_name
note: 用户名
fields:
- name: first_name
note: 用户名
type: custom
config: buildin.yaml
- name: last_name
note: 用户名
type: custom
range: buildin.yaml
- name: domain
note: 域名
type: custom
range: buildin.yaml
- name: field6
type: custom
config: custom.yaml
......@@ -50,6 +48,7 @@ fields:
type: user
range: age>10
format: $name-$age-$sex
- name: field9
note: 嵌套的字段
fields:
......
title: just a test
desc: this is the test file.
author: wwccss
version: 1.0
fields:
- name: field1
datatype: list
range: 11-20
step: 1
prefix: int_
postfix: ' '
fields:
- name: field1.1
datatype: list
range: 11-20
step: 1
prefix: int_
postfix: ' '
fields:
- name: field1.1.1
datatype: list
range: 11-20
step: 1
prefix: int_
postfix: ' '
- name: field2
datatype: list
range: 1.1-9.7
step: 0.01
prefix: float_
postfix: ' '
- name: field3
datatype: list
range: a-f
step: 2
prefix: char_
postfix: ' '
- name: field4
datatype: timestamp
range: 20190101,20200415-20200416
step: 10000
prefix: tm_
postfix: ' '
- name: field5
datatype: ip
range: 192.168.[0-9].[1-254]
step: 10000
prefix: tm_
postfix: ' '
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册