1
0
mirror of https://github.com/uptrace/go-clickhouse.git synced 2025-06-29 00:21:46 +02:00
Files
go-clickhouse/ch/internal/codegen/main.go

118 lines
2.2 KiB
Go
Raw Normal View History

package main
import (
"bytes"
_ "embed"
"flag"
"fmt"
"go/format"
"log"
"os"
"path/filepath"
"text/template"
)
var dirFlag = flag.String("dir", "", "destination directory name")
var (
2023-01-21 12:14:00 +02:00
//go:embed column.tpl
columnSrc string
//go:embed column_safe.tpl
columnSafeSrc string
//go:embed column_unsafe.tpl
columnUnsafeSrc string
)
2023-01-21 12:14:00 +02:00
var columns []ColumnType
type ColumnType struct {
2023-01-21 12:14:00 +02:00
Name string
GoType string
GoReflect string
Size int
IsCustom bool
}
func init() {
for _, size := range []int{8, 16, 32, 64} {
2023-01-21 12:14:00 +02:00
columns = append(columns, ColumnType{
Name: fmt.Sprintf("Int%d", size),
GoType: fmt.Sprintf("int%d", size),
GoReflect: "Int",
Size: size,
}, ColumnType{
2023-01-21 12:14:00 +02:00
Name: fmt.Sprintf("UInt%d", size),
GoType: fmt.Sprintf("uint%d", size),
GoReflect: "Uint",
Size: size,
})
}
for _, size := range []int{32, 64} {
2023-01-21 12:14:00 +02:00
columns = append(columns, ColumnType{
Name: fmt.Sprintf("Float%d", size),
GoType: fmt.Sprintf("float%d", size),
GoReflect: "Float",
Size: size,
})
}
2023-01-21 12:14:00 +02:00
columns = append(columns, ColumnType{
Name: "Bool",
GoType: "bool",
GoReflect: "Bool",
}, ColumnType{
Name: "String",
GoType: "string",
GoReflect: "String",
}, ColumnType{
Name: "Bytes",
GoType: "[]byte",
GoReflect: "Bytes",
}, ColumnType{
Name: "Enum",
GoType: "string",
GoReflect: "String",
IsCustom: true,
}, ColumnType{
Name: "DateTime",
GoType: "time.Time",
})
}
func main() {
flag.Parse()
for _, v := range []struct {
name string
2023-01-21 12:14:00 +02:00
tpl string
}{
2023-01-21 12:14:00 +02:00
{"column_gen", columnSrc},
{"column_safe_gen", columnSafeSrc},
{"column_unsafe_gen", columnUnsafeSrc},
} {
2023-01-21 12:14:00 +02:00
dest := filepath.Join(*dirFlag, v.name+".go")
tpl := template.Must(template.New("").Parse(v.tpl))
if err := gen(dest, tpl, columns); err != nil {
log.Fatalf("%s: %s", v.name, err)
}
}
}
2023-01-21 12:14:00 +02:00
func gen(filePath string, tpl *template.Template, vars any) error {
buf := new(bytes.Buffer)
if err := tpl.Execute(buf, vars); err != nil {
return err
}
data, err := format.Source(buf.Bytes())
if err != nil {
2023-01-21 12:14:00 +02:00
data = buf.Bytes()
}
if err := os.WriteFile(filePath, data, 0o644); err != nil {
return err
}
return nil
}