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

refactoring data generation codes

上级 1c4a5630
......@@ -4,7 +4,7 @@ version: 1.0
fields:
- field: f1
range: 0-9
range: 0-9:R
loop: 1-3
prefix: a-c
postfix: "\t"
......@@ -13,7 +13,7 @@ fields:
postfix: "\t"
- field: union_false
# mode: r
mode: r
postfix: "\t"
fields:
- field: child1
......@@ -26,7 +26,7 @@ fields:
prefix: part2_
- field: union_true
# mode: r
mode: p
union: true
fields:
- field: child1
......
......@@ -2,20 +2,19 @@ package ctrl
import (
constant "github.com/easysoft/zendata/internal/pkg/const"
"github.com/easysoft/zendata/internal/pkg/gen"
"github.com/easysoft/zendata/internal/pkg/model"
"github.com/easysoft/zendata/internal/pkg/service"
)
type FieldCtrl struct {
Field *model.DefField
FieldService *service.FieldService
ValueService *service.ValueService
Field *model.DefField
FieldService *service.FieldService
ValueService *service.ValueService
ListService *service.ListService `inject:""`
ArticleService *service.ArticleService `inject:""`
}
func (c *FieldCtrl) CreateField() {
fieldWithValue := model.FieldWithValues{}
if c.Field.Type == "" { // set default
c.Field.Type = constant.FieldTypeList
}
......@@ -27,9 +26,9 @@ func (c *FieldCtrl) CreateField() {
}
if c.Field.Type == constant.FieldTypeList {
gen.CreateListField(c.Field, &fieldWithValue)
c.ListService.CreateListField(c.Field)
} else if c.Field.Type == constant.FieldTypeArticle {
gen.CreateArticleField(c.Field, &fieldWithValue)
c.ArticleService.CreateArticleField(c.Field)
} else if c.Field.Type == constant.FieldTypeTimestamp {
c.ValueService.CreateTimestampField(c.Field)
} else if c.Field.Type == constant.FieldTypeUlid {
......
......@@ -7,7 +7,7 @@ import (
func GetRandFieldSection(pth string) (key int) {
max := 0
for k, v := range vari.RandFieldSectionShortKeysToPathMap {
for k, v := range vari.GlobalVars.RandFieldSectionShortKeysToPathMap {
if pth == v {
key = k
return
......
......@@ -235,8 +235,8 @@ func CreateValuesFromLiteral(field *model.DefField, desc string, stepStr string,
mp := PlaceholderMapForRandValues("list", elemArr, "", "", "", "",
field.Format, repeat, repeatTag)
vari.RandFieldSectionShortKeysToPathMap[key] = pth
vari.RandFieldSectionPathToValuesMap[key] = mp
vari.GlobalVars.RandFieldSectionShortKeysToPathMap[key] = pth
vari.GlobalVars.RandFieldSectionPathToValuesMap[key] = mp
return
}
......@@ -301,8 +301,8 @@ func CreateValuesFromInterval(field *model.DefField, desc, stepStr string, repea
mp := PlaceholderMapForRandValues(dataType, strItems, startStr, endStr, fmt.Sprintf("%v", step),
strconv.Itoa(precision), field.Format, repeat, repeatTag)
vari.RandFieldSectionShortKeysToPathMap[key] = pth
vari.RandFieldSectionPathToValuesMap[key] = mp
vari.GlobalVars.RandFieldSectionShortKeysToPathMap[key] = pth
vari.GlobalVars.RandFieldSectionPathToValuesMap[key] = mp
return
}
......
......@@ -238,7 +238,7 @@ func replacePlaceholder(col string) string {
func getValForPlaceholder(placeholderStr string, count int) []string {
placeholderInt, _ := strconv.Atoi(placeholderStr)
mp := vari.RandFieldSectionPathToValuesMap[placeholderInt]
mp := vari.GlobalVars.RandFieldSectionPathToValuesMap[placeholderInt]
tp := mp["type"].(string)
repeatObj := mp["repeat"]
......
......@@ -8,9 +8,10 @@ import (
)
type CombineService struct {
ExpressionService *ExpressionService `inject:""`
LoopService *LoopService `inject:""`
OutputService *OutputService `inject:""`
ExpressionService *ExpressionService `inject:""`
LoopService *LoopService `inject:""`
OutputService *OutputService `inject:""`
PlaceholderService *PlaceholderService `inject:""`
}
func (s *CombineService) CombineChildrenIfNeeded(field *model.DefField) {
......
......@@ -18,6 +18,7 @@ type FieldService struct {
FixService *FixService `inject:""`
LoopService *LoopService `inject:""`
ListService *ListService `inject:""`
RangeService *RangeService `inject:""`
RandomService *RandomService `inject:""`
}
......@@ -123,7 +124,7 @@ func (s *FieldService) CreateField(field *model.DefField) {
}
if field.Type == consts.FieldTypeList {
s.CreateListFieldValues(field)
s.ListService.CreateListFieldValues(field)
} else if field.Type == consts.FieldTypeArticle {
s.ArticleService.CreateArticleField(field)
......@@ -136,14 +137,6 @@ func (s *FieldService) CreateField(field *model.DefField) {
return
}
func (s *FieldService) CreateListFieldValues(field *model.DefField) {
if strings.Index(field.Range, ".txt") > -1 {
s.TextService.CreateFieldValuesFromText(field)
} else {
s.RangeService.CreateFieldValuesFromRange(field)
}
}
func (s *FieldService) GenValuesForConfig(field *model.DefField) (values []interface{}) {
groupValues := vari.GlobalVars.ResData[field.Config]
......
package service
import (
"github.com/easysoft/zendata/internal/pkg/model"
"strings"
)
type ListService struct {
TextService *TextService `inject:""`
RangeService *RangeService `inject:""`
}
func (s *ListService) CreateListField(field *model.DefField) {
if len(field.Fields) > 0 {
for _, child := range field.Fields {
s.CreateListField(&child)
}
} else {
s.CreateListFieldValues(field)
}
}
func (s *ListService) CreateListFieldValues(field *model.DefField) {
if strings.Index(field.Range, ".txt") > -1 {
s.TextService.CreateFieldValuesFromText(field)
} else {
s.RangeService.CreateFieldValuesFromRange(field)
}
}
......@@ -8,7 +8,8 @@ import (
)
type OutputService struct {
CombineService *CombineService `inject:""`
CombineService *CombineService `inject:""`
PlaceholderService *PlaceholderService `inject:""`
}
func (s *OutputService) GenJson(def *model.DefData) {
......@@ -36,10 +37,13 @@ func (s *OutputService) GenJson(def *model.DefData) {
}
func (s *OutputService) GenFieldMap(field *model.DefField, mp *map[string]interface{}, i int) {
if field.Union || len(field.Fields) == 0 {
(*mp)[field.Field] = field.Values[i%len(field.Values)]
if field.Union || len(field.Fields) == 0 { // set values
val := field.Values[i%len(field.Values)]
val = s.PlaceholderService.ReplacePlaceholder(val.(string))
} else {
(*mp)[field.Field] = val
} else { // set child object
childMap := map[string]interface{}{}
for _, child := range field.Fields {
......
......@@ -14,7 +14,9 @@ func (s *OutputService) GenRows(def *model.DefData) {
s.CombineService.CombineChildrenIfNeeded(&simulatedFieldFromDef)
for _, item := range simulatedFieldFromDef.Values {
logUtils.PrintLine(item.(string) + "\n")
line := s.PlaceholderService.ReplacePlaceholder(item.(string))
logUtils.PrintLine(line + "\n")
}
return
......
package service
import (
"fmt"
"github.com/easysoft/zendata/internal/pkg/gen/helper"
"github.com/easysoft/zendata/pkg/utils/vari"
"regexp"
"strconv"
"strings"
)
type PlaceholderService struct {
}
func (s *PlaceholderService) ReplacePlaceholder(val string) (ret string) {
ret = val
re := regexp.MustCompile("(?siU)\\${(.*)}")
matchResultArr := re.FindAllStringSubmatch(ret, -1)
matchTimes := len(matchResultArr)
for _, childArr := range matchResultArr {
placeholderStr := childArr[1]
values := s.getValForPlaceholder(placeholderStr, matchTimes)
for _, str := range values {
key, _ := strconv.Atoi(placeholderStr)
temp := s.PlaceholderStr(key)
ret = strings.Replace(ret, temp, str, 1)
}
}
return
}
func (s *PlaceholderService) getValForPlaceholder(placeholderStr string, count int) []string {
placeholderNo, _ := strconv.Atoi(placeholderStr)
mp := vari.GlobalVars.RandFieldSectionPathToValuesMap[placeholderNo]
tp := mp["type"].(string)
repeatObj := mp["repeat"]
repeat := 1
if repeatObj != nil {
repeat = repeatObj.(int)
}
strArr := make([]string, 0)
repeatTag := mp["repeatTag"].(string)
if tp == "int" {
start := mp["start"].(string)
end := mp["end"].(string)
precision := mp["precision"].(string)
format := mp["format"].(string)
strArr = helper.GetRandFromRange("int", start, end, "1",
repeat, repeatTag, precision, format, count)
} else if tp == "float" {
start := mp["start"].(string)
end := mp["end"].(string)
stepStr := fmt.Sprintf("%v", mp["step"])
precision := mp["precision"].(string)
format := mp["format"].(string)
strArr = helper.GetRandFromRange("float", start, end, stepStr,
repeat, repeatTag, precision, format, count)
} else if tp == "char" {
start := mp["start"].(string)
end := mp["end"].(string)
precision := mp["precision"].(string)
format := mp["format"].(string)
strArr = helper.GetRandFromRange("char", start, end, "1",
repeat, repeatTag, precision, format, count)
} else if tp == "list" {
list := mp["list"].([]string)
strArr = helper.GetRandFromList(list, repeat, count)
}
strArr = strArr[:count]
return strArr
}
func (s *PlaceholderService) PlaceholderStr(key int) string {
return fmt.Sprintf("${%d}", key)
}
func (s *PlaceholderService) PlaceholderMapForRandValues(tp string, list []string, start, end, step, precision, format string,
repeat int, repeatTag string) map[string]interface{} {
ret := map[string]interface{}{}
ret["type"] = tp
// for literal values
ret["list"] = list
// for interval values
ret["start"] = start
ret["end"] = end
ret["step"] = step
ret["precision"] = precision
ret["format"] = format
ret["repeat"] = repeat
ret["repeatTag"] = repeatTag
return ret
}
func (s *PlaceholderService) GetRandFieldSectionKey(pth string) (key int) {
max := 0
for k, v := range vari.GlobalVars.RandFieldSectionShortKeysToPathMap {
if pth == v {
key = k
return
}
if k > max {
max = k
}
}
if key == 0 {
key = max + 1
}
return
}
......@@ -18,6 +18,7 @@ import (
)
type RangeService struct {
PlaceholderService *PlaceholderService `inject:""`
}
func (s *RangeService) CreateFieldValuesFromRange(field *model.DefField) {
......@@ -86,11 +87,11 @@ func (s *RangeService) CreateValuesFromLiteral(field *model.DefField, desc strin
key := helper.GetRandFieldSection(pth)
items = append(items, gen.Placeholder(key))
mp := gen.PlaceholderMapForRandValues("list", elemArr, "", "", "", "",
mp := s.PlaceholderService.PlaceholderMapForRandValues("list", elemArr, "", "", "", "",
field.Format, repeat, repeatTag)
vari.RandFieldSectionShortKeysToPathMap[key] = pth
vari.RandFieldSectionPathToValuesMap[key] = mp
vari.GlobalVars.RandFieldSectionShortKeysToPathMap[key] = pth
vari.GlobalVars.RandFieldSectionPathToValuesMap[key] = mp
return
}
......@@ -144,7 +145,7 @@ func (s *RangeService) CreateValuesFromInterval(field *model.DefField, desc, ste
pth := field.Path + "->" + desc
key := helper.GetRandFieldSection(pth)
val := gen.Placeholder(key)
val := s.PlaceholderService.PlaceholderStr(key)
strItems := make([]string, 0)
//for i := 0; i < repeat*count; i++ { // chang to add only one placeholder item
......@@ -152,11 +153,11 @@ func (s *RangeService) CreateValuesFromInterval(field *model.DefField, desc, ste
strItems = append(strItems, val)
//}
mp := gen.PlaceholderMapForRandValues(dataType, strItems, startStr, endStr, fmt.Sprintf("%v", step),
mp := s.PlaceholderService.PlaceholderMapForRandValues(dataType, strItems, startStr, endStr, fmt.Sprintf("%v", step),
strconv.Itoa(precision), field.Format, repeat, repeatTag)
vari.RandFieldSectionShortKeysToPathMap[key] = pth
vari.RandFieldSectionPathToValuesMap[key] = mp
vari.GlobalVars.RandFieldSectionShortKeysToPathMap[key] = pth
vari.GlobalVars.RandFieldSectionPathToValuesMap[key] = mp
return
}
......
......@@ -20,16 +20,22 @@ type GenVarType struct {
DefData model.DefData
ResData map[string]map[string][]interface{}
CacheResFileToMap map[string]map[string][]interface{}
StartTime time.Time
EndTime time.Time
CacheResFileToMap map[string]map[string][]interface{}
RandFieldSectionPathToValuesMap map[int]map[string]interface{}
RandFieldSectionShortKeysToPathMap map[int]string
StartTime time.Time
EndTime time.Time
}
var (
GlobalVars = GenVarType{
DefData: model.DefData{},
CacheResFileToMap: map[string]map[string][]interface{}{},
OutputFormat: consts.FormatText,
DefData: model.DefData{},
OutputFormat: consts.FormatText,
CacheResFileToMap: map[string]map[string][]interface{}{},
RandFieldSectionPathToValuesMap: map[int]map[string]interface{}{},
RandFieldSectionShortKeysToPathMap: map[int]string{},
}
)
......@@ -70,11 +76,9 @@ var (
Ip string
Port int
ResLoading = false
Res = map[string]map[string][]string{}
RandFieldSectionPathToValuesMap = map[int]map[string]interface{}{}
RandFieldSectionShortKeysToPathMap = map[int]string{}
TopFieldMap = map[string]model.DefField{}
ResLoading = false
Res = map[string]map[string][]string{}
TopFieldMap = map[string]model.DefField{}
CacheResFileToMap = map[string]map[string][]string{}
CacheResFileToName = map[string]string{}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册