k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/validation/validate/helpers.go (about) 1 // Copyright 2015 go-swagger maintainers 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package validate 16 17 // TODO: define this as package validate/internal 18 // This must be done while keeping CI intact with all tests and test coverage 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 // Helpers available at the package level 82 var ( 83 valueHelp *valueHelper 84 errorHelp *errorHelper 85 ) 86 87 type errorHelper struct { 88 // A collection of unexported helpers for error construction 89 } 90 91 func (h *errorHelper) sErr(err errors.Error) *Result { 92 // Builds a Result from standard errors.Error 93 return &Result{Errors: []error{err}} 94 } 95 96 type valueHelper struct { 97 // A collection of unexported helpers for value validation 98 } 99 100 func (h *valueHelper) asInt64(val interface{}) int64 { 101 // Number conversion function for int64, without error checking 102 // (implements an implicit type upgrade). 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 //panic("Non numeric value in asInt64()") 113 return 0 114 } 115 } 116 117 func (h *valueHelper) asUint64(val interface{}) uint64 { 118 // Number conversion function for uint64, without error checking 119 // (implements an implicit type upgrade). 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 //panic("Non numeric value in asUint64()") 130 return 0 131 } 132 } 133 134 // Same for unsigned floats 135 func (h *valueHelper) asFloat64(val interface{}) float64 { 136 // Number conversion function for float64, without error checking 137 // (implements an implicit type upgrade). 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 //panic("Non numeric value in asFloat64()") 148 return 0 149 } 150 }