1 // Copyright 2015 go-swagger maintainers
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
23 // same as ECMA Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER
25 maxJSONFloat = float64(1<<53 - 1) // 9007199254740991.0 2^53 - 1
26 minJSONFloat = -float64(1<<53 - 1) //-9007199254740991.0 -2^53 - 1
27 epsilon float64 = 1e-9
30 // IsFloat64AJSONInteger allow for integers [-2^53, 2^53-1] inclusive
31 func IsFloat64AJSONInteger(f float64) bool {
32 if math.IsNaN(f) || math.IsInf(f, 0) || f < minJSONFloat || f > maxJSONFloat {
36 g := float64(uint64(f))
39 diff := math.Abs(f - g)
41 // more info: https://floating-point-gui.de/errors/comparison/#look-out-for-edge-cases
42 if f == g { // best case
44 } else if f == float64(int64(f)) || f == float64(uint64(f)) { // optimistic case
46 } else if f == 0 || g == 0 || diff < math.SmallestNonzeroFloat64 { // very close to 0 values
47 return diff < (epsilon * math.SmallestNonzeroFloat64)
49 // check the relative error
50 return diff/math.Min(fa+ga, math.MaxFloat64) < epsilon
53 var evaluatesAsTrue map[string]struct{}
56 evaluatesAsTrue = map[string]struct{}{
70 // ConvertBool turn a string into a boolean
71 func ConvertBool(str string) (bool, error) {
72 _, ok := evaluatesAsTrue[strings.ToLower(str)]
76 // ConvertFloat32 turn a string into a float32
77 func ConvertFloat32(str string) (float32, error) {
78 f, err := strconv.ParseFloat(str, 32)
82 return float32(f), nil
85 // ConvertFloat64 turn a string into a float64
86 func ConvertFloat64(str string) (float64, error) {
87 return strconv.ParseFloat(str, 64)
90 // ConvertInt8 turn a string into int8 boolean
91 func ConvertInt8(str string) (int8, error) {
92 i, err := strconv.ParseInt(str, 10, 8)
99 // ConvertInt16 turn a string into a int16
100 func ConvertInt16(str string) (int16, error) {
101 i, err := strconv.ParseInt(str, 10, 16)
108 // ConvertInt32 turn a string into a int32
109 func ConvertInt32(str string) (int32, error) {
110 i, err := strconv.ParseInt(str, 10, 32)
117 // ConvertInt64 turn a string into a int64
118 func ConvertInt64(str string) (int64, error) {
119 return strconv.ParseInt(str, 10, 64)
122 // ConvertUint8 turn a string into a uint8
123 func ConvertUint8(str string) (uint8, error) {
124 i, err := strconv.ParseUint(str, 10, 8)
131 // ConvertUint16 turn a string into a uint16
132 func ConvertUint16(str string) (uint16, error) {
133 i, err := strconv.ParseUint(str, 10, 16)
137 return uint16(i), nil
140 // ConvertUint32 turn a string into a uint32
141 func ConvertUint32(str string) (uint32, error) {
142 i, err := strconv.ParseUint(str, 10, 32)
146 return uint32(i), nil
149 // ConvertUint64 turn a string into a uint64
150 func ConvertUint64(str string) (uint64, error) {
151 return strconv.ParseUint(str, 10, 64)
154 // FormatBool turns a boolean into a string
155 func FormatBool(value bool) string {
156 return strconv.FormatBool(value)
159 // FormatFloat32 turns a float32 into a string
160 func FormatFloat32(value float32) string {
161 return strconv.FormatFloat(float64(value), 'f', -1, 32)
164 // FormatFloat64 turns a float64 into a string
165 func FormatFloat64(value float64) string {
166 return strconv.FormatFloat(value, 'f', -1, 64)
169 // FormatInt8 turns an int8 into a string
170 func FormatInt8(value int8) string {
171 return strconv.FormatInt(int64(value), 10)
174 // FormatInt16 turns an int16 into a string
175 func FormatInt16(value int16) string {
176 return strconv.FormatInt(int64(value), 10)
179 // FormatInt32 turns an int32 into a string
180 func FormatInt32(value int32) string {
181 return strconv.Itoa(int(value))
184 // FormatInt64 turns an int64 into a string
185 func FormatInt64(value int64) string {
186 return strconv.FormatInt(value, 10)
189 // FormatUint8 turns an uint8 into a string
190 func FormatUint8(value uint8) string {
191 return strconv.FormatUint(uint64(value), 10)
194 // FormatUint16 turns an uint16 into a string
195 func FormatUint16(value uint16) string {
196 return strconv.FormatUint(uint64(value), 10)
199 // FormatUint32 turns an uint32 into a string
200 func FormatUint32(value uint32) string {
201 return strconv.FormatUint(uint64(value), 10)
204 // FormatUint64 turns an uint64 into a string
205 func FormatUint64(value uint64) string {
206 return strconv.FormatUint(value, 10)