Update calmh/xdr
This commit is contained in:
parent
c6041d2590
commit
15251dfae1
|
@ -23,7 +23,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/calmh/xdr",
|
"ImportPath": "github.com/calmh/xdr",
|
||||||
"Rev": "ec3d404f43731551258977b38dd72cf557d00398"
|
"Rev": "45c46b7db7ff83b8b9ee09bbd95f36ab50043ece"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/juju/ratelimit",
|
"ImportPath": "github.com/juju/ratelimit",
|
||||||
|
|
|
@ -11,6 +11,8 @@ import (
|
||||||
"go/format"
|
"go/format"
|
||||||
"go/parser"
|
"go/parser"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -269,7 +271,7 @@ func handleStruct(t *ast.StructType) []fieldInfo {
|
||||||
return fs
|
return fs
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateCode(s structInfo) {
|
func generateCode(output io.Writer, s structInfo) {
|
||||||
name := s.Name
|
name := s.Name
|
||||||
fs := s.Fields
|
fs := s.Fields
|
||||||
|
|
||||||
|
@ -286,7 +288,7 @@ func generateCode(s structInfo) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Println(string(bs))
|
fmt.Fprintln(output, string(bs))
|
||||||
}
|
}
|
||||||
|
|
||||||
func uncamelize(s string) string {
|
func uncamelize(s string) string {
|
||||||
|
@ -295,16 +297,16 @@ func uncamelize(s string) string {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateDiagram(s structInfo) {
|
func generateDiagram(output io.Writer, s structInfo) {
|
||||||
sn := s.Name
|
sn := s.Name
|
||||||
fs := s.Fields
|
fs := s.Fields
|
||||||
|
|
||||||
fmt.Println(sn + " Structure:")
|
fmt.Fprintln(output, sn+" Structure:")
|
||||||
fmt.Println()
|
fmt.Fprintln(output)
|
||||||
fmt.Println(" 0 1 2 3")
|
fmt.Fprintln(output, " 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")
|
fmt.Fprintln(output, " 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 := "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
|
line := "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
|
||||||
fmt.Println(line)
|
fmt.Fprintln(output, line)
|
||||||
|
|
||||||
for _, f := range fs {
|
for _, f := range fs {
|
||||||
tn := f.FieldType
|
tn := f.FieldType
|
||||||
|
@ -312,52 +314,52 @@ func generateDiagram(s structInfo) {
|
||||||
name := uncamelize(f.Name)
|
name := uncamelize(f.Name)
|
||||||
|
|
||||||
if sl {
|
if sl {
|
||||||
fmt.Printf("| %s |\n", center("Number of "+name, 61))
|
fmt.Fprintf(output, "| %s |\n", center("Number of "+name, 61))
|
||||||
fmt.Println(line)
|
fmt.Fprintln(output, line)
|
||||||
}
|
}
|
||||||
switch tn {
|
switch tn {
|
||||||
case "bool":
|
case "bool":
|
||||||
fmt.Printf("| %s |V|\n", center(name+" (V=0 or 1)", 59))
|
fmt.Fprintf(output, "| %s |V|\n", center(name+" (V=0 or 1)", 59))
|
||||||
fmt.Println(line)
|
fmt.Fprintln(output, line)
|
||||||
case "uint16":
|
case "uint16":
|
||||||
fmt.Printf("| %s | %s |\n", center("0x0000", 29), center(name, 29))
|
fmt.Fprintf(output, "| %s | %s |\n", center("0x0000", 29), center(name, 29))
|
||||||
fmt.Println(line)
|
fmt.Fprintln(output, line)
|
||||||
case "uint32":
|
case "uint32":
|
||||||
fmt.Printf("| %s |\n", center(name, 61))
|
fmt.Fprintf(output, "| %s |\n", center(name, 61))
|
||||||
fmt.Println(line)
|
fmt.Fprintln(output, line)
|
||||||
case "int64", "uint64":
|
case "int64", "uint64":
|
||||||
fmt.Printf("| %-61s |\n", "")
|
fmt.Fprintf(output, "| %-61s |\n", "")
|
||||||
fmt.Printf("+ %s +\n", center(name+" (64 bits)", 61))
|
fmt.Fprintf(output, "+ %s +\n", center(name+" (64 bits)", 61))
|
||||||
fmt.Printf("| %-61s |\n", "")
|
fmt.Fprintf(output, "| %-61s |\n", "")
|
||||||
fmt.Println(line)
|
fmt.Fprintln(output, line)
|
||||||
case "string", "byte": // XXX We assume slice of byte!
|
case "string", "byte": // XXX We assume slice of byte!
|
||||||
fmt.Printf("| %s |\n", center("Length of "+name, 61))
|
fmt.Fprintf(output, "| %s |\n", center("Length of "+name, 61))
|
||||||
fmt.Println(line)
|
fmt.Fprintln(output, line)
|
||||||
fmt.Printf("/ %61s /\n", "")
|
fmt.Fprintf(output, "/ %61s /\n", "")
|
||||||
fmt.Printf("\\ %s \\\n", center(name+" (variable length)", 61))
|
fmt.Fprintf(output, "\\ %s \\\n", center(name+" (variable length)", 61))
|
||||||
fmt.Printf("/ %61s /\n", "")
|
fmt.Fprintf(output, "/ %61s /\n", "")
|
||||||
fmt.Println(line)
|
fmt.Fprintln(output, line)
|
||||||
default:
|
default:
|
||||||
if sl {
|
if sl {
|
||||||
tn = "Zero or more " + tn + " Structures"
|
tn = "Zero or more " + tn + " Structures"
|
||||||
fmt.Printf("/ %s /\n", center("", 61))
|
fmt.Fprintf(output, "/ %s /\n", center("", 61))
|
||||||
fmt.Printf("\\ %s \\\n", center(tn, 61))
|
fmt.Fprintf(output, "\\ %s \\\n", center(tn, 61))
|
||||||
fmt.Printf("/ %s /\n", center("", 61))
|
fmt.Fprintf(output, "/ %s /\n", center("", 61))
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("| %s |\n", center(tn, 61))
|
fmt.Fprintf(output, "| %s |\n", center(tn, 61))
|
||||||
}
|
}
|
||||||
fmt.Println(line)
|
fmt.Fprintln(output, line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Println()
|
fmt.Fprintln(output)
|
||||||
fmt.Println()
|
fmt.Fprintln(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateXdr(s structInfo) {
|
func generateXdr(output io.Writer, s structInfo) {
|
||||||
sn := s.Name
|
sn := s.Name
|
||||||
fs := s.Fields
|
fs := s.Fields
|
||||||
|
|
||||||
fmt.Printf("struct %s {\n", sn)
|
fmt.Fprintf(output, "struct %s {\n", sn)
|
||||||
|
|
||||||
for _, f := range fs {
|
for _, f := range fs {
|
||||||
tn := f.FieldType
|
tn := f.FieldType
|
||||||
|
@ -373,21 +375,21 @@ func generateXdr(s structInfo) {
|
||||||
|
|
||||||
switch tn {
|
switch tn {
|
||||||
case "uint16", "uint32":
|
case "uint16", "uint32":
|
||||||
fmt.Printf("\tunsigned int %s%s;\n", fn, suf)
|
fmt.Fprintf(output, "\tunsigned int %s%s;\n", fn, suf)
|
||||||
case "int64":
|
case "int64":
|
||||||
fmt.Printf("\thyper %s%s;\n", fn, suf)
|
fmt.Fprintf(output, "\thyper %s%s;\n", fn, suf)
|
||||||
case "uint64":
|
case "uint64":
|
||||||
fmt.Printf("\tunsigned hyper %s%s;\n", fn, suf)
|
fmt.Fprintf(output, "\tunsigned hyper %s%s;\n", fn, suf)
|
||||||
case "string":
|
case "string":
|
||||||
fmt.Printf("\tstring %s<%s>;\n", fn, l)
|
fmt.Fprintf(output, "\tstring %s<%s>;\n", fn, l)
|
||||||
case "byte":
|
case "byte":
|
||||||
fmt.Printf("\topaque %s<%s>;\n", fn, l)
|
fmt.Fprintf(output, "\topaque %s<%s>;\n", fn, l)
|
||||||
default:
|
default:
|
||||||
fmt.Printf("\t%s %s%s;\n", tn, fn, suf)
|
fmt.Fprintf(output, "\t%s %s%s;\n", tn, fn, suf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Println("}")
|
fmt.Fprintln(output, "}")
|
||||||
fmt.Println()
|
fmt.Fprintln(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
func center(s string, w int) string {
|
func center(s string, w int) string {
|
||||||
|
@ -418,25 +420,35 @@ func inspector(structs *[]structInfo) func(ast.Node) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
outputFile := flag.String("o", "", "Output file, blank for stdout")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
fname := flag.Arg(0)
|
fname := flag.Arg(0)
|
||||||
|
|
||||||
fset := token.NewFileSet()
|
fset := token.NewFileSet()
|
||||||
f, err := parser.ParseFile(fset, fname, nil, parser.ParseComments)
|
f, err := parser.ParseFile(fset, fname, nil, parser.ParseComments)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var structs []structInfo
|
var structs []structInfo
|
||||||
i := inspector(&structs)
|
i := inspector(&structs)
|
||||||
ast.Inspect(f, i)
|
ast.Inspect(f, i)
|
||||||
|
|
||||||
headerTpl.Execute(os.Stdout, map[string]string{"Package": f.Name.Name})
|
var output io.Writer = os.Stdout
|
||||||
|
if *outputFile != "" {
|
||||||
|
fd, err := os.Create(*outputFile)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
output = fd
|
||||||
|
}
|
||||||
|
|
||||||
|
headerTpl.Execute(output, map[string]string{"Package": f.Name.Name})
|
||||||
for _, s := range structs {
|
for _, s := range structs {
|
||||||
fmt.Printf("\n/*\n\n")
|
fmt.Fprintf(output, "\n/*\n\n")
|
||||||
generateDiagram(s)
|
generateDiagram(output, s)
|
||||||
generateXdr(s)
|
generateXdr(output, s)
|
||||||
fmt.Printf("*/\n")
|
fmt.Fprintf(output, "*/\n")
|
||||||
generateCode(s)
|
generateCode(output, s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue