File: load.go

package info (click to toggle)
golang-github-montanaflynn-stats 0.2.0%2Bgit20170729.66.4a16327-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 252 kB
  • ctags: 220
  • sloc: makefile: 23
file content (101 lines) | stat: -rw-r--r-- 1,709 bytes parent folder | download
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package stats

import (
	"strconv"
	"time"
)

// LoadRawData parses and converts a slice of mixed data types to floats
func LoadRawData(raw interface{}) (f Float64Data) {
	var r []interface{}
	var s Float64Data

	switch t := raw.(type) {
	case []interface{}:
		r = t
	case []uint:
		for _, v := range t {
			s = append(s, float64(v))
		}
		return s

	case []bool:
		for _, v := range t {
			if v == true {
				s = append(s, 1.0)
			} else {
				s = append(s, 0.0)
			}
		}
		return s
	case []float64:
		return Float64Data(t)
	case []int:
		for _, v := range t {
			s = append(s, float64(v))
		}
		return s
	case []string:
		for _, v := range t {
			r = append(r, v)
		}
	case []time.Duration:
		for _, v := range t {
			r = append(r, v)
		}
	case map[int]int:
		for i := 0; i < len(t); i++ {
			s = append(s, float64(t[i]))
		}
		return s
	case map[int]string:
		for i := 0; i < len(t); i++ {
			r = append(r, t[i])
		}
	case map[int]uint:
		for i := 0; i < len(t); i++ {
			s = append(s, float64(t[i]))
		}
		return s
	case map[int]bool:
		for i := 0; i < len(t); i++ {
			if t[i] == true {
				s = append(s, 1.0)
			} else {
				s = append(s, 0.0)
			}
		}
		return s
	case map[int]float64:
		for i := 0; i < len(t); i++ {
			s = append(s, t[i])
		}
		return s
	}

	for _, v := range r {
		switch t := v.(type) {
		case int:
			a := float64(t)
			f = append(f, a)
		case uint:
			f = append(f, float64(t))
		case float64:
			f = append(f, t)
		case string:
			fl, err := strconv.ParseFloat(t, 64)
			if err == nil {
				f = append(f, fl)
			}
		case bool:
			if t == true {
				f = append(f, 1.0)
			} else {
				f = append(f, 0.0)
			}
		case time.Duration:
			f = append(f, float64(t))
		}
	}
	return f
}