Parcourir la source

Regenerate XDR for empty struct types

Jakob Borg il y a 10 ans
Parent
commit
e5b33ce9f6

+ 1 - 1
Godeps/Godeps.json

@@ -19,7 +19,7 @@
 		},
 		{
 			"ImportPath": "github.com/calmh/xdr",
-			"Rev": "47c0042d09a827b81ee62497f99e5e0c7f0bd31c"
+			"Rev": "9eb3e1a622d9364deb39c831f7e5f164393d7e37"
 		},
 		{
 			"ImportPath": "github.com/golang/snappy",

+ 49 - 1
Godeps/_workspace/src/github.com/calmh/xdr/cmd/genxdr/main.go

@@ -171,6 +171,39 @@ func (o *{{.TypeName}}) DecodeXDRFrom(xr *xdr.Reader) error {
 	return xr.Error()
 }`))
 
+var emptyTypeTpl = template.Must(template.New("encoder").Parse(`
+func (o {{.TypeName}}) EncodeXDR(w io.Writer) (int, error) {
+	return 0, nil
+}//+n
+
+func (o {{.TypeName}}) MarshalXDR() ([]byte, error) {
+	return nil, nil
+}//+n
+
+func (o {{.TypeName}}) 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()
+}//+n
+
+func (o *{{.TypeName}}) DecodeXDR(r io.Reader) error {
+	return nil
+}//+n
+
+func (o *{{.TypeName}}) UnmarshalXDR(bs []byte) error {
+	return nil
+}//+n
+
+func (o *{{.TypeName}}) DecodeXDRFrom(xr *xdr.Reader) error {
+	return xr.Error()
+}`))
+
 var maxRe = regexp.MustCompile(`(?:\Wmax:)(\d+)(?:\s*,\s*(\d+))?`)
 
 type typeSet struct {
@@ -300,7 +333,14 @@ func generateCode(output io.Writer, s structInfo) {
 	fs := s.Fields
 
 	var buf bytes.Buffer
-	err := encodeTpl.Execute(&buf, map[string]interface{}{"TypeName": name, "Fields": fs})
+	var err error
+	if len(fs) == 0 {
+		// This is an empty type. We can create a quite simple codec for it.
+		err = emptyTypeTpl.Execute(&buf, map[string]interface{}{"TypeName": name})
+	} else {
+		// Generate with the default template.
+		err = encodeTpl.Execute(&buf, map[string]interface{}{"TypeName": name, "Fields": fs})
+	}
 	if err != nil {
 		panic(err)
 	}
@@ -326,6 +366,14 @@ func generateDiagram(output io.Writer, s structInfo) {
 	fs := s.Fields
 
 	fmt.Fprintln(output, sn+" Structure:")
+
+	if len(fs) == 0 {
+		fmt.Fprintln(output, "(contains no fields)")
+		fmt.Fprintln(output)
+		fmt.Fprintln(output)
+		return
+	}
+
 	fmt.Fprintln(output)
 	fmt.Fprintln(output, " 0                   1                   2                   3")
 	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")

+ 1 - 1
build.go

@@ -405,7 +405,7 @@ func assets() {
 }
 
 func xdr() {
-	runPrint("go", "generate", "./lib/discover", "./lib/db", "./lib/protocol")
+	runPrint("go", "generate", "./lib/discover", "./lib/db", "./lib/protocol", "./lib/relay/protocol")
 }
 
 func translate() {

+ 7 - 21
lib/protocol/message_xdr.go

@@ -1152,10 +1152,7 @@ func (o *CloseMessage) DecodeXDRFrom(xr *xdr.Reader) error {
 /*
 
 EmptyMessage Structure:
-
- 0                   1                   2                   3
- 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
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+(contains no fields)
 
 
 struct EmptyMessage {
@@ -1164,27 +1161,19 @@ struct EmptyMessage {
 */
 
 func (o EmptyMessage) EncodeXDR(w io.Writer) (int, error) {
-	var xw = xdr.NewWriter(w)
-	return o.EncodeXDRInto(xw)
+	return 0, nil
 }
 
 func (o EmptyMessage) MarshalXDR() ([]byte, error) {
-	return o.AppendXDR(make([]byte, 0, 128))
+	return nil, nil
 }
 
 func (o EmptyMessage) MustMarshalXDR() []byte {
-	bs, err := o.MarshalXDR()
-	if err != nil {
-		panic(err)
-	}
-	return bs
+	return nil
 }
 
 func (o EmptyMessage) AppendXDR(bs []byte) ([]byte, error) {
-	var aw = xdr.AppendWriter(bs)
-	var xw = xdr.NewWriter(&aw)
-	_, err := o.EncodeXDRInto(xw)
-	return []byte(aw), err
+	return bs, nil
 }
 
 func (o EmptyMessage) EncodeXDRInto(xw *xdr.Writer) (int, error) {
@@ -1192,14 +1181,11 @@ func (o EmptyMessage) EncodeXDRInto(xw *xdr.Writer) (int, error) {
 }
 
 func (o *EmptyMessage) DecodeXDR(r io.Reader) error {
-	xr := xdr.NewReader(r)
-	return o.DecodeXDRFrom(xr)
+	return nil
 }
 
 func (o *EmptyMessage) UnmarshalXDR(bs []byte) error {
-	var br = bytes.NewReader(bs)
-	var xr = xdr.NewReader(br)
-	return o.DecodeXDRFrom(xr)
+	return nil
 }
 
 func (o *EmptyMessage) DecodeXDRFrom(xr *xdr.Reader) error {

+ 28 - 84
lib/relay/protocol/packets_xdr.go

@@ -86,10 +86,7 @@ func (o *header) DecodeXDRFrom(xr *xdr.Reader) error {
 /*
 
 Ping Structure:
-
- 0                   1                   2                   3
- 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
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+(contains no fields)
 
 
 struct Ping {
@@ -98,27 +95,19 @@ struct Ping {
 */
 
 func (o Ping) EncodeXDR(w io.Writer) (int, error) {
-	var xw = xdr.NewWriter(w)
-	return o.EncodeXDRInto(xw)
+	return 0, nil
 }
 
 func (o Ping) MarshalXDR() ([]byte, error) {
-	return o.AppendXDR(make([]byte, 0, 128))
+	return nil, nil
 }
 
 func (o Ping) MustMarshalXDR() []byte {
-	bs, err := o.MarshalXDR()
-	if err != nil {
-		panic(err)
-	}
-	return bs
+	return nil
 }
 
 func (o Ping) AppendXDR(bs []byte) ([]byte, error) {
-	var aw = xdr.AppendWriter(bs)
-	var xw = xdr.NewWriter(&aw)
-	_, err := o.EncodeXDRInto(xw)
-	return []byte(aw), err
+	return bs, nil
 }
 
 func (o Ping) EncodeXDRInto(xw *xdr.Writer) (int, error) {
@@ -126,14 +115,11 @@ func (o Ping) EncodeXDRInto(xw *xdr.Writer) (int, error) {
 }
 
 func (o *Ping) DecodeXDR(r io.Reader) error {
-	xr := xdr.NewReader(r)
-	return o.DecodeXDRFrom(xr)
+	return nil
 }
 
 func (o *Ping) UnmarshalXDR(bs []byte) error {
-	var br = bytes.NewReader(bs)
-	var xr = xdr.NewReader(br)
-	return o.DecodeXDRFrom(xr)
+	return nil
 }
 
 func (o *Ping) DecodeXDRFrom(xr *xdr.Reader) error {
@@ -143,10 +129,7 @@ func (o *Ping) DecodeXDRFrom(xr *xdr.Reader) error {
 /*
 
 Pong Structure:
-
- 0                   1                   2                   3
- 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
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+(contains no fields)
 
 
 struct Pong {
@@ -155,27 +138,19 @@ struct Pong {
 */
 
 func (o Pong) EncodeXDR(w io.Writer) (int, error) {
-	var xw = xdr.NewWriter(w)
-	return o.EncodeXDRInto(xw)
+	return 0, nil
 }
 
 func (o Pong) MarshalXDR() ([]byte, error) {
-	return o.AppendXDR(make([]byte, 0, 128))
+	return nil, nil
 }
 
 func (o Pong) MustMarshalXDR() []byte {
-	bs, err := o.MarshalXDR()
-	if err != nil {
-		panic(err)
-	}
-	return bs
+	return nil
 }
 
 func (o Pong) AppendXDR(bs []byte) ([]byte, error) {
-	var aw = xdr.AppendWriter(bs)
-	var xw = xdr.NewWriter(&aw)
-	_, err := o.EncodeXDRInto(xw)
-	return []byte(aw), err
+	return bs, nil
 }
 
 func (o Pong) EncodeXDRInto(xw *xdr.Writer) (int, error) {
@@ -183,14 +158,11 @@ func (o Pong) EncodeXDRInto(xw *xdr.Writer) (int, error) {
 }
 
 func (o *Pong) DecodeXDR(r io.Reader) error {
-	xr := xdr.NewReader(r)
-	return o.DecodeXDRFrom(xr)
+	return nil
 }
 
 func (o *Pong) UnmarshalXDR(bs []byte) error {
-	var br = bytes.NewReader(bs)
-	var xr = xdr.NewReader(br)
-	return o.DecodeXDRFrom(xr)
+	return nil
 }
 
 func (o *Pong) DecodeXDRFrom(xr *xdr.Reader) error {
@@ -200,10 +172,7 @@ func (o *Pong) DecodeXDRFrom(xr *xdr.Reader) error {
 /*
 
 JoinRelayRequest Structure:
-
- 0                   1                   2                   3
- 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
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+(contains no fields)
 
 
 struct JoinRelayRequest {
@@ -212,27 +181,19 @@ struct JoinRelayRequest {
 */
 
 func (o JoinRelayRequest) EncodeXDR(w io.Writer) (int, error) {
-	var xw = xdr.NewWriter(w)
-	return o.EncodeXDRInto(xw)
+	return 0, nil
 }
 
 func (o JoinRelayRequest) MarshalXDR() ([]byte, error) {
-	return o.AppendXDR(make([]byte, 0, 128))
+	return nil, nil
 }
 
 func (o JoinRelayRequest) MustMarshalXDR() []byte {
-	bs, err := o.MarshalXDR()
-	if err != nil {
-		panic(err)
-	}
-	return bs
+	return nil
 }
 
 func (o JoinRelayRequest) AppendXDR(bs []byte) ([]byte, error) {
-	var aw = xdr.AppendWriter(bs)
-	var xw = xdr.NewWriter(&aw)
-	_, err := o.EncodeXDRInto(xw)
-	return []byte(aw), err
+	return bs, nil
 }
 
 func (o JoinRelayRequest) EncodeXDRInto(xw *xdr.Writer) (int, error) {
@@ -240,14 +201,11 @@ func (o JoinRelayRequest) EncodeXDRInto(xw *xdr.Writer) (int, error) {
 }
 
 func (o *JoinRelayRequest) DecodeXDR(r io.Reader) error {
-	xr := xdr.NewReader(r)
-	return o.DecodeXDRFrom(xr)
+	return nil
 }
 
 func (o *JoinRelayRequest) UnmarshalXDR(bs []byte) error {
-	var br = bytes.NewReader(bs)
-	var xr = xdr.NewReader(br)
-	return o.DecodeXDRFrom(xr)
+	return nil
 }
 
 func (o *JoinRelayRequest) DecodeXDRFrom(xr *xdr.Reader) error {
@@ -257,10 +215,7 @@ func (o *JoinRelayRequest) DecodeXDRFrom(xr *xdr.Reader) error {
 /*
 
 RelayFull Structure:
-
- 0                   1                   2                   3
- 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
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+(contains no fields)
 
 
 struct RelayFull {
@@ -269,27 +224,19 @@ struct RelayFull {
 */
 
 func (o RelayFull) EncodeXDR(w io.Writer) (int, error) {
-	var xw = xdr.NewWriter(w)
-	return o.EncodeXDRInto(xw)
+	return 0, nil
 }
 
 func (o RelayFull) MarshalXDR() ([]byte, error) {
-	return o.AppendXDR(make([]byte, 0, 128))
+	return nil, nil
 }
 
 func (o RelayFull) MustMarshalXDR() []byte {
-	bs, err := o.MarshalXDR()
-	if err != nil {
-		panic(err)
-	}
-	return bs
+	return nil
 }
 
 func (o RelayFull) AppendXDR(bs []byte) ([]byte, error) {
-	var aw = xdr.AppendWriter(bs)
-	var xw = xdr.NewWriter(&aw)
-	_, err := o.EncodeXDRInto(xw)
-	return []byte(aw), err
+	return bs, nil
 }
 
 func (o RelayFull) EncodeXDRInto(xw *xdr.Writer) (int, error) {
@@ -297,14 +244,11 @@ func (o RelayFull) EncodeXDRInto(xw *xdr.Writer) (int, error) {
 }
 
 func (o *RelayFull) DecodeXDR(r io.Reader) error {
-	xr := xdr.NewReader(r)
-	return o.DecodeXDRFrom(xr)
+	return nil
 }
 
 func (o *RelayFull) UnmarshalXDR(bs []byte) error {
-	var br = bytes.NewReader(bs)
-	var xr = xdr.NewReader(br)
-	return o.DecodeXDRFrom(xr)
+	return nil
 }
 
 func (o *RelayFull) DecodeXDRFrom(xr *xdr.Reader) error {