forked from parsyl/parquet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.go
61 lines (52 loc) · 1.36 KB
/
read.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
package dremel
import (
"fmt"
"strings"
"github.com/inigolabs/parquet/cmd/parquetgen/fields"
)
func readRequired(f fields.Field) string {
return fmt.Sprintf(`func read%s(x %s) %s {
return x.%s
}`, strings.Join(f.FieldNames(), ""), f.StructType(), f.TypeName(), strings.Join(f.FieldNames(), "."))
}
func readOptional(f fields.Field) string {
var out string
n := f.MaxDef()
for def := 0; def < n; def++ {
out += fmt.Sprintf(`case x.%s == nil:
defs = append(defs, %d)
return vals, defs, reps
`, nilField(def, f), def)
}
var ptr string
rts := f.RepetitionTypes()
if rts[len(rts)-1] == fields.Optional {
ptr = "*"
}
out += fmt.Sprintf(` default:
vals = append(vals, %sx.%s)
defs = append(defs, %d)
return vals, defs, reps`, ptr, nilField(n, f), n)
return fmt.Sprintf(`func read%s(x %s, vals []%s, defs, reps []uint8) ([]%s, []uint8, []uint8) {
switch {
%s
}
}`, strings.Join(f.FieldNames(), ""), f.StructType(), cleanTypeName(f.Type), cleanTypeName(f.Type), out)
}
func cleanTypeName(s string) string {
return strings.Replace(strings.Replace(s, "*", "", 1), "[]", "", 1)
}
func nilField(i int, f fields.Field) string {
var flds []string
var count int
for j, o := range f.RepetitionTypes() {
flds = append(flds, f.FieldNames()[j])
if o == fields.Optional {
count++
}
if count > i {
break
}
}
return strings.Join(flds, ".")
}