File: streamcsv.tmpl

package info (click to toggle)
golang-github-kshedden-dstream 0.0~git20190512.c4c4106-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 596 kB
  • sloc: makefile: 30
file content (98 lines) | stat: -rw-r--r-- 1,857 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
package dstream

import (
    "fmt"
	"time"
    "io"
    "os"
    "strconv"
    "math"
)

// Next advances to the next chunk.
func (cs *CSVReader) Next() bool {

	if cs.done {
		return false
	}

	if cs.limitchunk > 0 && cs.limitchunk <= cs.chunknum {
		cs.done = true
		return false
	}

	cs.chunknum++

	truncate(cs.bdata)

	for j := 0; j < cs.chunkSize; j++ {

		// Try to read a row, return false if done.
		var rec []string
		var err error
		if cs.firstrow != nil {
			rec = cs.firstrow
			cs.firstrow = nil
		} else {
			rec, err = cs.csvrdr.Read()
			if err == io.EOF {
				cs.done = true
				return ilen(cs.bdata[0]) > 0
			} else if err != nil {
				if cs.skipErrors {
					os.Stderr.WriteString(fmt.Sprintf("%v\n", err))
					continue
				}
				panic(err)
			}
		}
		cs.nobs++

		for pos, typ := range cs.types {
			fpos := cs.filepos[pos]
			switch typ.Type {
			    {{- range . }}
				    case {{ .Utype }}:
					    {{- if eq .ConvGroup "i" }}
						    x, err := strconv.Atoi(rec[fpos])
						    if err != nil {
							    panic(err)
						    }
						{{- else if eq .ConvGroup "f" }}
						    x, err := strconv.ParseFloat(rec[fpos], 64)
						    if err != nil {
							    x = math.NaN()
						    }
						{{- else if eq .ConvGroup "s" }}
						    x := rec[fpos]
						{{- else }}
						    x := cs.parseTime(rec[fpos])
						{{- end }}
						u := cs.bdata[pos].([]{{ .Type }})
						cs.bdata[pos] = append(u, {{ .Type }}(x))
				{{- end }}
				default:
					panic("unknown type")
			}
		}
    }

	return true
}

func (cs *CSVReader) setbdata() {

	cs.bdata = make([]interface{}, len(cs.names))

	for pos, dtype := range cs.dtypes {
		switch dtype {
            {{- range . }}
                case {{ .Utype }}:
     		        cs.bdata[pos] = make([]{{ .Type }}, 0)
		    {{- end }}
		    default:
		        panic("Unknown type")
		}
	}
}