blob: 4e446ff703f2d7c78d994c2d8ae701f08d9b34b6 [file] [log] [blame]
Serge Bazanskicc25bdf2018-10-25 14:02:58 +02001// 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
15package swag
16
17import (
18 "math"
19 "strconv"
20 "strings"
21)
22
23// same as ECMA Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER
24const (
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
28)
29
30// IsFloat64AJSONInteger allow for integers [-2^53, 2^53-1] inclusive
31func IsFloat64AJSONInteger(f float64) bool {
32 if math.IsNaN(f) || math.IsInf(f, 0) || f < minJSONFloat || f > maxJSONFloat {
33 return false
34 }
35 fa := math.Abs(f)
36 g := float64(uint64(f))
37 ga := math.Abs(g)
38
39 diff := math.Abs(f - g)
40
41 // more info: https://floating-point-gui.de/errors/comparison/#look-out-for-edge-cases
42 if f == g { // best case
43 return true
44 } else if f == float64(int64(f)) || f == float64(uint64(f)) { // optimistic case
45 return true
46 } else if f == 0 || g == 0 || diff < math.SmallestNonzeroFloat64 { // very close to 0 values
47 return diff < (epsilon * math.SmallestNonzeroFloat64)
48 }
49 // check the relative error
50 return diff/math.Min(fa+ga, math.MaxFloat64) < epsilon
51}
52
53var evaluatesAsTrue map[string]struct{}
54
55func init() {
56 evaluatesAsTrue = map[string]struct{}{
57 "true": {},
58 "1": {},
59 "yes": {},
60 "ok": {},
61 "y": {},
62 "on": {},
63 "selected": {},
64 "checked": {},
65 "t": {},
66 "enabled": {},
67 }
68}
69
70// ConvertBool turn a string into a boolean
71func ConvertBool(str string) (bool, error) {
72 _, ok := evaluatesAsTrue[strings.ToLower(str)]
73 return ok, nil
74}
75
76// ConvertFloat32 turn a string into a float32
77func ConvertFloat32(str string) (float32, error) {
78 f, err := strconv.ParseFloat(str, 32)
79 if err != nil {
80 return 0, err
81 }
82 return float32(f), nil
83}
84
85// ConvertFloat64 turn a string into a float64
86func ConvertFloat64(str string) (float64, error) {
87 return strconv.ParseFloat(str, 64)
88}
89
90// ConvertInt8 turn a string into int8 boolean
91func ConvertInt8(str string) (int8, error) {
92 i, err := strconv.ParseInt(str, 10, 8)
93 if err != nil {
94 return 0, err
95 }
96 return int8(i), nil
97}
98
99// ConvertInt16 turn a string into a int16
100func ConvertInt16(str string) (int16, error) {
101 i, err := strconv.ParseInt(str, 10, 16)
102 if err != nil {
103 return 0, err
104 }
105 return int16(i), nil
106}
107
108// ConvertInt32 turn a string into a int32
109func ConvertInt32(str string) (int32, error) {
110 i, err := strconv.ParseInt(str, 10, 32)
111 if err != nil {
112 return 0, err
113 }
114 return int32(i), nil
115}
116
117// ConvertInt64 turn a string into a int64
118func ConvertInt64(str string) (int64, error) {
119 return strconv.ParseInt(str, 10, 64)
120}
121
122// ConvertUint8 turn a string into a uint8
123func ConvertUint8(str string) (uint8, error) {
124 i, err := strconv.ParseUint(str, 10, 8)
125 if err != nil {
126 return 0, err
127 }
128 return uint8(i), nil
129}
130
131// ConvertUint16 turn a string into a uint16
132func ConvertUint16(str string) (uint16, error) {
133 i, err := strconv.ParseUint(str, 10, 16)
134 if err != nil {
135 return 0, err
136 }
137 return uint16(i), nil
138}
139
140// ConvertUint32 turn a string into a uint32
141func ConvertUint32(str string) (uint32, error) {
142 i, err := strconv.ParseUint(str, 10, 32)
143 if err != nil {
144 return 0, err
145 }
146 return uint32(i), nil
147}
148
149// ConvertUint64 turn a string into a uint64
150func ConvertUint64(str string) (uint64, error) {
151 return strconv.ParseUint(str, 10, 64)
152}
153
154// FormatBool turns a boolean into a string
155func FormatBool(value bool) string {
156 return strconv.FormatBool(value)
157}
158
159// FormatFloat32 turns a float32 into a string
160func FormatFloat32(value float32) string {
161 return strconv.FormatFloat(float64(value), 'f', -1, 32)
162}
163
164// FormatFloat64 turns a float64 into a string
165func FormatFloat64(value float64) string {
166 return strconv.FormatFloat(value, 'f', -1, 64)
167}
168
169// FormatInt8 turns an int8 into a string
170func FormatInt8(value int8) string {
171 return strconv.FormatInt(int64(value), 10)
172}
173
174// FormatInt16 turns an int16 into a string
175func FormatInt16(value int16) string {
176 return strconv.FormatInt(int64(value), 10)
177}
178
179// FormatInt32 turns an int32 into a string
180func FormatInt32(value int32) string {
181 return strconv.Itoa(int(value))
182}
183
184// FormatInt64 turns an int64 into a string
185func FormatInt64(value int64) string {
186 return strconv.FormatInt(value, 10)
187}
188
189// FormatUint8 turns an uint8 into a string
190func FormatUint8(value uint8) string {
191 return strconv.FormatUint(uint64(value), 10)
192}
193
194// FormatUint16 turns an uint16 into a string
195func FormatUint16(value uint16) string {
196 return strconv.FormatUint(uint64(value), 10)
197}
198
199// FormatUint32 turns an uint32 into a string
200func FormatUint32(value uint32) string {
201 return strconv.FormatUint(uint64(value), 10)
202}
203
204// FormatUint64 turns an uint64 into a string
205func FormatUint64(value uint64) string {
206 return strconv.FormatUint(value, 10)
207}