-
Notifications
You must be signed in to change notification settings - Fork 2
/
number.go
60 lines (52 loc) · 926 Bytes
/
number.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
package jsonast
import (
"errors"
)
var errNotANumber = errors.New("not a number")
// Number is the numeric JSON value
type Number interface {
Value
Float64() float64
}
type number struct {
Value
i float64
}
func newNumber(f float64) Number {
return &number{
Value: valueImpl{isNumber: true},
i: f,
}
}
func (n *number) Float64() float64 {
return n.i
}
// TODO: remove?
func convertNumber(i interface{}) (float64, error) {
switch t := i.(type) {
case int:
return float64(t), nil
case int8:
return float64(t), nil
case int16:
return float64(t), nil
case int32:
return float64(t), nil
case int64:
return float64(t), nil
case uint8:
return float64(t), nil
case uint16:
return float64(t), nil
case uint32:
return float64(t), nil
case uint64:
return float64(t), nil
case float32:
return float64(t), nil
case float64:
return t, nil
default:
return -1, errNotANumber
}
}