Debian Codegraph @

dstream/gen/convert.tmpl

raw
1 package dstream
2
3 import (
4 "fmt"
5 )
6
7 func (c *convert) Next() bool {
8
9 if !c.source.Next() {
10 return false
11 }
12
13 // The non-converted variables are just pointer assignments.
14 for j := 0; j < c.source.NumVar(); j++ {
15 if j != c.vpos {
16 c.bdata[j] = c.source.GetPos(j)
17 }
18 }
19
20 // Initialize the backing array if needed
21 to := c.bdata[c.vpos]
22 if to == nil {
23 switch c.dtype {
24 {{- range . }}
25 case {{ .Utype }}:
26 to = make([]{{ .Type }}, 0, 100)
27 {{- end }}
28 default:
29 msg := fmt.Sprintf("Convert: unknown type %v\n", c.dtype)
30 panic(msg)
31 }
32 }
33
34 // Need this to do nested switches.
35 {{ $types := . }}
36
37 // Unconverted data
38 from := c.source.GetPos(c.vpos)
39
40 switch to := to.(type) {
41 {{- range $Type1 := $types }}
42 case []{{ $Type1.Type }}:
43 switch from := from.(type) {
44 {{- range $Type2 := $types }}
45 case []{{ $Type2.Type }}:
46 {{- if eq $Type1.Type $Type2.Type }}
47 // Same types, nothing to do
48 c.bdata[c.vpos] = from
49 {{- else }}
50 to = to[0:0]
51 for _, x := range from {
52 to = append(to, {{ $Type1.Type }}(x))
53 }
54 c.bdata[c.vpos] = to
55 {{- end }}
56 {{- end }}
57 default:
58 msg := fmt.Sprintf("Convert: unknown origin type %T\n", from)
59 panic(msg)
60 }
61 {{- end }}
62 default:
63 msg := fmt.Sprintf("Convert: unkown destination type %T\n", to)
64 panic(msg)
65 }
66
67 return true
68 }