github.com/go-swagger/go-swagger@v0.31.0/generator/formats.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 generator 16 17 // TODO: we may probably find a way to register most of this dynamically from strfmt 18 19 // map of function calls to be generated to get the zero value of a given type 20 var zeroes = map[string]string{ 21 "bool": "false", 22 "float32": "0", 23 "float64": "0", 24 "int": "0", 25 "int8": "0", 26 "int16": "0", 27 "int32": "0", 28 "int64": "0", 29 "string": "\"\"", 30 "uint": "0", 31 "uint8": "0", 32 "uint16": "0", 33 "uint32": "0", 34 "uint64": "0", 35 // Extended formats (23 formats corresponding to the Default registry 36 // provided by go-openapi/strfmt) 37 "strfmt.Base64": "strfmt.Base64([]byte(nil))", 38 "strfmt.CreditCard": "strfmt.CreditCard(\"\")", 39 "strfmt.Date": "strfmt.Date{}", 40 "strfmt.DateTime": "strfmt.DateTime{}", 41 "strfmt.Duration": "strfmt.Duration(0)", 42 "strfmt.Email": "strfmt.Email(\"\")", 43 "strfmt.HexColor": "strfmt.HexColor(\"#000000\")", 44 "strfmt.Hostname": "strfmt.Hostname(\"\")", 45 "strfmt.IPv4": "strfmt.IPv4(\"\")", 46 "strfmt.IPv6": "strfmt.IPv6(\"\")", 47 "strfmt.ISBN": "strfmt.ISBN(\"\")", 48 "strfmt.ISBN10": "strfmt.ISBN10(\"\")", 49 "strfmt.ISBN13": "strfmt.ISBN13(\"\")", 50 "strfmt.MAC": "strfmt.MAC(\"\")", 51 "strfmt.ObjectId": "strfmt.ObjectId{}", 52 "strfmt.Password": "strfmt.Password(\"\")", 53 "strfmt.RGBColor": "strfmt.RGBColor(\"rgb(0,0,0)\")", 54 "strfmt.SSN": "strfmt.SSN(\"\")", 55 "strfmt.URI": "strfmt.URI(\"\")", 56 "strfmt.UUID": "strfmt.UUID(\"\")", 57 "strfmt.UUID3": "strfmt.UUID3(\"\")", 58 "strfmt.UUID4": "strfmt.UUID4(\"\")", 59 "strfmt.UUID5": "strfmt.UUID5(\"\")", 60 "strfmt.ULID": "strfmt.ULID(\"\")", 61 // "file": "runtime.File", 62 } 63 64 // conversion functions from string representation to a numerical or boolean 65 // primitive type 66 var stringConverters = map[string]string{ 67 "bool": "swag.ConvertBool", 68 "float32": "swag.ConvertFloat32", 69 "float64": "swag.ConvertFloat64", 70 "int8": "swag.ConvertInt8", 71 "int16": "swag.ConvertInt16", 72 "int32": "swag.ConvertInt32", 73 "int64": "swag.ConvertInt64", 74 "uint8": "swag.ConvertUint8", 75 "uint16": "swag.ConvertUint16", 76 "uint32": "swag.ConvertUint32", 77 "uint64": "swag.ConvertUint64", 78 } 79 80 // formatting (string representation) functions from a native representation 81 // of a numerical or boolean primitive type 82 var stringFormatters = map[string]string{ 83 "bool": "swag.FormatBool", 84 "float32": "swag.FormatFloat32", 85 "float64": "swag.FormatFloat64", 86 "int8": "swag.FormatInt8", 87 "int16": "swag.FormatInt16", 88 "int32": "swag.FormatInt32", 89 "int64": "swag.FormatInt64", 90 "uint8": "swag.FormatUint8", 91 "uint16": "swag.FormatUint16", 92 "uint32": "swag.FormatUint32", 93 "uint64": "swag.FormatUint64", 94 } 95 96 // typeMapping contains a mapping of type name to go type 97 var typeMapping = map[string]string{ 98 // Standard formats with native, straightforward, mapping 99 "string": "string", 100 "boolean": "bool", 101 "integer": "int64", 102 "number": "float64", 103 // For file producers 104 "file": "runtime.File", 105 } 106 107 // formatMapping contains a type-specific version of mapping of format to go type 108 var formatMapping = map[string]map[string]string{ 109 "number": { 110 "double": "float64", 111 "float": "float32", 112 "int": "int64", 113 "int8": "int8", 114 "int16": "int16", 115 "int32": "int32", 116 "int64": "int64", 117 "uint": "uint64", 118 "uint8": "uint8", 119 "uint16": "uint16", 120 "uint32": "uint32", 121 "uint64": "uint64", 122 }, 123 "integer": { 124 "int": "int64", 125 "int8": "int8", 126 "int16": "int16", 127 "int32": "int32", 128 "int64": "int64", 129 "uint": "uint64", 130 "uint8": "uint8", 131 "uint16": "uint16", 132 "uint32": "uint32", 133 "uint64": "uint64", 134 }, 135 "string": { 136 "char": "rune", 137 // Extended format registry from go-openapi/strfmt. 138 // Currently, 23 such formats are supported (default strftm registry), 139 // plus the following aliases: 140 // - "datetime" alias for the more official "date-time" 141 // - "objectid" and "ObjectId" aliases for "bsonobjectid" 142 "binary": "io.ReadCloser", 143 "byte": "strfmt.Base64", 144 "creditcard": "strfmt.CreditCard", 145 "date": "strfmt.Date", 146 "date-time": "strfmt.DateTime", 147 "datetime": "strfmt.DateTime", 148 "duration": "strfmt.Duration", 149 "email": "strfmt.Email", 150 "hexcolor": "strfmt.HexColor", 151 "hostname": "strfmt.Hostname", 152 "ipv4": "strfmt.IPv4", 153 "ipv6": "strfmt.IPv6", 154 "isbn": "strfmt.ISBN", 155 "isbn10": "strfmt.ISBN10", 156 "isbn13": "strfmt.ISBN13", 157 "mac": "strfmt.MAC", 158 "bsonobjectid": "strfmt.ObjectId", 159 "objectid": "strfmt.ObjectId", 160 "ObjectId": "strfmt.ObjectId", // NOTE: does it work with uppercase? 161 "password": "strfmt.Password", 162 "rgbcolor": "strfmt.RGBColor", 163 "ssn": "strfmt.SSN", 164 "uri": "strfmt.URI", 165 "uuid": "strfmt.UUID", 166 "uuid3": "strfmt.UUID3", 167 "uuid4": "strfmt.UUID4", 168 "uuid5": "strfmt.UUID5", 169 "ulid": "strfmt.ULID", 170 // For file producers 171 "file": "runtime.File", 172 }, 173 } 174 175 // go primitive types 176 var primitives = map[string]struct{}{ 177 "bool": {}, 178 "byte": {}, 179 "[]byte": {}, 180 "complex64": {}, 181 "complex128": {}, 182 "float32": {}, 183 "float64": {}, 184 "int": {}, 185 "int8": {}, 186 "int16": {}, 187 "int32": {}, 188 "int64": {}, 189 "rune": {}, 190 "string": {}, 191 "uint": {}, 192 "uint8": {}, 193 "uint16": {}, 194 "uint32": {}, 195 "uint64": {}, 196 } 197 198 // Formats with a custom formatter. 199 // Currently, 23 such formats are supported 200 var customFormatters = map[string]struct{}{ 201 "strfmt.Base64": {}, 202 "strfmt.CreditCard": {}, 203 "strfmt.Date": {}, 204 "strfmt.DateTime": {}, 205 "strfmt.Duration": {}, 206 "strfmt.Email": {}, 207 "strfmt.HexColor": {}, 208 "strfmt.Hostname": {}, 209 "strfmt.IPv4": {}, 210 "strfmt.IPv6": {}, 211 "strfmt.ISBN": {}, 212 "strfmt.ISBN10": {}, 213 "strfmt.ISBN13": {}, 214 "strfmt.MAC": {}, 215 "strfmt.ObjectId": {}, 216 "strfmt.Password": {}, 217 "strfmt.RGBColor": {}, 218 "strfmt.SSN": {}, 219 "strfmt.URI": {}, 220 "strfmt.UUID": {}, 221 "strfmt.UUID3": {}, 222 "strfmt.UUID4": {}, 223 "strfmt.UUID5": {}, 224 // the following interfaces do not generate validations 225 "io.ReadCloser": {}, // for "format": "binary" (server side) 226 "io.Writer": {}, // for "format": "binary" (client side) 227 // NOTE: runtime.File is not a customFormatter 228 }