This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
124 lines (102 loc) · 2.18 KB
/
gen.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// The following directive is necessary to make the package coherent:
// +build ignore
// This program generates carrow_generated.go. It can be invoked by running go
// generate
package main
import (
"log"
"os"
"strings"
"text/template"
"time"
)
func main() {
arrowTypes := []string{"Bool", "Float64", "Integer64", "String", "Timestamp"}
f, err := os.Create("carrow_generated.go")
die(err)
defer f.Close()
packageTemplate.Execute(f, struct {
Timestamp time.Time
ArrowTypes []string
}{
Timestamp: time.Now(),
ArrowTypes: arrowTypes,
})
}
func die(err error) {
if err != nil {
log.Fatal(err)
}
}
func CType(name string) string {
switch name {
case "Bool":
return "C.uint8_t"
case "Float64":
return "C.double"
case "Integer64":
return "C.long"
case "String":
return "*C.char"
case "Timestamp":
return "C.long"
}
panic(name)
}
var funcMap = template.FuncMap{
"ToUpper": strings.ToUpper,
"CType": CType,
}
var packageTemplate = template.Must(template.New("").Funcs(funcMap).Parse(`
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// {{ .Timestamp }}
package carrow
/*
#cgo pkg-config: arrow plasma
#cgo LDFLAGS: -lcarrow
#cgo linux LDFLAGS: -L./bindings/linux-x86_64
#cgo CXXFLAGS: -I/src/arrow/cpp/src
// FIXME: plasma headers
#include "carrow.h"
#include <stdlib.h>
*/
import "C"
// DType is a data type
type DType C.int
// Linux default memory page size is 4k
const bufferSize = 1024 * 4
// Supported data types
var(
{{- range $val := .ArrowTypes}}
{{$val}}Type = DType(C.{{$val | ToUpper }}_DTYPE)
{{- end}}
)
// Array Builders
{{- range $val := .ArrowTypes}}
type {{$val}}ArrayBuilder struct {
builder
buffer [bufferSize]{{$val | CType}}
bufferIdx int
}
// New{{$val}}ArrayBuilder returns a new {{$val}}ArrayBuilder
func New{{$val}}ArrayBuilder() *{{$val}}ArrayBuilder {
r := C.array_builder_new(C.int({{$val}}Type))
if r.err != nil {
return nil
}
bld := &{{$val}}ArrayBuilder{}
bld.builder = builder{r.ptr, bld}
return bld
}
{{- end}}
func (dt DType) String() string {
switch dt {
{{- range $val := .ArrowTypes}}
case {{$val}}Type:
return "{{$val}}"
{{- end}}
}
return "<unknown>"
}
`))