-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
103 lines (86 loc) · 2.57 KB
/
config.go
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
package main
import (
"github.com/go-playground/validator/v10"
)
const (
CsvFormat = "csv"
)
type Config struct {
TotalCount int `validate:"gt=0"`
SharedFields []Field `validate:"dive"`
Entities []Entity `validate:"required,gt=0,dive"`
}
type Entity struct {
Field Field
Config EntityConfig
csvColumnsCache []string
}
type EntityConfig struct {
// Count and Rate are optional
Count int64 `validate:"gte=0"`
// 1..100; if == 0, default is 100
Rate int `validate:"gte=0,lte=100"`
Filepath string `validate:"required"`
OutputFormat string
currentCount int64
}
type Field struct {
Name string `json:",omitempty"`
NilChance int `json:",omitempty" validate:"gte=0,lte=100"`
Type *Type `json:",omitempty"`
Fields []Field `json:",omitempty" validate:"dive"`
Array *Array `json:",omitempty"`
}
type Type struct {
Type string `json:",omitempty"`
// TODO:
// - MaskedString by gofakeit.Generate() or gofakeit.Numerify()
// - Date interval by DateRange()
Const interface{} `json:",omitempty"`
OneOf []interface{} `json:",omitempty"`
DateFormat string `json:",omitempty"`
Min int `json:",omitempty"`
Max int `json:",omitempty" validate:"omitempty"`
AsString bool `json:",omitempty"`
AsJson bool `json:",omitempty"`
Template string `json:",omitempty"`
Reference string `json:",omitempty"`
seq int64
}
type Array struct {
Value *Field
Fixed []Field `json:",omitempty" validate:"dive"`
MinLen int
MaxLen int `validate:"omitempty,gtefield=MinLen"`
}
func FieldStructLevelValidation(sl validator.StructLevel) {
field := sl.Current().Interface().(Field)
setCount := 0
if field.Type != nil {
setCount++
}
if field.Fields != nil {
setCount++
}
if field.Array != nil {
setCount++
}
switch {
case setCount == 0:
sl.ReportError(field.Name, "Struct", "", "missing_one_of_optionals", "Zero optional params set")
case setCount > 1:
sl.ReportError(field.Name, "Struct", "", "many_optional_params", "More than 1 optional params set")
}
}
func ArrayStructLevelValidation(sl validator.StructLevel) {
arr := sl.Current().Interface().(Array)
if arr.Value == nil && arr.Fixed == nil {
sl.ReportError(arr.Value, "Value", "", "missing_one_of_optionals", "Value or Fixed must be set")
}
}
func TypeStructLevelValidation(sl validator.StructLevel) {
t := sl.Current().Interface().(Type)
if t.Type == OneOfType && len(t.OneOf) == 0 {
sl.ReportError(t.OneOf, "OneOf", "", "missing_param", "'OneOf' param not set")
}
}