syncthing/xdr/cmd/genxdr/main.go

432 lines
10 KiB
Go
Raw Normal View History

2014-07-12 22:45:33 +00:00
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
// All rights reserved. Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
2014-06-01 20:50:14 +00:00
2014-02-20 16:40:15 +00:00
package main
import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"os"
"regexp"
"strconv"
"strings"
"text/template"
)
2014-07-13 07:23:10 +00:00
type fieldInfo struct {
2014-02-20 16:40:15 +00:00
Name string
2014-07-13 07:23:10 +00:00
IsBasic bool // handled by one the native Read/WriteUint64 etc functions
IsSlice bool // field is a slice of FieldType
FieldType string // original type of field, i.e. "int"
Encoder string // the encoder name, i.e. "Uint64" for Read/WriteUint64
Convert string // what to convert to when encoding, i.e. "uint64"
Max int // max size for slices and strings
}
type structInfo struct {
Name string
Fields []fieldInfo
2014-02-20 16:40:15 +00:00
}
2014-07-13 07:23:10 +00:00
var headerTpl = template.Must(template.New("header").Parse(`// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
// All rights reserved. Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
// ************************************************************
// This file is automatically generated by genxdr. Do not edit.
// ************************************************************
package {{.Package}}
2014-02-20 16:40:15 +00:00
import (
"bytes"
"io"
"github.com/calmh/syncthing/xdr"
)
`))
var encodeTpl = template.Must(template.New("encoder").Parse(`
func (o {{.TypeName}}) EncodeXDR(w io.Writer) (int, error) {
var xw = xdr.NewWriter(w)
return o.encodeXDR(xw)
}//+n
func (o {{.TypeName}}) MarshalXDR() []byte {
return o.AppendXDR(make([]byte, 0, 128))
}//+n
func (o {{.TypeName}}) AppendXDR(bs []byte) []byte {
var aw = xdr.AppendWriter(bs)
2014-06-30 11:33:52 +00:00
var xw = xdr.NewWriter(&aw)
2014-02-20 16:40:15 +00:00
o.encodeXDR(xw)
2014-06-30 11:33:52 +00:00
return []byte(aw)
2014-02-20 16:40:15 +00:00
}//+n
func (o {{.TypeName}}) encodeXDR(xw *xdr.Writer) (int, error) {
2014-07-13 07:23:10 +00:00
{{range $fieldInfo := .Fields}}
{{if not $fieldInfo.IsSlice}}
{{if ne $fieldInfo.Convert ""}}
xw.Write{{$fieldInfo.Encoder}}({{$fieldInfo.Convert}}(o.{{$fieldInfo.Name}}))
{{else if $fieldInfo.IsBasic}}
{{if ge $fieldInfo.Max 1}}
if len(o.{{$fieldInfo.Name}}) > {{$fieldInfo.Max}} {
return xw.Tot(), xdr.ErrElementSizeExceeded
}
{{end}}
xw.Write{{$fieldInfo.Encoder}}(o.{{$fieldInfo.Name}})
{{else}}
o.{{$fieldInfo.Name}}.encodeXDR(xw)
{{end}}
2014-02-20 16:40:15 +00:00
{{else}}
2014-07-13 07:23:10 +00:00
{{if ge $fieldInfo.Max 1}}
if len(o.{{$fieldInfo.Name}}) > {{$fieldInfo.Max}} {
return xw.Tot(), xdr.ErrElementSizeExceeded
}
{{end}}
xw.WriteUint32(uint32(len(o.{{$fieldInfo.Name}})))
for i := range o.{{$fieldInfo.Name}} {
{{if ne $fieldInfo.Convert ""}}
xw.Write{{$fieldInfo.Encoder}}({{$fieldInfo.Convert}}(o.{{$fieldInfo.Name}}[i]))
{{else if $fieldInfo.IsBasic}}
xw.Write{{$fieldInfo.Encoder}}(o.{{$fieldInfo.Name}}[i])
{{else}}
o.{{$fieldInfo.Name}}[i].encodeXDR(xw)
{{end}}
}
2014-02-20 16:40:15 +00:00
{{end}}
{{end}}
return xw.Tot(), xw.Error()
}//+n
func (o *{{.TypeName}}) DecodeXDR(r io.Reader) error {
xr := xdr.NewReader(r)
return o.decodeXDR(xr)
}//+n
func (o *{{.TypeName}}) UnmarshalXDR(bs []byte) error {
2014-06-30 11:33:52 +00:00
var br = bytes.NewReader(bs)
var xr = xdr.NewReader(br)
2014-02-20 16:40:15 +00:00
return o.decodeXDR(xr)
}//+n
func (o *{{.TypeName}}) decodeXDR(xr *xdr.Reader) error {
2014-07-13 07:23:10 +00:00
{{range $fieldInfo := .Fields}}
{{if not $fieldInfo.IsSlice}}
{{if ne $fieldInfo.Convert ""}}
o.{{$fieldInfo.Name}} = {{$fieldInfo.FieldType}}(xr.Read{{$fieldInfo.Encoder}}())
{{else if $fieldInfo.IsBasic}}
{{if ge $fieldInfo.Max 1}}
o.{{$fieldInfo.Name}} = xr.Read{{$fieldInfo.Encoder}}Max({{$fieldInfo.Max}})
{{else}}
o.{{$fieldInfo.Name}} = xr.Read{{$fieldInfo.Encoder}}()
{{end}}
{{else}}
(&o.{{$fieldInfo.Name}}).decodeXDR(xr)
{{end}}
2014-02-20 16:40:15 +00:00
{{else}}
2014-07-13 07:23:10 +00:00
_{{$fieldInfo.Name}}Size := int(xr.ReadUint32())
{{if ge $fieldInfo.Max 1}}
if _{{$fieldInfo.Name}}Size > {{$fieldInfo.Max}} {
return xdr.ErrElementSizeExceeded
}
{{end}}
o.{{$fieldInfo.Name}} = make([]{{$fieldInfo.FieldType}}, _{{$fieldInfo.Name}}Size)
for i := range o.{{$fieldInfo.Name}} {
{{if ne $fieldInfo.Convert ""}}
o.{{$fieldInfo.Name}}[i] = {{$fieldInfo.FieldType}}(xr.Read{{$fieldInfo.Encoder}}())
{{else if $fieldInfo.IsBasic}}
o.{{$fieldInfo.Name}}[i] = xr.Read{{$fieldInfo.Encoder}}()
{{else}}
(&o.{{$fieldInfo.Name}}[i]).decodeXDR(xr)
{{end}}
}
2014-02-20 16:40:15 +00:00
{{end}}
{{end}}
return xr.Error()
}`))
var maxRe = regexp.MustCompile(`\Wmax:(\d+)`)
type typeSet struct {
Type string
Encoder string
}
var xdrEncoders = map[string]typeSet{
"int16": typeSet{"uint16", "Uint16"},
"uint16": typeSet{"", "Uint16"},
"int32": typeSet{"uint32", "Uint32"},
"uint32": typeSet{"", "Uint32"},
"int64": typeSet{"uint64", "Uint64"},
"uint64": typeSet{"", "Uint64"},
"int": typeSet{"uint64", "Uint64"},
"string": typeSet{"", "String"},
"[]byte": typeSet{"", "Bytes"},
"bool": typeSet{"", "Bool"},
}
2014-07-13 07:23:10 +00:00
func handleStruct(t *ast.StructType) []fieldInfo {
var fs []fieldInfo
2014-02-20 16:40:15 +00:00
for _, sf := range t.Fields.List {
if len(sf.Names) == 0 {
// We don't handle anonymous fields
continue
}
fn := sf.Names[0].Name
var max = 0
if sf.Comment != nil {
c := sf.Comment.List[0].Text
if m := maxRe.FindStringSubmatch(c); m != nil {
max, _ = strconv.Atoi(m[1])
}
2014-07-12 21:06:48 +00:00
if strings.Contains(c, "noencode") {
continue
}
2014-02-20 16:40:15 +00:00
}
2014-07-13 07:23:10 +00:00
var f fieldInfo
2014-02-20 16:40:15 +00:00
switch ft := sf.Type.(type) {
case *ast.Ident:
tn := ft.Name
if enc, ok := xdrEncoders[tn]; ok {
2014-07-13 07:23:10 +00:00
f = fieldInfo{
2014-02-20 16:40:15 +00:00
Name: fn,
IsBasic: true,
FieldType: tn,
Encoder: enc.Encoder,
Convert: enc.Type,
Max: max,
}
} else {
2014-07-13 07:23:10 +00:00
f = fieldInfo{
2014-02-20 16:40:15 +00:00
Name: fn,
IsBasic: false,
FieldType: tn,
Max: max,
}
}
case *ast.ArrayType:
if ft.Len != nil {
// We don't handle arrays
continue
}
tn := ft.Elt.(*ast.Ident).Name
if enc, ok := xdrEncoders["[]"+tn]; ok {
2014-07-13 07:23:10 +00:00
f = fieldInfo{
2014-02-20 16:40:15 +00:00
Name: fn,
IsBasic: true,
FieldType: tn,
Encoder: enc.Encoder,
Convert: enc.Type,
Max: max,
}
} else if enc, ok := xdrEncoders[tn]; ok {
2014-07-13 07:23:10 +00:00
f = fieldInfo{
2014-02-20 16:40:15 +00:00
Name: fn,
IsBasic: true,
IsSlice: true,
FieldType: tn,
Encoder: enc.Encoder,
Convert: enc.Type,
Max: max,
}
} else {
2014-07-13 07:23:10 +00:00
f = fieldInfo{
2014-02-20 16:40:15 +00:00
Name: fn,
IsBasic: false,
IsSlice: true,
FieldType: tn,
Max: max,
}
}
}
fs = append(fs, f)
}
2014-07-13 07:23:10 +00:00
return fs
2014-02-20 16:40:15 +00:00
}
2014-07-13 07:23:10 +00:00
func generateCode(s structInfo) {
name := s.Name
fs := s.Fields
2014-02-20 16:40:15 +00:00
var buf bytes.Buffer
err := encodeTpl.Execute(&buf, map[string]interface{}{"TypeName": name, "Fields": fs})
if err != nil {
panic(err)
}
bs := regexp.MustCompile(`(\s*\n)+`).ReplaceAll(buf.Bytes(), []byte("\n"))
bs = bytes.Replace(bs, []byte("//+n"), []byte("\n"), -1)
bs, err = format.Source(bs)
if err != nil {
panic(err)
}
fmt.Println(string(bs))
}
2014-07-13 07:23:10 +00:00
func uncamelize(s string) string {
return regexp.MustCompile("[a-z][A-Z]").ReplaceAllStringFunc(s, func(camel string) string {
return camel[:1] + " " + camel[1:]
})
}
func generateDiagram(s structInfo) {
sn := s.Name
fs := s.Fields
2014-02-20 16:40:15 +00:00
fmt.Println(sn + " Structure:")
fmt.Println()
fmt.Println(" 0 1 2 3")
fmt.Println(" 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1")
line := "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
fmt.Println(line)
for _, f := range fs {
tn := f.FieldType
sl := f.IsSlice
2014-07-13 07:23:10 +00:00
name := uncamelize(f.Name)
2014-02-20 16:40:15 +00:00
if sl {
2014-07-13 07:23:10 +00:00
fmt.Printf("| %s |\n", center("Number of "+name, 61))
2014-02-20 16:40:15 +00:00
fmt.Println(line)
}
switch tn {
2014-07-13 07:23:10 +00:00
case "bool":
fmt.Printf("| %s |V|\n", center(name+" (V=0 or 1)", 59))
fmt.Println(line)
2014-02-20 16:40:15 +00:00
case "uint16":
2014-07-13 07:23:10 +00:00
fmt.Printf("| %s | %s |\n", center("0x0000", 29), center(name, 29))
2014-02-20 16:40:15 +00:00
fmt.Println(line)
case "uint32":
2014-07-13 07:23:10 +00:00
fmt.Printf("| %s |\n", center(name, 61))
2014-02-20 16:40:15 +00:00
fmt.Println(line)
case "int64", "uint64":
fmt.Printf("| %-61s |\n", "")
2014-07-13 07:23:10 +00:00
fmt.Printf("+ %s +\n", center(name+" (64 bits)", 61))
2014-02-20 16:40:15 +00:00
fmt.Printf("| %-61s |\n", "")
fmt.Println(line)
case "string", "byte": // XXX We assume slice of byte!
2014-07-13 07:23:10 +00:00
fmt.Printf("| %s |\n", center("Length of "+name, 61))
2014-02-20 16:40:15 +00:00
fmt.Println(line)
fmt.Printf("/ %61s /\n", "")
2014-07-13 07:23:10 +00:00
fmt.Printf("\\ %s \\\n", center(name+" (variable length)", 61))
2014-02-20 16:40:15 +00:00
fmt.Printf("/ %61s /\n", "")
fmt.Println(line)
default:
if sl {
tn = "Zero or more " + tn + " Structures"
fmt.Printf("/ %s /\n", center("", 61))
fmt.Printf("\\ %s \\\n", center(tn, 61))
fmt.Printf("/ %s /\n", center("", 61))
} else {
fmt.Printf("| %s |\n", center(tn, 61))
}
fmt.Println(line)
}
}
fmt.Println()
fmt.Println()
}
2014-07-13 07:23:10 +00:00
func generateXdr(s structInfo) {
sn := s.Name
fs := s.Fields
2014-02-20 16:40:15 +00:00
fmt.Printf("struct %s {\n", sn)
for _, f := range fs {
tn := f.FieldType
fn := f.Name
suf := ""
2014-07-13 07:23:10 +00:00
l := ""
if f.Max > 0 {
l = strconv.Itoa(f.Max)
}
2014-02-20 16:40:15 +00:00
if f.IsSlice {
2014-07-13 07:23:10 +00:00
suf = "<" + l + ">"
2014-02-20 16:40:15 +00:00
}
switch tn {
2014-07-13 07:23:10 +00:00
case "uint16", "uint32":
2014-02-20 16:40:15 +00:00
fmt.Printf("\tunsigned int %s%s;\n", fn, suf)
case "int64":
fmt.Printf("\thyper %s%s;\n", fn, suf)
case "uint64":
fmt.Printf("\tunsigned hyper %s%s;\n", fn, suf)
case "string":
2014-07-13 07:23:10 +00:00
fmt.Printf("\tstring %s<%s>;\n", fn, l)
2014-02-20 16:40:15 +00:00
case "byte":
2014-07-13 07:23:10 +00:00
fmt.Printf("\topaque %s<%s>;\n", fn, l)
2014-02-20 16:40:15 +00:00
default:
fmt.Printf("\t%s %s%s;\n", tn, fn, suf)
}
}
fmt.Println("}")
fmt.Println()
}
func center(s string, w int) string {
w -= len(s)
l := w / 2
r := l
if l+r < w {
r++
}
return strings.Repeat(" ", l) + s + strings.Repeat(" ", r)
}
2014-07-13 07:23:10 +00:00
func inspector(structs *[]structInfo) func(ast.Node) bool {
2014-02-20 16:40:15 +00:00
return func(n ast.Node) bool {
switch n := n.(type) {
case *ast.TypeSpec:
switch t := n.Type.(type) {
case *ast.StructType:
name := n.Name.Name
2014-07-13 07:23:10 +00:00
fs := handleStruct(t)
*structs = append(*structs, structInfo{name, fs})
2014-02-20 16:40:15 +00:00
}
return false
default:
return true
}
}
}
func main() {
flag.Parse()
fname := flag.Arg(0)
2014-07-13 07:23:10 +00:00
fset := token.NewFileSet()
2014-02-20 16:40:15 +00:00
f, err := parser.ParseFile(fset, fname, nil, parser.ParseComments)
if err != nil {
panic(err)
}
2014-07-13 07:23:10 +00:00
var structs []structInfo
i := inspector(&structs)
ast.Inspect(f, i)
2014-02-20 16:40:15 +00:00
2014-07-13 07:23:10 +00:00
headerTpl.Execute(os.Stdout, map[string]string{"Package": f.Name.Name})
for _, s := range structs {
fmt.Printf("\n/*\n\n")
generateDiagram(s)
generateXdr(s)
fmt.Printf("*/\n")
generateCode(s)
2014-02-20 16:40:15 +00:00
}
}