forked from lilith44/gox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
float64s.go
60 lines (50 loc) · 1.15 KB
/
float64s.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 gox
import (
`encoding/json`
`fmt`
`strconv`
`strings`
)
// Float64s 浮点型列表
type Float64s []float64
func (f Float64s) MarshalJSON() (bytes []byte, err error) {
values := make([]string, len(f))
for index, value := range []float64(f) {
values[index] = fmt.Sprintf(`"%f"`, value)
}
bytes = []byte(fmt.Sprintf("[%s]", strings.Join(values, ",")))
return
}
func (f *Float64s) UnmarshalJSON(bytes []byte) (err error) {
var int64Values []float64
if err = json.Unmarshal(bytes, &int64Values); nil == err {
*f = int64Values
return
} else {
var stringValues []string
if err = json.Unmarshal(bytes, &stringValues); nil != err {
return
}
err = f.toFloat64Slice(stringValues)
}
return
}
func (f *Float64s) UnmarshalParam(src string) (err error) {
if "" == src {
return
}
values := strings.Split(src, ",")
err = f.toFloat64Slice(values)
return
}
func (f *Float64s) toFloat64Slice(values []string) (err error) {
realValue := float64(0)
*f = make([]float64, len(values))
for index, value := range values {
if realValue, err = strconv.ParseFloat(value, 64); nil != err {
return
}
(*f)[index] = realValue
}
return
}