Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
易企天创
zendata
提交
16b4177d
Z
zendata
项目概览
易企天创
/
zendata
9 个月 前同步成功
通知
17
Star
2
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Z
zendata
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
16b4177d
编写于
4月 28, 2022
作者:
fantasy_cs
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: Support for key range of prefix/suffix
上级
e329ceb3
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
175 addition
and
17 deletion
+175
-17
.gitignore
.gitignore
+1
-0
demo/02.1_fix_range.yaml
demo/02.1_fix_range.yaml
+30
-0
src/gen/generator.go
src/gen/generator.go
+85
-14
src/gen/list.go
src/gen/list.go
+50
-3
src/model/domain.go
src/model/domain.go
+9
-0
未找到文件。
.gitignore
浏览文件 @
16b4177d
/log/
/.idea/
/.vscode/
/build/
/tmp/cache/.data.db-shm
/tmp/cache/.data.db-wal
...
...
demo/02.1_fix_range.yaml
0 → 100644
浏览文件 @
16b4177d
# 字段前后缀可以使用简单的区间功能,如 A-Z, 1-9,支持多片段,也支持随机或步长。对于引用文件的复杂方式不支持,这种情况建议使用多字段组合实现
fields
:
-
field
:
nesting
# 这种简单的两个字段组合,可以改为前缀的方式实现。
fields
:
-
field
:
child1
range
:
1-3
-
field
:
child
range
:
A-E
postfix
:
___
-
field
:
range_n_m
# 前后缀支持区间
prefix
:
1-3
range
:
A-E
postfix
:
___
-
field
:
range_mul
# 区间多片段
prefix
:
1-2, a-d
range
:
A-E
postfix
:
___
-
field
:
step
# 区间步长
prefix
:
1-6:2
range
:
A-E
postfix
:
___
-
field
:
random
# 区间随机
prefix
:
1-3:R
range
:
A-E
# 此处使用反引号,生成字符串"1-9"和"a-z"。注意:需要用[]符号扩起,否则不是合法的YAML文件格式。
postfix
:
___
\ No newline at end of file
src/gen/generator.go
浏览文件 @
16b4177d
...
...
@@ -3,6 +3,11 @@ package gen
import
(
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/easysoft/zendata/src/gen/helper"
"github.com/easysoft/zendata/src/model"
commonUtils
"github.com/easysoft/zendata/src/utils/common"
...
...
@@ -14,10 +19,6 @@ import (
"github.com/easysoft/zendata/src/utils/vari"
"github.com/fatih/color"
"github.com/mattn/go-runewidth"
"regexp"
"strconv"
"strings"
"time"
)
func
GenerateFromContent
(
fileContents
[][]
byte
,
fieldsToExport
*
[]
string
)
(
...
...
@@ -135,6 +136,9 @@ func GenerateFromYaml(files []string, fieldsToExport *[]string) (
}
func
GenerateForFieldRecursive
(
field
*
model
.
DefField
,
withFix
bool
)
(
values
[]
string
)
{
field
.
PrefixRange
=
CreateFieldFixValuesFromList
(
field
.
Prefix
,
field
)
field
.
PostfixRange
=
CreateFieldFixValuesFromList
(
field
.
Postfix
,
field
)
if
len
(
field
.
Fields
)
>
0
{
// has sub fields
fieldNameToValuesMap
:=
map
[
string
][]
string
{}
// refer field name to values
fieldMap
:=
map
[
string
]
model
.
DefField
{}
...
...
@@ -248,30 +252,41 @@ func GenerateValuesForField(field *model.DefField) []string {
computerLoop
(
field
)
indexOfRow
:=
0
count
:=
0
uniqueTotal
:=
len
(
fieldWithValues
.
Values
)
if
l
:=
len
(
field
.
PostfixRange
.
Values
);
l
>
0
{
uniqueTotal
*=
l
}
if
l
:=
len
(
field
.
PrefixRange
.
Values
);
l
>
0
{
uniqueTotal
*=
l
}
for
{
// 2. random replacement
isRandomAndLoopEnd
:=
!
vari
.
ResLoading
&&
// ignore rand in resource
!
(
*
field
)
.
ReferToAnotherYaml
&&
(
*
field
)
.
IsRand
&&
(
*
field
)
.
LoopIndex
>
(
*
field
)
.
LoopEnd
// isNotRandomAndValOver := !(*field).IsRand && indexOfRow >= len(fieldWithValues.Values)
if
count
>=
vari
.
Total
||
count
>=
len
(
fieldWithValues
.
Values
)
||
isRandomAndLoopEnd
{
if
count
>=
vari
.
Total
||
count
>=
uniqueTotal
||
isRandomAndLoopEnd
{
for
_
,
v
:=
range
fieldWithValues
.
Values
{
str
:=
fmt
.
Sprintf
(
"%v"
,
v
)
str
=
addFix
(
str
,
field
,
true
)
str
=
addFix
(
str
,
field
,
count
,
true
)
values
=
append
(
values
,
str
)
}
break
}
// 处理格式、前后缀、loop等
val
:=
loopFieldValWithFix
(
field
,
fieldWithValues
,
&
indexOfRow
,
true
)
val
:=
loopFieldValWithFix
(
field
,
fieldWithValues
,
&
indexOfRow
,
count
,
true
)
values
=
append
(
values
,
val
)
count
++
if
count
>=
vari
.
Total
||
count
>=
len
(
fieldWithValues
.
Values
)
{
if
count
>=
vari
.
Total
||
count
>=
uniqueTotal
{
break
}
(
*
field
)
.
LoopIndex
=
(
*
field
)
.
LoopIndex
+
1
if
(
*
field
)
.
LoopIndex
>
(
*
field
)
.
LoopEnd
{
(
*
field
)
.
LoopIndex
=
(
*
field
)
.
LoopStart
...
...
@@ -349,7 +364,7 @@ func loopFieldValues(field *model.DefField, oldValues []string, total int, withF
count
:=
0
for
{
// 处理格式、前后缀、loop等
str
:=
loopFieldValWithFix
(
field
,
fieldValue
,
&
indexOfRow
,
withFix
)
str
:=
loopFieldValWithFix
(
field
,
fieldValue
,
&
indexOfRow
,
count
,
withFix
)
values
=
append
(
values
,
str
)
count
++
...
...
@@ -369,7 +384,7 @@ func loopFieldValues(field *model.DefField, oldValues []string, total int, withF
}
func
loopFieldValWithFix
(
field
*
model
.
DefField
,
fieldValue
model
.
FieldWithValues
,
indexOfRow
*
int
,
withFix
bool
)
(
loopStr
string
)
{
indexOfRow
*
int
,
count
int
,
withFix
bool
)
(
loopStr
string
)
{
for
j
:=
0
;
j
<
(
*
field
)
.
LoopIndex
;
j
++
{
if
loopStr
!=
""
{
...
...
@@ -385,14 +400,14 @@ func loopFieldValWithFix(field *model.DefField, fieldValue model.FieldWithValues
*
indexOfRow
++
}
loopStr
=
addFix
(
loopStr
,
field
,
withFix
)
loopStr
=
addFix
(
loopStr
,
field
,
count
,
withFix
)
return
}
func
addFix
(
str
string
,
field
*
model
.
DefField
,
withFix
bool
)
(
ret
string
)
{
prefix
:=
field
.
Prefix
postfix
:=
field
.
Postfix
func
addFix
(
str
string
,
field
*
model
.
DefField
,
count
int
,
withFix
bool
)
(
ret
string
)
{
prefix
:=
GetStrValueFromRange
(
field
.
PrefixRange
,
count
)
postfix
:=
GetStrValueFromRange
(
field
.
PostfixRange
,
count
)
divider
:=
field
.
Divider
if
field
.
Length
>
runewidth
.
StringWidth
(
str
)
{
...
...
@@ -409,6 +424,62 @@ func addFix(str string, field *model.DefField, withFix bool) (ret string) {
return
}
func
GetStrValueFromRange
(
rang
*
model
.
Range
,
index
int
)
string
{
if
len
(
rang
.
Values
)
==
0
{
return
""
}
idx
:=
index
%
len
(
rang
.
Values
)
x
:=
rang
.
Values
[
idx
]
return
convPrefixVal2Str
(
x
,
""
)
}
func
convPrefixVal2Str
(
val
interface
{},
format
string
)
string
{
str
:=
"n/a"
success
:=
false
switch
val
.
(
type
)
{
case
int64
:
if
format
!=
""
{
str
,
success
=
stringUtils
.
FormatStr
(
format
,
val
.
(
int64
),
0
)
}
if
!
success
{
str
=
strconv
.
FormatInt
(
val
.
(
int64
),
10
)
}
case
float64
:
precision
:=
0
if
format
!=
""
{
str
,
success
=
stringUtils
.
FormatStr
(
format
,
val
.
(
float64
),
precision
)
}
if
!
success
{
str
=
strconv
.
FormatFloat
(
val
.
(
float64
),
'f'
,
precision
,
64
)
}
case
byte
:
str
=
string
(
val
.
(
byte
))
if
format
!=
""
{
str
,
success
=
stringUtils
.
FormatStr
(
format
,
str
,
0
)
}
if
!
success
{
str
=
string
(
val
.
(
byte
))
}
case
string
:
str
=
val
.
(
string
)
match
,
_
:=
regexp
.
MatchString
(
"%[0-9]*d"
,
format
)
if
match
{
valInt
,
err
:=
strconv
.
Atoi
(
str
)
if
err
==
nil
{
str
,
success
=
stringUtils
.
FormatStr
(
format
,
valInt
,
0
)
}
}
else
{
str
,
success
=
stringUtils
.
FormatStr
(
format
,
str
,
0
)
}
default
:
}
return
str
}
func
GenerateFieldVal
(
field
model
.
DefField
,
fieldValue
model
.
FieldWithValues
,
index
*
int
)
(
val
string
,
err
error
)
{
// 叶节点
if
len
(
fieldValue
.
Values
)
==
0
{
...
...
src/gen/list.go
浏览文件 @
16b4177d
package
gen
import
(
"math"
"strconv"
"strings"
"github.com/easysoft/zendata/src/model"
commonUtils
"github.com/easysoft/zendata/src/utils/common"
constant
"github.com/easysoft/zendata/src/utils/const"
fileUtils
"github.com/easysoft/zendata/src/utils/file"
"github.com/easysoft/zendata/src/utils/vari"
"math"
"strconv"
"strings"
)
func
CreateListField
(
field
*
model
.
DefField
,
fieldWithValue
*
model
.
FieldWithValues
)
{
...
...
@@ -84,6 +85,52 @@ func CreateFieldValuesFromList(field *model.DefField, fieldValue *model.FieldWit
}
}
func
CreateFieldFixValuesFromList
(
strRang
string
,
field
*
model
.
DefField
)
(
rang
*
model
.
Range
)
{
rang
=
&
model
.
Range
{}
if
strRang
==
""
{
return
}
rangeSections
:=
ParseRangeProperty
(
strRang
)
// parse 1
index
:=
0
for
_
,
rangeSection
:=
range
rangeSections
{
if
index
>=
constant
.
MaxNumb
{
break
}
if
rangeSection
==
""
{
continue
}
descStr
,
stepStr
,
count
,
countTag
:=
ParseRangeSection
(
rangeSection
)
// parse 2
if
strings
.
ToLower
(
stepStr
)
==
"r"
{
rang
.
IsRand
=
true
}
typ
,
desc
:=
ParseRangeSectionDesc
(
descStr
)
// parse 3
items
:=
make
([]
interface
{},
0
)
if
typ
==
"literal"
{
items
=
CreateValuesFromLiteral
(
field
,
desc
,
stepStr
,
count
,
countTag
)
}
else
if
typ
==
"interval"
{
items
=
CreateValuesFromInterval
(
field
,
desc
,
stepStr
,
count
,
countTag
)
}
else
if
typ
==
"yaml"
{
// load from a yaml
items
=
CreateValuesFromYaml
(
field
,
desc
,
stepStr
,
count
,
countTag
)
field
.
ReferToAnotherYaml
=
true
}
rang
.
Values
=
append
(
rang
.
Values
,
items
...
)
index
=
index
+
len
(
items
)
}
if
len
(
rang
.
Values
)
==
0
{
rang
.
Values
=
append
(
rang
.
Values
,
"N/A"
)
}
return
}
func
CheckRangeType
(
startStr
string
,
endStr
string
,
stepStr
string
)
(
dataType
string
,
step
interface
{},
precision
int
,
rand
bool
,
count
int
)
{
step
=
1
...
...
src/model/domain.go
浏览文件 @
16b4177d
...
...
@@ -108,6 +108,15 @@ type FieldSimple struct {
LoopIndex
int
`yaml:"-"`
IsRand
bool
`yaml:"-"`
ReferToAnotherYaml
bool
`yaml:"-"`
PrefixRange
*
Range
`yaml:"-"`
PostfixRange
*
Range
`yaml:"-"`
}
// add by Leo [2022/04/27]
type
Range
struct
{
Values
[]
interface
{}
IsRand
bool
}
type
FieldWithValues
struct
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录