Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
易企天创
zendata
提交
a2996516
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 搜索 >>
提交
a2996516
编写于
3月 09, 2021
作者:
陈
陈琦
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add pad before deal with prefix and postfix
上级
42998686
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
35 addition
and
29 deletion
+35
-29
.zd.conf
.zd.conf
+1
-1
demo/default.yaml
demo/default.yaml
+2
-2
src/gen/generator.go
src/gen/generator.go
+4
-0
src/gen/helper/expression.go
src/gen/helper/expression.go
+6
-5
src/gen/printer.go
src/gen/printer.go
+1
-1
src/gen/writer.go
src/gen/writer.go
+6
-6
src/utils/string/string.go
src/utils/string/string.go
+15
-14
未找到文件。
.zd.conf
浏览文件 @
a2996516
Version
=
1
.
7
Version
=
1
.
7
Language
=
zh
demo/default.yaml
浏览文件 @
a2996516
...
...
@@ -94,10 +94,10 @@ fields:
postfix
:
"
\t
"
-
field
:
field_length
# 指定宽度。
range
:
1-99
# 0
1\t,02\t,03\t..., 99\t
range
:
1-99
# 0
01\n,002\n,003\n..., 099\n
length
:
3
# 包含前后缀的宽度。
leftpad
:
0
# 宽度不够时,补充的字符。
postfix
:
"
\t
"
postfix
:
"
-
"
-
field
:
field_text
# 从一个文件中随机读取。
range
:
users.txt:R
# 相对当前文件路径。
...
...
src/gen/generator.go
浏览文件 @
a2996516
...
...
@@ -12,6 +12,7 @@ import (
stringUtils
"github.com/easysoft/zendata/src/utils/string"
"github.com/easysoft/zendata/src/utils/vari"
"github.com/fatih/color"
"github.com/mattn/go-runewidth"
"regexp"
"strconv"
"strings"
...
...
@@ -340,6 +341,9 @@ func loopFieldValWithFix(field *model.DefField, fieldValue model.FieldWithValues
*
indexOfRow
++
}
if
field
.
Length
>
runewidth
.
StringWidth
(
loopStr
)
{
loopStr
=
stringUtils
.
AddPad
(
loopStr
,
*
field
)
}
if
withFix
&&
!
vari
.
Trim
{
loopStr
=
prefix
+
loopStr
+
postfix
}
...
...
src/gen/helper/expression.go
浏览文件 @
a2996516
...
...
@@ -17,7 +17,7 @@ func GenExpressionValues(field model.DefField, valuesMap map[string][]string) (r
exp
:=
field
.
Value
reg
:=
regexp
.
MustCompile
(
`\$([_,a-z,A-Z,0-9]+)`
)
arr
:=
reg
.
FindAllStringSubmatch
(
exp
,
-
1
)
arr
:=
reg
.
FindAllStringSubmatch
(
exp
,
-
1
)
total
:=
1
for
_
,
items
:=
range
arr
{
// computer total
...
...
@@ -48,7 +48,7 @@ func GenExpressionValues(field model.DefField, valuesMap map[string][]string) (r
tp
:=
""
var
val
interface
{}
if
len
(
referValues
)
>
0
{
valStr
=
referValues
[
i
%
len
(
referValues
)]
valStr
=
referValues
[
i
%
len
(
referValues
)]
valStr
=
strings
.
TrimLeft
(
valStr
,
referField
.
Prefix
)
valStr
=
strings
.
TrimRight
(
valStr
,
referField
.
Postfix
)
...
...
@@ -71,16 +71,17 @@ func GenExpressionValues(field model.DefField, valuesMap map[string][]string) (r
}
mask
:=
""
if
dataTypeFromValues
==
"int"
{
if
dataTypeFromValues
==
"int"
{
mask
=
"%.0f"
}
else
{
mask
=
"%d"
}
str
:=
fmt
.
Sprintf
(
field
.
Prefix
+
mask
+
field
.
Postfix
,
result
)
str
:=
fmt
.
Sprintf
(
mask
,
result
)
if
field
.
Length
>
runewidth
.
StringWidth
(
str
)
{
str
=
stringUtils
.
AddPad
(
str
,
field
)
}
str
=
field
.
Prefix
+
str
+
field
.
Postfix
ret
=
append
(
ret
,
str
)
}
}
...
...
@@ -105,4 +106,4 @@ func getNumType(str string) (val interface{}, tp string) {
tp
=
"float"
return
}
\ No newline at end of file
}
src/gen/printer.go
浏览文件 @
a2996516
...
...
@@ -35,7 +35,7 @@ func Print(rows [][]string, format string, table string, colIsNumArr []bool,
col
=
replacePlaceholder
(
col
)
field
:=
vari
.
TopFieldMap
[
fields
[
j
]]
if
field
.
Length
>
runewidth
.
StringWidth
(
col
)
{
col
=
stringUtils
.
AddPad
(
col
,
field
)
//
col = stringUtils.AddPad(col, field)
}
if
j
>
0
&&
vari
.
Human
{
// use a tab
...
...
src/gen/writer.go
浏览文件 @
a2996516
...
...
@@ -5,7 +5,6 @@ import (
"github.com/360EntSecGroup-Skylar/excelize/v2"
constant
"github.com/easysoft/zendata/src/utils/const"
logUtils
"github.com/easysoft/zendata/src/utils/log"
stringUtils
"github.com/easysoft/zendata/src/utils/string"
"github.com/easysoft/zendata/src/utils/vari"
"github.com/mattn/go-runewidth"
)
...
...
@@ -13,12 +12,13 @@ import (
const
(
sheetName
=
"Sheet1"
)
var
(
csvWriter
*
csv
.
Writer
)
func
Write
(
rows
[][]
string
,
format
string
,
table
string
,
colIsNumArr
[]
bool
,
fields
[]
string
)
(
lines
[]
interface
{})
{
fields
[]
string
)
(
lines
[]
interface
{})
{
f
:=
excelize
.
NewFile
()
index
:=
f
.
NewSheet
(
sheetName
)
...
...
@@ -38,11 +38,11 @@ func Write(rows [][]string, format string, table string, colIsNumArr []bool,
col
=
replacePlaceholder
(
col
)
field
:=
vari
.
TopFieldMap
[
fields
[
j
]]
if
field
.
Length
>
runewidth
.
StringWidth
(
col
)
{
col
=
stringUtils
.
AddPad
(
col
,
field
)
//
col = stringUtils.AddPad(col, field)
}
if
format
==
constant
.
FormatExcel
{
colName
,
_
:=
excelize
.
CoordinatesToCellName
(
j
+
1
,
i
+
2
)
colName
,
_
:=
excelize
.
CoordinatesToCellName
(
j
+
1
,
i
+
2
)
f
.
SetCellValue
(
sheetName
,
colName
,
col
)
}
else
if
format
==
constant
.
FormatCsv
{
...
...
@@ -69,9 +69,9 @@ func Write(rows [][]string, format string, table string, colIsNumArr []bool,
func
printExcelHeader
(
fields
[]
string
,
f
*
excelize
.
File
)
{
headerLine
:=
""
for
idx
,
field
:=
range
fields
{
colName
,
_
:=
excelize
.
CoordinatesToCellName
(
idx
+
1
,
1
)
colName
,
_
:=
excelize
.
CoordinatesToCellName
(
idx
+
1
,
1
)
f
.
SetCellValue
(
sheetName
,
colName
,
field
)
}
logUtils
.
PrintLine
(
headerLine
)
}
\ No newline at end of file
}
src/utils/string/string.go
浏览文件 @
a2996516
...
...
@@ -13,6 +13,7 @@ import (
constant
"github.com/easysoft/zendata/src/utils/const"
"github.com/mattn/go-runewidth"
"gopkg.in/yaml.v2"
"net/url"
"regexp"
"strconv"
"strings"
...
...
@@ -72,7 +73,7 @@ func FindInArrBool(str string, arr []string) bool {
found
,
_
:=
FindInArr
(
str
,
arr
)
return
found
}
func
FindInArr
(
str
string
,
arr
[]
string
)
(
bool
,
int
)
{
func
FindInArr
(
str
string
,
arr
[]
string
)
(
bool
,
int
)
{
for
index
,
s
:=
range
arr
{
if
str
==
s
{
return
true
,
index
...
...
@@ -87,8 +88,8 @@ func StrInArr(str string, arr []string) bool {
return
found
}
func
InArray
(
need
interface
{},
arr
[]
string
)
bool
{
for
_
,
v
:=
range
arr
{
if
need
==
v
{
for
_
,
v
:=
range
arr
{
if
need
==
v
{
return
true
}
}
...
...
@@ -141,7 +142,7 @@ func FormatStr(format string, val interface{}, precision int) (ret string, pass
}
str
:=
fmt
.
Sprintf
(
format
,
val
)
if
strings
.
Index
(
str
,
"%!"
)
==
0
{
if
strings
.
Index
(
str
,
"%!"
)
==
0
{
return
""
,
false
}
...
...
@@ -149,7 +150,7 @@ func FormatStr(format string, val interface{}, precision int) (ret string, pass
}
func
AddPad
(
str
string
,
field
model
.
DefField
)
string
{
if
field
.
Length
>
0
&&
field
.
Length
>
runewidth
.
StringWidth
(
str
)
{
if
field
.
Length
>
0
&&
field
.
Length
>
runewidth
.
StringWidth
(
str
)
{
gap
:=
field
.
Length
-
len
(
str
)
if
field
.
LeftPad
!=
""
{
field
.
LeftPad
=
field
.
LeftPad
[
:
1
]
...
...
@@ -173,7 +174,7 @@ func StartWith(str, sub string) bool {
return
strings
.
Index
(
str
,
sub
)
==
0
}
func
EndWith
(
str
,
sub
string
)
bool
{
return
strings
.
Contains
(
str
,
sub
)
&&
strings
.
LastIndex
(
str
,
sub
)
==
len
(
str
)
-
len
(
sub
)
return
strings
.
Contains
(
str
,
sub
)
&&
strings
.
LastIndex
(
str
,
sub
)
==
len
(
str
)
-
len
(
sub
)
}
func
ConvertForSql
(
str
string
)
(
ret
string
)
{
...
...
@@ -181,10 +182,10 @@ func ConvertForSql(str string) (ret string) {
count
:=
0
for
i
,
item
:=
range
arr
{
if
count
%
2
==
1
&&
string
(
item
)
!=
"'"
{
ret
=
ret
+
"'"
}
else
if
i
==
len
(
arr
)
-
1
&&
count
%
2
==
0
&&
string
(
item
)
==
"'"
{
ret
=
ret
+
"'"
if
count
%
2
==
1
&&
string
(
item
)
!=
"'"
{
ret
=
ret
+
"'"
}
else
if
i
==
len
(
arr
)
-
1
&&
count
%
2
==
0
&&
string
(
item
)
==
"'"
{
ret
=
ret
+
"'"
}
if
string
(
item
)
!=
"'"
{
...
...
@@ -243,7 +244,7 @@ func Base64(str string) (ret string) {
return
}
func
UrlEncode
(
str
string
)
(
ret
string
)
{
ret
=
base64
.
URLEncoding
.
EncodeToString
([]
byte
(
str
)
)
ret
=
url
.
QueryEscape
(
str
)
return
}
...
...
@@ -261,8 +262,8 @@ func ReplaceSpecialChars(bytes []byte) []byte {
}
if
strings
.
Index
(
strings
.
TrimSpace
(
line
),
"range"
)
==
0
||
inRanges
{
line
=
strings
.
ReplaceAll
(
line
,
"["
,
string
(
constant
.
LeftBrackets
))
line
=
strings
.
ReplaceAll
(
line
,
"]"
,
string
(
constant
.
RightBrackets
))
line
=
strings
.
ReplaceAll
(
line
,
"["
,
string
(
constant
.
LeftBrackets
))
line
=
strings
.
ReplaceAll
(
line
,
"]"
,
string
(
constant
.
RightBrackets
))
}
ret
+=
line
+
"
\n
"
...
...
@@ -280,7 +281,7 @@ func ConvertYamlStringToMapFormat(bytes []byte) (ret string) {
// replace '"test"' to "test"
reg
:=
regexp
.
MustCompile
(
`([:\s]+?)'"(.*)"'`
)
//if reg.MatchString(ret) {
ret
=
reg
.
ReplaceAllString
(
ret
,
`${1}"${2}"`
)
ret
=
reg
.
ReplaceAllString
(
ret
,
`${1}"${2}"`
)
//}
return
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录