...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package validate
16
17
18
19
20 import (
21 "reflect"
22
23 "k8s.io/kube-openapi/pkg/validation/errors"
24 )
25
26 const (
27 swaggerExample = "example"
28 swaggerExamples = "examples"
29 )
30
31 const (
32 objectType = "object"
33 arrayType = "array"
34 stringType = "string"
35 integerType = "integer"
36 numberType = "number"
37 booleanType = "boolean"
38 nullType = "null"
39 )
40
41 const (
42 jsonProperties = "properties"
43 jsonDefault = "default"
44 )
45
46 const (
47 stringFormatDate = "date"
48 stringFormatDateTime = "date-time"
49 stringFormatPassword = "password"
50 stringFormatByte = "byte"
51 stringFormatCreditCard = "creditcard"
52 stringFormatDuration = "duration"
53 stringFormatEmail = "email"
54 stringFormatHexColor = "hexcolor"
55 stringFormatHostname = "hostname"
56 stringFormatIPv4 = "ipv4"
57 stringFormatIPv6 = "ipv6"
58 stringFormatISBN = "isbn"
59 stringFormatISBN10 = "isbn10"
60 stringFormatISBN13 = "isbn13"
61 stringFormatMAC = "mac"
62 stringFormatRGBColor = "rgbcolor"
63 stringFormatSSN = "ssn"
64 stringFormatURI = "uri"
65 stringFormatUUID = "uuid"
66 stringFormatUUID3 = "uuid3"
67 stringFormatUUID4 = "uuid4"
68 stringFormatUUID5 = "uuid5"
69
70 integerFormatInt32 = "int32"
71 integerFormatInt64 = "int64"
72 integerFormatUInt32 = "uint32"
73 integerFormatUInt64 = "uint64"
74
75 numberFormatFloat32 = "float32"
76 numberFormatFloat64 = "float64"
77 numberFormatFloat = "float"
78 numberFormatDouble = "double"
79 )
80
81
82 var (
83 valueHelp *valueHelper
84 errorHelp *errorHelper
85 )
86
87 type errorHelper struct {
88
89 }
90
91 func (h *errorHelper) sErr(err errors.Error) *Result {
92
93 return &Result{Errors: []error{err}}
94 }
95
96 type valueHelper struct {
97
98 }
99
100 func (h *valueHelper) asInt64(val interface{}) int64 {
101
102
103 v := reflect.ValueOf(val)
104 switch v.Kind() {
105 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
106 return v.Int()
107 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
108 return int64(v.Uint())
109 case reflect.Float32, reflect.Float64:
110 return int64(v.Float())
111 default:
112
113 return 0
114 }
115 }
116
117 func (h *valueHelper) asUint64(val interface{}) uint64 {
118
119
120 v := reflect.ValueOf(val)
121 switch v.Kind() {
122 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
123 return uint64(v.Int())
124 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
125 return v.Uint()
126 case reflect.Float32, reflect.Float64:
127 return uint64(v.Float())
128 default:
129
130 return 0
131 }
132 }
133
134
135 func (h *valueHelper) asFloat64(val interface{}) float64 {
136
137
138 v := reflect.ValueOf(val)
139 switch v.Kind() {
140 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
141 return float64(v.Int())
142 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
143 return float64(v.Uint())
144 case reflect.Float32, reflect.Float64:
145 return v.Float()
146 default:
147
148 return 0
149 }
150 }
151
View as plain text