-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconverters.go
59 lines (49 loc) · 1.29 KB
/
converters.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
package forms
import (
"errors"
"strconv"
)
var (
//Some built in converters that return the type they suggest with any errors.
IntConverter ConverterFunc = int_converter
Float64Converter ConverterFunc = float64_converter
Float32Converter ConverterFunc = float32_converter
)
func make_human_readable(numerr *strconv.NumError) (err error) {
switch numerr.Err {
case strconv.ErrRange:
err = errors.New("That number is out of range")
case strconv.ErrSyntax:
err = errors.New("That is not a number")
}
return
}
func int_converter(in string) (out interface{}, err error) {
//parse the input
i, err := strconv.ParseInt(in, 10, 0)
//attempt to make the errors more human readable
if numerr, ok := err.(*strconv.NumError); ok && err != nil {
err = make_human_readable(numerr)
return
}
//set our output
out = int(i)
return
}
func float64_converter(in string) (out interface{}, err error) {
out, err = strconv.ParseFloat(in, 64)
if numerr, ok := err.(*strconv.NumError); ok && err != nil {
err = make_human_readable(numerr)
return
}
return
}
func float32_converter(in string) (out interface{}, err error) {
f, err := strconv.ParseFloat(in, 32)
if numerr, ok := err.(*strconv.NumError); ok && err != nil {
err = make_human_readable(numerr)
return
}
out = float32(f)
return
}