forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
int.go
94 lines (78 loc) · 2.57 KB
/
int.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
package validate
import (
"fmt"
"math"
"github.com/hashicorp/terraform/helper/schema"
)
func IntBetweenAndNot(min, max, not int) schema.SchemaValidateFunc {
return func(i interface{}, k string) (_ []string, errors []error) {
v, ok := i.(int)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be int", k))
return
}
if v < min || v > max {
errors = append(errors, fmt.Errorf("expected %s to be in the range (%d - %d), got %d", k, min, max, v))
return
}
if v == not {
errors = append(errors, fmt.Errorf("expected %s to not be %d, got %d", k, not, v))
return
}
return
}
}
// IntBetweenAndDivisibleBy returns a SchemaValidateFunc which tests if the provided value
// is of type int and is between min and max (inclusive) and is divisible by a given number
func IntBetweenAndDivisibleBy(min, max, divisor int) schema.SchemaValidateFunc { // nolint: unparam
return func(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(int)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be int", k))
return
}
if v < min || v > max {
errors = append(errors, fmt.Errorf("expected %s to be in the range (%d - %d), got %d", k, min, max, v))
return
}
if math.Mod(float64(v), float64(divisor)) != 0 {
errors = append(errors, fmt.Errorf("expected %s to be divisible by %d", k, divisor))
return
}
return warnings, errors
}
}
// IntDivisibleBy returns a SchemaValidateFunc which tests if the provided value
// is of type int and is divisible by a given number
func IntDivisibleBy(divisor int) schema.SchemaValidateFunc { // nolint: unparam
return func(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(int)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be int", k))
return
}
if math.Mod(float64(v), float64(divisor)) != 0 {
errors = append(errors, fmt.Errorf("expected %s to be divisible by %d", k, divisor))
return
}
return warnings, errors
}
}
// IntInSlice returns a SchemaValidateFunc which tests if the provided value
// is of type int and matches the value of an element in the valid slice
func IntInSlice(valid []int) schema.SchemaValidateFunc {
return func(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(int)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be int", k))
return
}
for _, str := range valid {
if v == str {
return
}
}
errors = append(errors, fmt.Errorf("expected %q to be one of %v, got %v", k, valid, v))
return warnings, errors
}
}