-
Notifications
You must be signed in to change notification settings - Fork 544
/
main.go
130 lines (121 loc) · 2.8 KB
/
main.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
125
126
127
128
129
130
// Licensed to ClickHouse, Inc. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. ClickHouse, Inc. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package main
import (
"bytes"
_ "embed"
"fmt"
"go/format"
"log"
"os"
"path"
"sort"
"text/template"
)
var (
//go:embed column.tpl
columnSrc string
)
var (
types []_type
)
type _type struct {
Size int
ChType string
GoType string
}
func init() {
for _, size := range []int{8, 16, 32, 64} {
types = append(types, _type{
Size: size,
ChType: fmt.Sprintf("Int%d", size),
GoType: fmt.Sprintf("int%d", size),
}, _type{
Size: size,
ChType: fmt.Sprintf("UInt%d", size),
GoType: fmt.Sprintf("uint%d", size),
})
}
for _, size := range []int{32, 64} {
types = append(types, _type{
Size: size,
ChType: fmt.Sprintf("Float%d", size),
GoType: fmt.Sprintf("float%d", size),
})
}
sort.Slice(types, func(i, j int) bool {
return sequenceKey(types[i].ChType) < sequenceKey(types[j].ChType)
})
}
func write(name string, v any, t *template.Template) error {
out := new(bytes.Buffer)
if err := t.Execute(out, v); err != nil {
return err
}
// fmt.Println(out.String())
data, err := format.Source(out.Bytes())
if err != nil {
return err
}
cwd, err := os.Getwd()
if err != nil {
return err
}
if err := os.WriteFile(path.Join(cwd, fmt.Sprintf("lib/column/%s.go", name)), data, 0o600); err != nil {
return err
}
return nil
}
func main() {
for name, tpl := range map[string]*template.Template{
"column_gen": template.Must(template.New("column").Parse(columnSrc)),
} {
if err := write(name, types, tpl); err != nil {
log.Fatal(err)
}
}
}
const maxByte = 1<<8 - 1
func isDigit(d byte) bool {
return '0' <= d && d <= '9'
}
func sequenceKey(key string) string {
sKey := make([]byte, 0, len(key)+8)
j := -1
for i := 0; i < len(key); i++ {
b := key[i]
if !isDigit(b) {
sKey = append(sKey, b)
j = -1
continue
}
if j == -1 {
sKey = append(sKey, 0x00)
j = len(sKey) - 1
}
if sKey[j] == 1 && sKey[j+1] == '0' {
sKey[j+1] = b
continue
}
if sKey[j]+1 > maxByte {
panic("sequenceKey: invalid key")
}
sKey = append(sKey, b)
sKey[j]++
}
return string(sKey)
}