forked from parsyl/parquet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dremel.go
46 lines (36 loc) · 971 Bytes
/
dremel.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
package dremel
import (
"fmt"
"strings"
"github.com/inigolabs/parquet/cmd/parquetgen/fields"
)
// Package dremel generates code that parquetgen
// uses to encode/decode a struct for writing and
// reading parquet files.
// Write generates the code for initializing a struct
// with data from a parquet file.
func Write(f fields.Field) string {
if f.Repeated() {
return writeRepeated(f)
}
if f.Optional() {
return writeOptional(f)
}
return writeRequired(f)
}
// Read generates the code for reading a struct
// and using the data to write to a parquet file.
func Read(f fields.Field) string {
if f.Repeated() {
return readRepeated(f)
}
if f.Optional() {
return readOptional(f)
}
return readRequired(f)
}
func writeRequired(f fields.Field) string {
return fmt.Sprintf(`func %s(x *%s, vals []%s) {
x.%s = vals[0]
}`, fmt.Sprintf("write%s", strings.Join(f.FieldNames(), "")), f.StructType(), f.TypeName(), strings.Join(f.FieldNames(), "."))
}