2022-04-30 10:30:34 +03:00
|
|
|
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
|
2022-04-30 10:30:34 +03:00
|
|
|
//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
|
2022-04-30 10:30:34 +03:00
|
|
|
|
|
|
|
type ColumnType struct {
|
2023-01-21 12:14:00 +02:00
|
|
|
Name string
|
|
|
|
GoType string
|
|
|
|
GoReflect string
|
|
|
|
Size int
|
|
|
|
IsCustom bool
|
2022-04-30 10:30:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2022-04-30 10:30:34 +03:00
|
|
|
}, 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,
|
2022-04-30 10:30:34 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
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,
|
2022-04-30 10:30:34 +03:00
|
|
|
})
|
|
|
|
}
|
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",
|
|
|
|
})
|
2022-04-30 10:30:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
for _, v := range []struct {
|
|
|
|
name string
|
2023-01-21 12:14:00 +02:00
|
|
|
tpl string
|
2022-04-30 10:30:34 +03:00
|
|
|
}{
|
2023-01-21 12:14:00 +02:00
|
|
|
{"column_gen", columnSrc},
|
|
|
|
{"column_safe_gen", columnSafeSrc},
|
|
|
|
{"column_unsafe_gen", columnUnsafeSrc},
|
2022-04-30 10:30:34 +03:00
|
|
|
} {
|
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)
|
2022-04-30 10:30:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-21 12:14:00 +02:00
|
|
|
func gen(filePath string, tpl *template.Template, vars any) error {
|
2022-04-30 10:30:34 +03:00
|
|
|
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()
|
2022-04-30 10:30:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.WriteFile(filePath, data, 0o644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|