|
@@ -36,31 +36,73 @@ type structInfo struct {
|
|
|
Fields []fieldInfo
|
|
|
}
|
|
|
|
|
|
-var headerTpl = template.Must(template.New("header").Parse(`// ************************************************************
|
|
|
+func (i structInfo) SizeExpr() string {
|
|
|
+ var xdrSizes = map[string]int{
|
|
|
+ "int8": 4,
|
|
|
+ "uint8": 4,
|
|
|
+ "int16": 4,
|
|
|
+ "uint16": 4,
|
|
|
+ "int32": 4,
|
|
|
+ "uint32": 4,
|
|
|
+ "int64": 8,
|
|
|
+ "uint64": 8,
|
|
|
+ "int": 8,
|
|
|
+ "bool": 4,
|
|
|
+ }
|
|
|
+
|
|
|
+ var terms []string
|
|
|
+ nl := ""
|
|
|
+ for _, f := range i.Fields {
|
|
|
+ if size := xdrSizes[f.FieldType]; size > 0 {
|
|
|
+ if f.IsSlice {
|
|
|
+ terms = append(terms, nl+"4+len(o."+f.Name+")*"+strconv.Itoa(size))
|
|
|
+ } else {
|
|
|
+ terms = append(terms, strconv.Itoa(size))
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ switch f.FieldType {
|
|
|
+ case "string", "[]byte":
|
|
|
+ if f.IsSlice {
|
|
|
+ terms = append(terms, nl+"4+xdr.SizeOfSlice(o."+f.Name+")")
|
|
|
+ } else {
|
|
|
+ terms = append(terms, nl+"4+len(o."+f.Name+")+xdr.Padding(len(o."+f.Name+"))")
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ if f.IsSlice {
|
|
|
+ terms = append(terms, nl+"4+xdr.SizeOfSlice(o."+f.Name+")")
|
|
|
+ } else {
|
|
|
+ terms = append(terms, nl+"o."+f.Name+".XDRSize()")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ nl = "\n"
|
|
|
+ }
|
|
|
+ return strings.Join(terms, "+")
|
|
|
+}
|
|
|
+
|
|
|
+var headerData = `// ************************************************************
|
|
|
// This file is automatically generated by genxdr. Do not edit.
|
|
|
// ************************************************************
|
|
|
|
|
|
package {{.Package}}
|
|
|
|
|
|
import (
|
|
|
- "bytes"
|
|
|
- "io"
|
|
|
-
|
|
|
"github.com/calmh/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.EncodeXDRInto(xw)
|
|
|
+var encoderData = `
|
|
|
+func (o {{.Name}}) XDRSize() int {
|
|
|
+ return {{.SizeExpr}}
|
|
|
}//+n
|
|
|
|
|
|
-func (o {{.TypeName}}) MarshalXDR() ([]byte, error) {
|
|
|
- return o.AppendXDR(make([]byte, 0, 128))
|
|
|
+func (o {{.Name}}) MarshalXDR() ([]byte, error) {
|
|
|
+ buf:= make([]byte, o.XDRSize())
|
|
|
+ m := &xdr.Marshaller{Data: buf}
|
|
|
+ return buf, o.MarshalXDRInto(m)
|
|
|
}//+n
|
|
|
|
|
|
-func (o {{.TypeName}}) MustMarshalXDR() []byte {
|
|
|
+func (o {{.Name}}) MustMarshalXDR() []byte {
|
|
|
bs, err := o.MarshalXDR()
|
|
|
if err != nil {
|
|
|
panic(err)
|
|
@@ -68,141 +110,155 @@ func (o {{.TypeName}}) MustMarshalXDR() []byte {
|
|
|
return bs
|
|
|
}//+n
|
|
|
|
|
|
-func (o {{.TypeName}}) AppendXDR(bs []byte) ([]byte, error) {
|
|
|
- var aw = xdr.AppendWriter(bs)
|
|
|
- var xw = xdr.NewWriter(&aw)
|
|
|
- _, err := o.EncodeXDRInto(xw)
|
|
|
- return []byte(aw), err
|
|
|
+func (o {{.Name}}) MarshalXDRInto(m *xdr.Marshaller) error {
|
|
|
+ {{range $fi := .Fields}}
|
|
|
+ {{if $fi.IsSlice}}
|
|
|
+ {{template "marshalSlice" $fi}}
|
|
|
+ {{else}}
|
|
|
+ {{template "marshalValue" $fi}}
|
|
|
+ {{end}}
|
|
|
+ {{end}}
|
|
|
+ return m.Error
|
|
|
}//+n
|
|
|
|
|
|
-func (o {{.TypeName}}) EncodeXDRInto(xw *xdr.Writer) (int, error) {
|
|
|
- {{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 l := len(o.{{$fieldInfo.Name}}); l > {{$fieldInfo.Max}} {
|
|
|
- return xw.Tot(), xdr.ElementSizeExceeded("{{$fieldInfo.Name}}", l, {{$fieldInfo.Max}})
|
|
|
- }
|
|
|
- {{end}}
|
|
|
- xw.Write{{$fieldInfo.Encoder}}(o.{{$fieldInfo.Name}})
|
|
|
- {{else}}
|
|
|
- _, err := o.{{$fieldInfo.Name}}.EncodeXDRInto(xw)
|
|
|
- if err != nil {
|
|
|
- return xw.Tot(), err
|
|
|
- }
|
|
|
- {{end}}
|
|
|
- {{else}}
|
|
|
- {{if ge $fieldInfo.Max 1}}
|
|
|
- if l := len(o.{{$fieldInfo.Name}}); l > {{$fieldInfo.Max}} {
|
|
|
- return xw.Tot(), xdr.ElementSizeExceeded("{{$fieldInfo.Name}}", l, {{$fieldInfo.Max}})
|
|
|
- }
|
|
|
- {{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}}
|
|
|
- _, err := o.{{$fieldInfo.Name}}[i].EncodeXDRInto(xw)
|
|
|
- if err != nil {
|
|
|
- return xw.Tot(), err
|
|
|
- }
|
|
|
- {{end}}
|
|
|
+{{define "marshalValue"}}
|
|
|
+ {{if ne .Convert ""}}
|
|
|
+ m.Marshal{{.Encoder}}({{.Convert}}(o.{{.Name}}))
|
|
|
+ {{else if .IsBasic}}
|
|
|
+ {{if ge .Max 1}}
|
|
|
+ if l := len(o.{{.Name}}); l > {{.Max}} {
|
|
|
+ return xdr.ElementSizeExceeded("{{.Name}}", l, {{.Max}})
|
|
|
}
|
|
|
{{end}}
|
|
|
+ m.Marshal{{.Encoder}}(o.{{.Name}})
|
|
|
+ {{else}}
|
|
|
+ if err := o.{{.Name}}.MarshalXDRInto(m); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
{{end}}
|
|
|
- return xw.Tot(), xw.Error()
|
|
|
-}//+n
|
|
|
+{{end}}
|
|
|
|
|
|
-func (o *{{.TypeName}}) DecodeXDR(r io.Reader) error {
|
|
|
- xr := xdr.NewReader(r)
|
|
|
- return o.DecodeXDRFrom(xr)
|
|
|
-}//+n
|
|
|
+{{define "marshalSlice"}}
|
|
|
+ {{if ge .Max 1}}
|
|
|
+ if l := len(o.{{.Name}}); l > {{.Max}} {
|
|
|
+ return xdr.ElementSizeExceeded("{{.Name}}", l, {{.Max}})
|
|
|
+ }
|
|
|
+ {{end}}
|
|
|
+
|
|
|
+ m.MarshalUint32(uint32(len(o.{{.Name}})))
|
|
|
+ for i := range o.{{.Name}} {
|
|
|
+ {{if ne .Convert ""}}
|
|
|
+ m.Marshal{{.Encoder}}({{.Convert}}(o.{{.Name}}[i]))
|
|
|
+ {{else if .IsBasic}}
|
|
|
+ m.Marshal{{.Encoder}}(o.{{.Name}}[i])
|
|
|
+ {{else}}
|
|
|
+ if err := o.{{.Name}}[i].MarshalXDRInto(m); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ {{end}}
|
|
|
+ }
|
|
|
+{{end}}
|
|
|
|
|
|
-func (o *{{.TypeName}}) UnmarshalXDR(bs []byte) error {
|
|
|
- var br = bytes.NewReader(bs)
|
|
|
- var xr = xdr.NewReader(br)
|
|
|
- return o.DecodeXDRFrom(xr)
|
|
|
+func (o *{{.Name}}) UnmarshalXDR(bs []byte) error {
|
|
|
+ u := &xdr.Unmarshaller{Data: bs}
|
|
|
+ return o.UnmarshalXDRFrom(u)
|
|
|
+}
|
|
|
+
|
|
|
+func (o *{{.Name}}) UnmarshalXDRFrom(u *xdr.Unmarshaller) error {
|
|
|
+ {{range $fi := .Fields}}
|
|
|
+ {{if $fi.IsSlice}}
|
|
|
+ {{template "unmarshalSlice" $fi}}
|
|
|
+ {{else}}
|
|
|
+ {{template "unmarshalValue" $fi}}
|
|
|
+ {{end}}
|
|
|
+ {{end}}
|
|
|
+ return u.Error
|
|
|
}//+n
|
|
|
|
|
|
-func (o *{{.TypeName}}) DecodeXDRFrom(xr *xdr.Reader) error {
|
|
|
- {{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}}).DecodeXDRFrom(xr)
|
|
|
- {{end}}
|
|
|
+{{define "unmarshalValue"}}
|
|
|
+ {{if ne .Convert ""}}
|
|
|
+ o.{{.Name}} = {{.FieldType}}(u.Unmarshal{{.Encoder}}())
|
|
|
+ {{else if .IsBasic}}
|
|
|
+ {{if ge .Max 1}}
|
|
|
+ o.{{.Name}} = u.Unmarshal{{.Encoder}}Max({{.Max}})
|
|
|
{{else}}
|
|
|
- _{{$fieldInfo.Name}}Size := int(xr.ReadUint32())
|
|
|
- if _{{$fieldInfo.Name}}Size < 0 {
|
|
|
- return xdr.ElementSizeExceeded("{{$fieldInfo.Name}}", _{{$fieldInfo.Name}}Size, {{$fieldInfo.Max}})
|
|
|
+ o.{{.Name}} = u.Unmarshal{{.Encoder}}()
|
|
|
+ {{end}}
|
|
|
+ {{else}}
|
|
|
+ (&o.{{.Name}}).UnmarshalXDRFrom(u)
|
|
|
+ {{end}}
|
|
|
+{{end}}
|
|
|
+
|
|
|
+{{define "unmarshalSlice"}}
|
|
|
+ _{{.Name}}Size := int(u.UnmarshalUint32())
|
|
|
+ if _{{.Name}}Size < 0 {
|
|
|
+ return xdr.ElementSizeExceeded("{{.Name}}", _{{.Name}}Size, {{.Max}})
|
|
|
+ } else if _{{.Name}}Size == 0 {
|
|
|
+ o.{{.Name}} = nil
|
|
|
+ } else {
|
|
|
+ {{if ge .Max 1}}
|
|
|
+ if _{{.Name}}Size > {{.Max}} {
|
|
|
+ return xdr.ElementSizeExceeded("{{.Name}}", _{{.Name}}Size, {{.Max}})
|
|
|
}
|
|
|
- {{if ge $fieldInfo.Max 1}}
|
|
|
- if _{{$fieldInfo.Name}}Size > {{$fieldInfo.Max}} {
|
|
|
- return xdr.ElementSizeExceeded("{{$fieldInfo.Name}}", _{{$fieldInfo.Name}}Size, {{$fieldInfo.Max}})
|
|
|
- }
|
|
|
+ {{end}}
|
|
|
+ if _{{.Name}}Size <= len(o.{{.Name}}) {
|
|
|
+ {{if eq .FieldType "string"}}
|
|
|
+ for i := _{{.Name}}Size; i < len(o.{{.Name}}); i++ { o.{{.Name}}[i] = "" }
|
|
|
{{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}}
|
|
|
- {{if ge $fieldInfo.Submax 1}}
|
|
|
- o.{{$fieldInfo.Name}}[i] = xr.Read{{$fieldInfo.Encoder}}Max({{$fieldInfo.Submax}})
|
|
|
- {{else}}
|
|
|
- o.{{$fieldInfo.Name}}[i] = xr.Read{{$fieldInfo.Encoder}}()
|
|
|
- {{end}}
|
|
|
+ {{if eq .FieldType "[]byte"}}
|
|
|
+ for i := _{{.Name}}Size; i < len(o.{{.Name}}); i++ { o.{{.Name}}[i] = nil }
|
|
|
+ {{end}}
|
|
|
+ o.{{.Name}} = o.{{.Name}}[:_{{.Name}}Size]
|
|
|
+ } else {
|
|
|
+ o.{{.Name}} = make([]{{.FieldType}}, _{{.Name}}Size)
|
|
|
+ }
|
|
|
+ for i := range o.{{.Name}} {
|
|
|
+ {{if ne .Convert ""}}
|
|
|
+ o.{{.Name}}[i] = {{.FieldType}}(u.Unmarshal{{.Encoder}}())
|
|
|
+ {{else if .IsBasic}}
|
|
|
+ {{if ge .Submax 1}}
|
|
|
+ o.{{.Name}}[i] = u.Unmarshal{{.Encoder}}Max({{.Submax}})
|
|
|
{{else}}
|
|
|
- (&o.{{$fieldInfo.Name}}[i]).DecodeXDRFrom(xr)
|
|
|
+ o.{{.Name}}[i] = u.Unmarshal{{.Encoder}}()
|
|
|
{{end}}
|
|
|
- }
|
|
|
- {{end}}
|
|
|
- {{end}}
|
|
|
- return xr.Error()
|
|
|
-}`))
|
|
|
+ {{else}}
|
|
|
+ (&o.{{.Name}}[i]).UnmarshalXDRFrom(u)
|
|
|
+ {{end}}
|
|
|
+ }
|
|
|
+ }
|
|
|
+{{end}}
|
|
|
+`
|
|
|
+
|
|
|
+var (
|
|
|
+ encodeTpl = template.Must(template.New("encoder").Parse(encoderData))
|
|
|
+ headerTpl = template.Must(template.New("header").Parse(headerData))
|
|
|
+)
|
|
|
|
|
|
var emptyTypeTpl = template.Must(template.New("encoder").Parse(`
|
|
|
-func (o {{.TypeName}}) EncodeXDR(w io.Writer) (int, error) {
|
|
|
- return 0, nil
|
|
|
-}//+n
|
|
|
+func (o {{.Name}}) XDRSize() int {
|
|
|
+ return 0
|
|
|
+}
|
|
|
|
|
|
-func (o {{.TypeName}}) MarshalXDR() ([]byte, error) {
|
|
|
+func (o {{.Name}}) MarshalXDR() ([]byte, error) {
|
|
|
return nil, nil
|
|
|
}//+n
|
|
|
|
|
|
-func (o {{.TypeName}}) MustMarshalXDR() []byte {
|
|
|
+func (o {{.Name}}) MustMarshalXDR() []byte {
|
|
|
return nil
|
|
|
}//+n
|
|
|
|
|
|
-func (o {{.TypeName}}) AppendXDR(bs []byte) ([]byte, error) {
|
|
|
- return bs, nil
|
|
|
-}//+n
|
|
|
-
|
|
|
-func (o {{.TypeName}}) EncodeXDRInto(xw *xdr.Writer) (int, error) {
|
|
|
- return xw.Tot(), xw.Error()
|
|
|
+func (o {{.Name}}) MarshalXDRInto(m *xdr.Marshaller) error {
|
|
|
+ return nil
|
|
|
}//+n
|
|
|
|
|
|
-func (o *{{.TypeName}}) DecodeXDR(r io.Reader) error {
|
|
|
+func (o *{{.Name}}) UnmarshalXDR(bs []byte) error {
|
|
|
return nil
|
|
|
}//+n
|
|
|
|
|
|
-func (o *{{.TypeName}}) UnmarshalXDR(bs []byte) error {
|
|
|
+func (o *{{.Name}}) UnmarshalXDRFrom(u *xdr.Unmarshaller) error {
|
|
|
return nil
|
|
|
}//+n
|
|
|
-
|
|
|
-func (o *{{.TypeName}}) DecodeXDRFrom(xr *xdr.Reader) error {
|
|
|
- return xr.Error()
|
|
|
-}`))
|
|
|
+`))
|
|
|
|
|
|
var maxRe = regexp.MustCompile(`(?:\Wmax:)(\d+)(?:\s*,\s*(\d+))?`)
|
|
|
|
|
@@ -286,7 +342,7 @@ func handleStruct(t *ast.StructType) []fieldInfo {
|
|
|
f = fieldInfo{
|
|
|
Name: fn,
|
|
|
IsBasic: true,
|
|
|
- FieldType: tn,
|
|
|
+ FieldType: "[]" + tn,
|
|
|
Encoder: enc.Encoder,
|
|
|
Convert: enc.Type,
|
|
|
Max: max1,
|
|
@@ -329,17 +385,14 @@ func handleStruct(t *ast.StructType) []fieldInfo {
|
|
|
}
|
|
|
|
|
|
func generateCode(output io.Writer, s structInfo) {
|
|
|
- name := s.Name
|
|
|
- fs := s.Fields
|
|
|
-
|
|
|
var buf bytes.Buffer
|
|
|
var err error
|
|
|
- if len(fs) == 0 {
|
|
|
+ if len(s.Fields) == 0 {
|
|
|
// This is an empty type. We can create a quite simple codec for it.
|
|
|
- err = emptyTypeTpl.Execute(&buf, map[string]interface{}{"TypeName": name})
|
|
|
+ err = emptyTypeTpl.Execute(&buf, s)
|
|
|
} else {
|
|
|
// Generate with the default template.
|
|
|
- err = encodeTpl.Execute(&buf, map[string]interface{}{"TypeName": name, "Fields": fs})
|
|
|
+ err = encodeTpl.Execute(&buf, s)
|
|
|
}
|
|
|
if err != nil {
|
|
|
panic(err)
|
|
@@ -347,12 +400,7 @@ func generateCode(output io.Writer, s structInfo) {
|
|
|
|
|
|
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.Fprintln(output, string(bs))
|
|
|
+ output.Write(bs)
|
|
|
}
|
|
|
|
|
|
func uncamelize(s string) string {
|
|
@@ -384,46 +432,46 @@ func generateDiagram(output io.Writer, s structInfo) {
|
|
|
tn := f.FieldType
|
|
|
name := uncamelize(f.Name)
|
|
|
|
|
|
+ suffix := ""
|
|
|
if f.IsSlice {
|
|
|
fmt.Fprintf(output, "| %s |\n", center("Number of "+name, 61))
|
|
|
fmt.Fprintln(output, line)
|
|
|
+ suffix = " (n items)"
|
|
|
+ fmt.Fprintf(output, "/ %s /\n", center("", 61))
|
|
|
}
|
|
|
switch tn {
|
|
|
case "bool":
|
|
|
fmt.Fprintf(output, "| %s |V|\n", center(name+" (V=0 or 1)", 59))
|
|
|
- fmt.Fprintln(output, line)
|
|
|
case "int16", "uint16":
|
|
|
- fmt.Fprintf(output, "| %s | %s |\n", center("0x0000", 29), center(name, 29))
|
|
|
- fmt.Fprintln(output, line)
|
|
|
+ fmt.Fprintf(output, "| %s | %s |\n", center("16 zero bits", 29), center(name, 29))
|
|
|
+ case "int8", "uint8":
|
|
|
+ fmt.Fprintf(output, "| %s | %s |\n", center("24 zero bits", 45), center(name, 13))
|
|
|
case "int32", "uint32":
|
|
|
- fmt.Fprintf(output, "| %s |\n", center(name, 61))
|
|
|
- fmt.Fprintln(output, line)
|
|
|
+ fmt.Fprintf(output, "| %s |\n", center(name+suffix, 61))
|
|
|
case "int64", "uint64":
|
|
|
fmt.Fprintf(output, "| %-61s |\n", "")
|
|
|
fmt.Fprintf(output, "+ %s +\n", center(name+" (64 bits)", 61))
|
|
|
fmt.Fprintf(output, "| %-61s |\n", "")
|
|
|
- fmt.Fprintln(output, line)
|
|
|
- case "string", "byte": // XXX We assume slice of byte!
|
|
|
- fmt.Fprintf(output, "| %s |\n", center("Length of "+name, 61))
|
|
|
- fmt.Fprintln(output, line)
|
|
|
+ case "string", "[]byte":
|
|
|
fmt.Fprintf(output, "/ %61s /\n", "")
|
|
|
- fmt.Fprintf(output, "\\ %s \\\n", center(name+" (variable length)", 61))
|
|
|
+ fmt.Fprintf(output, "\\ %s \\\n", center(name+" (length + padded data)", 61))
|
|
|
fmt.Fprintf(output, "/ %61s /\n", "")
|
|
|
- fmt.Fprintln(output, line)
|
|
|
default:
|
|
|
if f.IsSlice {
|
|
|
tn = "Zero or more " + tn + " Structures"
|
|
|
- fmt.Fprintf(output, "/ %s /\n", center("", 61))
|
|
|
fmt.Fprintf(output, "\\ %s \\\n", center(tn, 61))
|
|
|
- fmt.Fprintf(output, "/ %s /\n", center("", 61))
|
|
|
} else {
|
|
|
tn = tn + " Structure"
|
|
|
fmt.Fprintf(output, "/ %s /\n", center("", 61))
|
|
|
fmt.Fprintf(output, "\\ %s \\\n", center(tn, 61))
|
|
|
fmt.Fprintf(output, "/ %s /\n", center("", 61))
|
|
|
}
|
|
|
- fmt.Fprintln(output, line)
|
|
|
}
|
|
|
+
|
|
|
+ if f.IsSlice {
|
|
|
+ fmt.Fprintf(output, "/ %s /\n", center("", 61))
|
|
|
+ }
|
|
|
+ fmt.Fprintln(output, line)
|
|
|
}
|
|
|
fmt.Fprintln(output)
|
|
|
fmt.Fprintln(output)
|
|
@@ -448,9 +496,9 @@ func generateXdr(output io.Writer, s structInfo) {
|
|
|
}
|
|
|
|
|
|
switch tn {
|
|
|
- case "int16", "int32":
|
|
|
+ case "int8", "int16", "int32":
|
|
|
fmt.Fprintf(output, "\tint %s%s;\n", fn, suf)
|
|
|
- case "uint16", "uint32":
|
|
|
+ case "uint8", "uint16", "uint32":
|
|
|
fmt.Fprintf(output, "\tunsigned int %s%s;\n", fn, suf)
|
|
|
case "int64":
|
|
|
fmt.Fprintf(output, "\thyper %s%s;\n", fn, suf)
|
|
@@ -458,7 +506,7 @@ func generateXdr(output io.Writer, s structInfo) {
|
|
|
fmt.Fprintf(output, "\tunsigned hyper %s%s;\n", fn, suf)
|
|
|
case "string":
|
|
|
fmt.Fprintf(output, "\tstring %s<%s>;\n", fn, l)
|
|
|
- case "byte":
|
|
|
+ case "[]byte":
|
|
|
fmt.Fprintf(output, "\topaque %s<%s>;\n", fn, l)
|
|
|
default:
|
|
|
fmt.Fprintf(output, "\t%s %s%s;\n", tn, fn, suf)
|
|
@@ -510,6 +558,22 @@ func main() {
|
|
|
i := inspector(&structs)
|
|
|
ast.Inspect(f, i)
|
|
|
|
|
|
+ buf := new(bytes.Buffer)
|
|
|
+ headerTpl.Execute(buf, map[string]string{"Package": f.Name.Name})
|
|
|
+ for _, s := range structs {
|
|
|
+ fmt.Fprintf(buf, "\n/*\n\n")
|
|
|
+ generateDiagram(buf, s)
|
|
|
+ generateXdr(buf, s)
|
|
|
+ fmt.Fprintf(buf, "*/\n")
|
|
|
+ generateCode(buf, s)
|
|
|
+ }
|
|
|
+
|
|
|
+ bs, err := format.Source(buf.Bytes())
|
|
|
+ if err != nil {
|
|
|
+ log.Print(buf.String())
|
|
|
+ log.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
var output io.Writer = os.Stdout
|
|
|
if *outputFile != "" {
|
|
|
fd, err := os.Create(*outputFile)
|
|
@@ -518,13 +582,5 @@ func main() {
|
|
|
}
|
|
|
output = fd
|
|
|
}
|
|
|
-
|
|
|
- headerTpl.Execute(output, map[string]string{"Package": f.Name.Name})
|
|
|
- for _, s := range structs {
|
|
|
- fmt.Fprintf(output, "\n/*\n\n")
|
|
|
- generateDiagram(output, s)
|
|
|
- generateXdr(output, s)
|
|
|
- fmt.Fprintf(output, "*/\n")
|
|
|
- generateCode(output, s)
|
|
|
- }
|
|
|
+ output.Write(bs)
|
|
|
}
|