-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.go
153 lines (130 loc) · 3.97 KB
/
models.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package settings
import (
"reflect"
"strings"
"time"
"github.com/go-playground/validator/v10"
)
// Engine — data model to process settings.
type Engine struct {
Value reflect.Value
Type reflect.Type
NumberOfFields int
Validate *validator.Validate
Field Loop
}
// newEngine creates new model to process settings.
func newEngine(settings interface{}) *Engine {
return &Engine{
Value: reflect.ValueOf(settings),
Type: reflect.TypeOf(settings),
Validate: validator.New(),
}
}
// Loop — variables that used during field processing.
type Loop struct {
envTag string
envValue string
validationRule string
durationValue time.Duration
int64Value int64
uint64Value uint64
field reflect.StructField
value reflect.Value
hasEnvTag bool
mustBeOmitted bool
hasEnvValue bool
mustBeValidated bool
required bool
defaultSetting string
hasDefaultSetting bool
float64Value float64
}
// exceedsMaximumUint returns true if the value exceeds the maximum uint value.
func (field *Loop) exceedsMaximumUint() bool {
var kind reflect.Kind
if kind = field.value.Kind(); kind == reflect.Uint {
kind = reflect.Uint64
}
//nolint:gomnd // it is a formula
return field.uint64Value > 1<<(2<<(uint64(kind)-6))-1
}
// notInIntRange returns true if the value is not in the int range.
func (field *Loop) notInIntRange() bool {
var kind reflect.Kind
if kind = field.value.Kind(); kind == reflect.Int {
kind = reflect.Int64
}
//nolint:gomnd // it is a formula
return field.int64Value > 1<<((2<<(int64(kind)-1))-1)-1 ||
field.int64Value < -1<<((2<<(int64(kind)-1))-1)
}
// getStruct checks and returns a struct to process.
func (engine *Engine) getStruct() error {
if engine.Value.Kind() == reflect.Ptr {
// initing struct if it is added via pointer
if engine.Value.IsNil() {
// the main struct must be inited
// the third law of reflection
// https://blog.golang.org/laws-of-reflection
// we have no parameter to change value
if !engine.Value.CanSet() {
return ErrNotAddressable
}
newStruct := reflect.New(engine.Type.Elem())
newValue := reflect.ValueOf(newStruct.Interface())
engine.Value.Set(newValue)
}
engine.Value = engine.Value.Elem()
engine.Type = engine.Type.Elem()
err := engine.getStruct()
if err != nil {
return err
}
}
// checking that kind us a struct
if engine.Type.Kind() != reflect.Struct {
return ErrNotAStruct
}
// checking the number of the fields in the struct.
engine.NumberOfFields = engine.Type.NumField()
if engine.NumberOfFields == 0 {
return ErrTheModelHasEmptyStruct
}
return nil
}
// validationFailedError forms validation error.
func (engine *Engine) validationFailed() error {
return &validationFailedError{
Name: engine.Field.field.Name,
Type: engine.Field.value.Type().String(),
ValidationRule: engine.Field.validationRule,
}
}
func (engine *Engine) validateRequired() {
engine.Field.required = false
// receiving the 'validate' tag value
engine.Field.validationRule, engine.Field.mustBeValidated = engine.Field.field.Tag.Lookup("validate")
// process validation rule to ascertain the required status
rules := strings.Split(engine.Field.validationRule, ",")
for _, value := range rules {
if value == required {
engine.Field.required = true
break
}
}
}
// startIteration launches field processing.
func (engine *Engine) startIteration(i int) {
engine.Field.field = engine.Type.Field(i)
engine.Field.value = engine.Value.FieldByName(engine.Field.field.Name)
// receiving env tag
engine.Field.envTag, engine.Field.hasEnvTag = engine.Field.field.Tag.Lookup(env)
if engine.Field.hasEnvTag && engine.Field.envTag == omit {
engine.Field.mustBeOmitted = true
return
}
engine.Field.mustBeOmitted = false
// receiving default setting
engine.Field.defaultSetting, engine.Field.hasDefaultSetting = engine.Field.field.Tag.Lookup(defaultSetting)
}