-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.go
128 lines (107 loc) · 3.05 KB
/
codegen.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
// go build gen/* && ./codegen.exe pack/unpack.go pack/marshaller.go
// go run pack/*
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"reflect"
"strings"
"text/template"
)
type tpl struct {
FieldName string
}
var (
intTpl = template.Must(template.New("intTpl").Parse(`
// {{.FieldName}}
var {{.FieldName}}Raw uint32
binary.Read(r, binary.LittleEndian, &{{.FieldName}}Raw)
in.{{.FieldName}} = int({{.FieldName}}Raw)
`))
strTpl = template.Must(template.New("strTpl").Parse(`
// {{.FieldName}}
var {{.FieldName}}LenRaw uint32
binary.Read(r, binary.LittleEndian, &{{.FieldName}}LenRaw)
{{.FieldName}}Raw := make([]byte, {{.FieldName}}LenRaw)
binary.Read(r, binary.LittleEndian, &{{.FieldName}}Raw)
in.{{.FieldName}} = string({{.FieldName}}Raw)
`))
)
func main() {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, os.Args[1], nil, parser.ParseComments)
if err != nil {
log.Fatal(err)
}
out, _ := os.Create(os.Args[2])
fmt.Fprintln(out, `package `+node.Name.Name)
fmt.Fprintln(out) // empty line
fmt.Fprintln(out, `import "encoding/binary"`)
fmt.Fprintln(out, `import "bytes"`)
fmt.Fprintln(out) // empty line
for _, f := range node.Decls {
g, ok := f.(*ast.GenDecl)
if !ok {
fmt.Printf("SKIP %T is not *ast.GenDecl\n", f)
continue
}
SPECS_LOOP:
for _, spec := range g.Specs {
currType, ok := spec.(*ast.TypeSpec)
if !ok {
fmt.Printf("SKIP %T is not ast.TypeSpec\n", spec)
continue
}
currStruct, ok := currType.Type.(*ast.StructType)
if !ok {
fmt.Printf("SKIP %T is not ast.StructType\n", currStruct)
continue
}
if g.Doc == nil {
fmt.Printf("SKIP struct %#v doesnt have comments\n", currType.Name.Name)
continue
}
needCodegen := false
for _, comment := range g.Doc.List {
needCodegen = needCodegen || strings.HasPrefix(comment.Text, "// cgen: binpack")
}
if !needCodegen {
fmt.Printf("SKIP struct %#v doesnt have cgen mark\n", currType.Name.Name)
continue SPECS_LOOP
}
fmt.Printf("process struct %s\n", currType.Name.Name)
fmt.Printf("\tgenerating Unpack method\n")
fmt.Fprintln(out, "func (in *"+currType.Name.Name+") Unpack(data []byte) error {")
fmt.Fprintln(out, " r := bytes.NewReader(data)")
FIELDS_LOOP:
for _, field := range currStruct.Fields.List {
if field.Tag != nil {
tag := reflect.StructTag(field.Tag.Value[1 : len(field.Tag.Value)-1])
if tag.Get("cgen") == "-" {
continue FIELDS_LOOP
}
}
fieldName := field.Names[0].Name
fileType := field.Type.(*ast.Ident).Name
fmt.Printf("\tgenerating code for field %s.%s\n", currType.Name.Name, fieldName)
switch fileType {
case "int":
intTpl.Execute(out, tpl{fieldName})
case "string":
strTpl.Execute(out, tpl{fieldName})
default:
log.Fatalln("unsupported", fileType)
}
}
fmt.Fprintln(out, " return nil")
fmt.Fprintln(out, "}") // end of Unpack func
fmt.Fprintln(out) // empty line
}
}
}
// go build gen/* && ./codegen.exe pack/unpack.go pack/marshaller.go
// go run pack/*