-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmockOption.go
More file actions
137 lines (119 loc) · 2.93 KB
/
mockOption.go
File metadata and controls
137 lines (119 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package mock
// minLen 和 maxLen 用于指定生成的mock数据的最大长度和最小长度
type MinMax struct {
// 生成的mock数据的最大长度
MaxLen int
// 生成的mock数据的最小长度
MinLen int
}
type Options struct {
// 指定字段名,指定生成的数据来源
StringSource map[string][]string
// 生成的mock数据的最大长度
MaxLen int
// 生成的mock数据的最小长度
MinLen int
// 生成的mock数据的最大值
Max int64
// 生成的mock数据的最小值
Min int64
// 指定字段名,生成最大长度
MinMaxLenByField map[string]MinMax
//指定字段生成数据范围
MinMaxByField map[string]MinMax
// country 用中文还是英文
CountryChina bool
// 默认是mock
TagName string
// 设置忽略的字段名
IgnoreFields map[string]bool
// 省份
Province string
// 城市
City string
//浮点数保留小数长度
FloatLen string
// 区
// District string
}
type Option func(*Options)
// 指定字段名,指定生成长度的范围, slice的长度和string的长度
func WithMinMaxLenByField(field string, minLen int, maxLen int) Option {
return func(o *Options) {
if o.MinMaxLenByField == nil {
o.MinMaxLenByField = make(map[string]MinMax)
}
o.MinMaxLenByField[field] = MinMax{MaxLen: maxLen, MinLen: minLen}
}
}
// 指定字段名,指定生成大小的范围,适用int uint,float下所有类型
func WithMinMaxByField(field string, minLen int, maxLen int) Option {
return func(o *Options) {
if o.MinMaxByField == nil {
o.MinMaxByField = make(map[string]MinMax)
}
o.MinMaxByField[field] = MinMax{MaxLen: maxLen, MinLen: minLen}
}
}
// 设置最大长度
func WithMaxLen(maxLen int) Option {
return func(o *Options) {
o.MaxLen = maxLen
}
}
// 设置最小长度
func WithMinLen(minLen int) Option {
return func(o *Options) {
o.MinLen = minLen
}
}
// 设置最大值
func WithMax(max int64) Option {
return func(o *Options) {
o.Max = max
}
}
// 设置最小值
func WithMin(min int64) Option {
return func(o *Options) {
o.Min = min
}
}
// 设置浮点数返回长度,默认返回两位小数
func WithFloatLen(format string) Option {
return func(o *Options) {
o.FloatLen = format
}
}
// 设置country为英文
func WithCountryEn() Option {
return func(o *Options) {
o.CountryChina = true
}
}
// 包含field字符串,就会使用source中的数据, 字段比较是string类型
func WithContainsFieldSourceString(field string, source []string) Option {
return func(o *Options) {
if o.StringSource == nil {
o.StringSource = make(map[string][]string)
}
o.StringSource[field] = source
}
}
// 设置tag名
func WithTag(tag string) Option {
return func(o *Options) {
o.TagName = tag
}
}
// 设置忽略的字段名
func WithIgnoreFields(fields ...string) Option {
return func(o *Options) {
if o.IgnoreFields == nil {
o.IgnoreFields = make(map[string]bool)
}
for _, s := range fields {
o.IgnoreFields[s] = true
}
}
}