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