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