-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathraw_data.go
67 lines (57 loc) · 1.14 KB
/
raw_data.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
package jsnm
import (
"fmt"
"reflect"
"strconv"
)
type RawData struct {
raw interface{}
}
func NewRawData(raw interface{}) RawData {
return RawData{raw: raw}
}
func (d *RawData) Raw() interface{} {
if d == nil {
return nil
}
return d.raw
}
func (d *RawData) String() string {
if d == nil {
return ""
}
return fmt.Sprintf("%v", d.raw)
}
func (d *RawData) Int64() (int64, error) {
return strconv.ParseInt(d.String(), 10, 0)
}
func (d *RawData) MustInt64() int64 {
i64, err := strconv.ParseInt(d.String(), 10, 0)
if err != nil {
return 0
}
return i64
}
func (d *RawData) MustFloat64() float64 {
f, err := d.Float64()
if err != nil {
return 0.0
}
return f
}
func (d *RawData) Float64() (float64, error) {
return strconv.ParseFloat(d.String(), 0)
}
func (d *RawData) Decode() string {
typ := reflect.TypeOf(d.raw)
switch typ.Kind() {
case reflect.String:
return d.String()
case reflect.Float64, reflect.Float32:
return strconv.FormatInt(int64(d.MustFloat64()), 10)
case reflect.Int8, reflect.Int16, reflect.Int, reflect.Int32, reflect.Int64:
return strconv.FormatInt(d.MustInt64(), 10)
default:
return d.String()
}
}