Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
易企天创
zendata
提交
33c735a1
Z
zendata
项目概览
易企天创
/
zendata
8 个月 前同步成功
通知
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 搜索 >>
提交
33c735a1
编写于
9月 17, 2020
作者:
aaronchen2k2k
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
close task#7906
上级
32631ab8
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
109 addition
and
10 deletion
+109
-10
demo/test/test_format.yaml
demo/test/test_format.yaml
+31
-0
src/gen/generator.go
src/gen/generator.go
+6
-4
src/gen/rand.go
src/gen/rand.go
+3
-3
src/utils/string/string.go
src/utils/string/string.go
+69
-3
未找到文件。
demo/test/test_format.yaml
0 → 100644
浏览文件 @
33c735a1
title
:
测试
desc
:
author
:
zentao
version
:
1.0
fields
:
-
field
:
field_format_exp
range
:
1-10
format
:
"
passwd%02d"
postfix
:
"
\t
"
-
field
:
field_format_func_md5
range
:
a-z
format
:
md5
postfix
:
"
\t
"
-
field
:
field_format_func_sha1
range
:
password
format
:
sha1
postfix
:
"
\t
"
-
field
:
field_format_func_base64
range
:
pass/?&=word+
format
:
base64
postfix
:
"
\t
"
-
field
:
field_format_func_urlencode
range
:
pass/?&=word+
format
:
urlencode
postfix
:
"
\t
"
\ No newline at end of file
src/gen/generator.go
浏览文件 @
33c735a1
...
...
@@ -226,7 +226,7 @@ func GetFieldValStr(field model.DefField, val interface{}) string {
switch
val
.
(
type
)
{
case
int64
:
if
field
.
Format
!=
""
{
str
,
success
=
stringUtils
.
FormatStr
(
field
.
Format
,
val
.
(
int64
))
str
,
success
=
stringUtils
.
FormatStr
(
field
.
Format
,
val
.
(
int64
)
,
0
)
}
if
!
success
{
str
=
strconv
.
FormatInt
(
val
.
(
int64
),
10
)
...
...
@@ -237,7 +237,7 @@ func GetFieldValStr(field model.DefField, val interface{}) string {
precision
=
field
.
Precision
}
if
field
.
Format
!=
""
{
str
,
success
=
stringUtils
.
FormatStr
(
field
.
Format
,
val
.
(
float64
))
str
,
success
=
stringUtils
.
FormatStr
(
field
.
Format
,
val
.
(
float64
)
,
precision
)
}
if
!
success
{
str
=
strconv
.
FormatFloat
(
val
.
(
float64
),
'f'
,
precision
,
64
)
...
...
@@ -245,7 +245,7 @@ func GetFieldValStr(field model.DefField, val interface{}) string {
case
byte
:
str
=
string
(
val
.
(
byte
))
if
field
.
Format
!=
""
{
str
,
success
=
stringUtils
.
FormatStr
(
field
.
Format
,
str
)
str
,
success
=
stringUtils
.
FormatStr
(
field
.
Format
,
str
,
0
)
}
if
!
success
{
str
=
string
(
val
.
(
byte
))
...
...
@@ -257,8 +257,10 @@ func GetFieldValStr(field model.DefField, val interface{}) string {
if
match
{
valInt
,
err
:=
strconv
.
Atoi
(
str
)
if
err
==
nil
{
str
,
success
=
stringUtils
.
FormatStr
(
field
.
Format
,
valInt
)
str
,
success
=
stringUtils
.
FormatStr
(
field
.
Format
,
valInt
,
0
)
}
}
else
{
str
,
success
=
stringUtils
.
FormatStr
(
field
.
Format
,
str
,
0
)
}
default
:
}
...
...
src/gen/rand.go
浏览文件 @
33c735a1
...
...
@@ -53,7 +53,7 @@ func GetRandFromRange(dataType, start, end, step, repeatStr, precisionStr string
items
:=
make
([]
string
,
0
)
item
:=
strconv
.
FormatInt
(
val
,
10
)
if
format
!=
""
{
formatVal
,
success
:=
stringUtils
.
FormatStr
(
format
,
val
)
formatVal
,
success
:=
stringUtils
.
FormatStr
(
format
,
val
,
0
)
if
success
{
item
=
formatVal
}
}
...
...
@@ -85,7 +85,7 @@ func GetRandFromRange(dataType, start, end, step, repeatStr, precisionStr string
item
:=
string
(
val
)
if
format
!=
""
{
formatVal
,
success
:=
stringUtils
.
FormatStr
(
format
,
val
)
formatVal
,
success
:=
stringUtils
.
FormatStr
(
format
,
val
,
0
)
if
success
{
item
=
formatVal
}
}
...
...
@@ -120,7 +120,7 @@ func GetRandFromRange(dataType, start, end, step, repeatStr, precisionStr string
item
:=
strconv
.
FormatFloat
(
val
,
'f'
,
precision
,
64
)
if
format
!=
""
{
formatVal
,
success
:=
stringUtils
.
FormatStr
(
format
,
val
)
formatVal
,
success
:=
stringUtils
.
FormatStr
(
format
,
val
,
precision
)
if
success
{
item
=
formatVal
}
}
...
...
src/utils/string/string.go
浏览文件 @
33c735a1
...
...
@@ -2,6 +2,9 @@ package stringUtils
import
(
"bytes"
"crypto/md5"
"crypto/sha1"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"fmt"
...
...
@@ -104,13 +107,35 @@ func GetNumbWidth(numb int) int {
return
width
}
func
FormatStr
(
format
string
,
val
interface
{})
(
string
,
bool
)
{
str
:=
fmt
.
Sprintf
(
format
,
val
)
func
FormatStr
(
format
string
,
val
interface
{},
precision
int
)
(
ret
string
,
pass
bool
)
{
format
=
strings
.
ToLower
(
strings
.
TrimSpace
(
format
))
if
strings
.
Index
(
format
,
"md5"
)
==
0
{
str
:=
interfaceToStr
(
val
,
precision
)
ret
=
Md5
(
str
)
pass
=
true
return
}
else
if
strings
.
Index
(
format
,
"sha1"
)
==
0
{
str
:=
interfaceToStr
(
val
,
precision
)
ret
=
Sha1
(
str
)
pass
=
true
return
}
else
if
strings
.
Index
(
format
,
"base64"
)
==
0
{
str
:=
interfaceToStr
(
val
,
precision
)
ret
=
Base64
(
str
)
pass
=
true
return
}
else
if
strings
.
Index
(
format
,
"urlencode"
)
==
0
{
str
:=
interfaceToStr
(
val
,
precision
)
ret
=
UrlEncode
(
str
)
pass
=
true
return
}
str
:=
fmt
.
Sprintf
(
format
,
val
)
if
strings
.
Index
(
str
,
"%!"
)
==
0
{
return
""
,
false
}
return
str
,
true
}
...
...
@@ -171,4 +196,45 @@ func GetPinyin(word string) string {
p
,
_
:=
pinyin
.
New
(
word
)
.
Split
(
""
)
.
Mode
(
pinyin
.
WithoutTone
)
.
Convert
()
return
p
}
func
interfaceToStr
(
intf
interface
{},
precision
int
)
(
ret
string
)
{
switch
intf
.
(
type
)
{
case
int64
:
return
strconv
.
FormatInt
(
intf
.
(
int64
),
10
)
case
float64
:
return
strconv
.
FormatFloat
(
intf
.
(
float64
),
'f'
,
precision
,
64
)
case
byte
:
return
string
(
intf
.
(
byte
))
case
string
:
return
intf
.
(
string
)
default
:
return
intf
.
(
string
)
}
}
func
Md5
(
str
string
)
(
ret
string
)
{
h
:=
md5
.
New
()
h
.
Write
([]
byte
(
str
))
ret
=
hex
.
EncodeToString
(
h
.
Sum
(
nil
))
return
}
func
Sha1
(
str
string
)
(
ret
string
)
{
h
:=
sha1
.
New
()
h
.
Write
([]
byte
(
str
))
bs
:=
h
.
Sum
(
nil
)
ret
=
fmt
.
Sprintf
(
"%x"
,
bs
)
return
}
func
Base64
(
str
string
)
(
ret
string
)
{
ret
=
base64
.
StdEncoding
.
EncodeToString
([]
byte
(
str
))
return
}
func
UrlEncode
(
str
string
)
(
ret
string
)
{
ret
=
base64
.
URLEncoding
.
EncodeToString
([]
byte
(
str
))
return
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录