/
main.go
94 lines (80 loc) · 1.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
package main
import (
"bytes"
"go/format"
"os"
"strings"
"text/template"
)
const decodeTmpl = `// Code generated by gen/gen_pointer.go. DO NOT EDIT.
package proto
import "unsafe"
{{range .Decoder}}
var {{.Codec}}PtrCodec = codec{
size: sizeOf{{.Name}}Ptr,
encode: encode{{.Name}}Ptr,
decode: decode{{.Name}}Ptr,
}
func sizeOf{{.Name}}Ptr(p unsafe.Pointer, f *structField) int {
p = deref(p)
if p != nil {
return sizeOf{{.Name}}Required(p, f)
}
return 0
}
func encode{{.Name}}Ptr(b []byte, p unsafe.Pointer, f *structField) []byte {
p = deref(p)
if p != nil {
return encode{{.Name}}Required(b, p, f)
}
return b
}
func decode{{.Name}}Ptr(b []byte, p unsafe.Pointer) (int, error) {
v := (*unsafe.Pointer)(p)
if *v == nil {
*v = unsafe.Pointer(new({{.Type}}))
}
return decode{{.Name}}(b, *v)
}
{{end}}
`
func main() {
type decoder struct {
Type string
Name string
Codec string
}
var decoders = []decoder{
{Type: "bool", Name: "Bool"},
{Type: "string", Name: "String"},
{Type: "int32", Name: "Int32"},
{Type: "uint32", Name: "Uint32"},
{Type: "int64", Name: "Int64"},
{Type: "uint64", Name: "Uint64"},
{Type: "int32", Name: "Zigzag32"},
{Type: "int64", Name: "Zigzag64"},
{Type: "uint32", Name: "Fixed32"},
{Type: "uint64", Name: "Fixed64"},
{Type: "float32", Name: "Float32"},
{Type: "float64", Name: "Float64"},
}
for i, d := range decoders {
decoders[i].Codec = strings.ToLower(d.Name)
}
var out bytes.Buffer
tmpl, err := template.New("").Parse(decodeTmpl)
if err != nil {
panic(err)
}
tmpl.Execute(&out, &struct {
Decoder []decoder
}{
Decoder: decoders,
})
source, err := format.Source(out.Bytes())
if err != nil {
panic(err)
}
f, _ := os.OpenFile("pointer_codec.go", os.O_WRONLY|os.O_CREATE|os.O_SYNC|os.O_TRUNC, 0o644)
f.Write(source)
}