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