This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.go
71 lines (59 loc) · 1.94 KB
/
convert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package rest
import (
"encoding/base64"
"strconv"
)
// ToBool tries to parse the given string to a bool value.
func ToBool(value string) (bool, bool) {
v, err := strconv.ParseBool(value)
return v, err == nil
}
// ToFloat32 tries to parse the given string to a float32 value.
func ToFloat32(value string) (float32, bool) {
v, err := strconv.ParseFloat(value, 32)
return float32(v), err == nil
}
// ToFloat64 tries to parse the given string to a float64 value.
func ToFloat64(value string) (float64, bool) {
v, err := strconv.ParseFloat(value, 64)
return float64(v), err == nil
}
// ToString returns the given argument if string not empty.
func ToString(value string) (string, bool) {
return value, len(value) > 0
}
// ToInt tries to parse the given string to a int value.
func ToInt(value string) (int, bool) {
v, err := strconv.ParseInt(value, 10, 0)
return int(v), err == nil
}
// ToInt32 tries to parse the given string to a int32 value.
func ToInt32(value string) (int32, bool) {
v, err := strconv.ParseInt(value, 10, 32)
return int32(v), err == nil
}
// ToInt64 tries to parse the given string to a int64 value.
func ToInt64(value string) (int64, bool) {
v, err := strconv.ParseInt(value, 10, 64)
return int64(v), err == nil
}
// ToUint tries to parse the given string to a uint value.
func ToUint(value string) (uint, bool) {
v, err := strconv.ParseUint(value, 10, 0)
return uint(v), err == nil
}
// ToUint32 tries to parse the given string to a uint32 value.
func ToUint32(value string) (uint32, bool) {
v, err := strconv.ParseUint(value, 10, 32)
return uint32(v), err == nil
}
// ToUint64 tries to parse the given string to a uint64 value.
func ToUint64(value string) (uint64, bool) {
v, err := strconv.ParseUint(value, 10, 64)
return uint64(v), err == nil
}
// ToBytes tries to parse the given string to a bytes value.
func ToBytes(value string) ([]byte, bool) {
decoded, err := base64.URLEncoding.DecodeString(value)
return decoded, err == nil
}