From e937e51476319380ab02fa0cc35909997230465a Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Sun, 6 Jul 2014 19:21:37 +0200 Subject: [PATCH] Add AppendXDR to XDR types, build.sh xdr --- build.sh | 11 ++++++ discover/packets_xdr.go | 24 ++++++++++-- files/leveldb_xdr.go | 12 +++++- protocol/{message_types.go => message.go} | 0 protocol/message_xdr.go | 48 +++++++++++++++++++---- scanner/blocks_xdr.go | 14 ++++--- scanner/file_xdr.go | 14 ++++--- xdr/cmd/{coder => genxdr}/main.go | 6 ++- 8 files changed, 104 insertions(+), 25 deletions(-) rename protocol/{message_types.go => message.go} (100%) rename xdr/cmd/{coder => genxdr}/main.go (98%) diff --git a/build.sh b/build.sh index a073c30b5..14e4791b9 100755 --- a/build.sh +++ b/build.sh @@ -98,6 +98,13 @@ setup() { go get -u code.google.com/p/go.tools/cmd/vet } +xdr() { + go install ./xdr/cmd/genxdr + for f in discover/packets files/leveldb protocol/message scanner/blocks scanner/file ; do + genxdr "${f}.go" > "${f}_xdr.go" + done +} + case "$1" in "") shift @@ -201,6 +208,10 @@ case "$1" in setup ;; + xdr) + xdr + ;; + *) echo "Unknown build parameter $1" ;; diff --git a/discover/packets_xdr.go b/discover/packets_xdr.go index 4e1cd1740..2eee7487e 100644 --- a/discover/packets_xdr.go +++ b/discover/packets_xdr.go @@ -13,7 +13,11 @@ func (o QueryV2) EncodeXDR(w io.Writer) (int, error) { } func (o QueryV2) MarshalXDR() []byte { - var aw = make(xdr.AppendWriter, 0, 128) + return o.AppendXDR(make([]byte, 0, 128)) +} + +func (o QueryV2) AppendXDR(bs []byte) []byte { + var aw = xdr.AppendWriter(bs) var xw = xdr.NewWriter(&aw) o.encodeXDR(xw) return []byte(aw) @@ -51,7 +55,11 @@ func (o AnnounceV2) EncodeXDR(w io.Writer) (int, error) { } func (o AnnounceV2) MarshalXDR() []byte { - var aw = make(xdr.AppendWriter, 0, 128) + return o.AppendXDR(make([]byte, 0, 128)) +} + +func (o AnnounceV2) AppendXDR(bs []byte) []byte { + var aw = xdr.AppendWriter(bs) var xw = xdr.NewWriter(&aw) o.encodeXDR(xw) return []byte(aw) @@ -101,7 +109,11 @@ func (o Node) EncodeXDR(w io.Writer) (int, error) { } func (o Node) MarshalXDR() []byte { - var aw = make(xdr.AppendWriter, 0, 128) + return o.AppendXDR(make([]byte, 0, 128)) +} + +func (o Node) AppendXDR(bs []byte) []byte { + var aw = xdr.AppendWriter(bs) var xw = xdr.NewWriter(&aw) o.encodeXDR(xw) return []byte(aw) @@ -152,7 +164,11 @@ func (o Address) EncodeXDR(w io.Writer) (int, error) { } func (o Address) MarshalXDR() []byte { - var aw = make(xdr.AppendWriter, 0, 128) + return o.AppendXDR(make([]byte, 0, 128)) +} + +func (o Address) AppendXDR(bs []byte) []byte { + var aw = xdr.AppendWriter(bs) var xw = xdr.NewWriter(&aw) o.encodeXDR(xw) return []byte(aw) diff --git a/files/leveldb_xdr.go b/files/leveldb_xdr.go index 9bf9962a3..4bfbac6e7 100644 --- a/files/leveldb_xdr.go +++ b/files/leveldb_xdr.go @@ -13,7 +13,11 @@ func (o fileVersion) EncodeXDR(w io.Writer) (int, error) { } func (o fileVersion) MarshalXDR() []byte { - var aw = make(xdr.AppendWriter, 0, 128) + return o.AppendXDR(make([]byte, 0, 128)) +} + +func (o fileVersion) AppendXDR(bs []byte) []byte { + var aw = xdr.AppendWriter(bs) var xw = xdr.NewWriter(&aw) o.encodeXDR(xw) return []byte(aw) @@ -48,7 +52,11 @@ func (o versionList) EncodeXDR(w io.Writer) (int, error) { } func (o versionList) MarshalXDR() []byte { - var aw = make(xdr.AppendWriter, 0, 128) + return o.AppendXDR(make([]byte, 0, 128)) +} + +func (o versionList) AppendXDR(bs []byte) []byte { + var aw = xdr.AppendWriter(bs) var xw = xdr.NewWriter(&aw) o.encodeXDR(xw) return []byte(aw) diff --git a/protocol/message_types.go b/protocol/message.go similarity index 100% rename from protocol/message_types.go rename to protocol/message.go diff --git a/protocol/message_xdr.go b/protocol/message_xdr.go index b95bf783d..9be18cdb6 100644 --- a/protocol/message_xdr.go +++ b/protocol/message_xdr.go @@ -13,7 +13,11 @@ func (o IndexMessage) EncodeXDR(w io.Writer) (int, error) { } func (o IndexMessage) MarshalXDR() []byte { - var aw = make(xdr.AppendWriter, 0, 128) + return o.AppendXDR(make([]byte, 0, 128)) +} + +func (o IndexMessage) AppendXDR(bs []byte) []byte { + var aw = xdr.AppendWriter(bs) var xw = xdr.NewWriter(&aw) o.encodeXDR(xw) return []byte(aw) @@ -64,7 +68,11 @@ func (o FileInfo) EncodeXDR(w io.Writer) (int, error) { } func (o FileInfo) MarshalXDR() []byte { - var aw = make(xdr.AppendWriter, 0, 128) + return o.AppendXDR(make([]byte, 0, 128)) +} + +func (o FileInfo) AppendXDR(bs []byte) []byte { + var aw = xdr.AppendWriter(bs) var xw = xdr.NewWriter(&aw) o.encodeXDR(xw) return []byte(aw) @@ -121,7 +129,11 @@ func (o BlockInfo) EncodeXDR(w io.Writer) (int, error) { } func (o BlockInfo) MarshalXDR() []byte { - var aw = make(xdr.AppendWriter, 0, 128) + return o.AppendXDR(make([]byte, 0, 128)) +} + +func (o BlockInfo) AppendXDR(bs []byte) []byte { + var aw = xdr.AppendWriter(bs) var xw = xdr.NewWriter(&aw) o.encodeXDR(xw) return []byte(aw) @@ -159,7 +171,11 @@ func (o RequestMessage) EncodeXDR(w io.Writer) (int, error) { } func (o RequestMessage) MarshalXDR() []byte { - var aw = make(xdr.AppendWriter, 0, 128) + return o.AppendXDR(make([]byte, 0, 128)) +} + +func (o RequestMessage) AppendXDR(bs []byte) []byte { + var aw = xdr.AppendWriter(bs) var xw = xdr.NewWriter(&aw) o.encodeXDR(xw) return []byte(aw) @@ -204,7 +220,11 @@ func (o ClusterConfigMessage) EncodeXDR(w io.Writer) (int, error) { } func (o ClusterConfigMessage) MarshalXDR() []byte { - var aw = make(xdr.AppendWriter, 0, 128) + return o.AppendXDR(make([]byte, 0, 128)) +} + +func (o ClusterConfigMessage) AppendXDR(bs []byte) []byte { + var aw = xdr.AppendWriter(bs) var xw = xdr.NewWriter(&aw) o.encodeXDR(xw) return []byte(aw) @@ -275,7 +295,11 @@ func (o Repository) EncodeXDR(w io.Writer) (int, error) { } func (o Repository) MarshalXDR() []byte { - var aw = make(xdr.AppendWriter, 0, 128) + return o.AppendXDR(make([]byte, 0, 128)) +} + +func (o Repository) AppendXDR(bs []byte) []byte { + var aw = xdr.AppendWriter(bs) var xw = xdr.NewWriter(&aw) o.encodeXDR(xw) return []byte(aw) @@ -326,7 +350,11 @@ func (o Node) EncodeXDR(w io.Writer) (int, error) { } func (o Node) MarshalXDR() []byte { - var aw = make(xdr.AppendWriter, 0, 128) + return o.AppendXDR(make([]byte, 0, 128)) +} + +func (o Node) AppendXDR(bs []byte) []byte { + var aw = xdr.AppendWriter(bs) var xw = xdr.NewWriter(&aw) o.encodeXDR(xw) return []byte(aw) @@ -366,7 +394,11 @@ func (o Option) EncodeXDR(w io.Writer) (int, error) { } func (o Option) MarshalXDR() []byte { - var aw = make(xdr.AppendWriter, 0, 128) + return o.AppendXDR(make([]byte, 0, 128)) +} + +func (o Option) AppendXDR(bs []byte) []byte { + var aw = xdr.AppendWriter(bs) var xw = xdr.NewWriter(&aw) o.encodeXDR(xw) return []byte(aw) diff --git a/scanner/blocks_xdr.go b/scanner/blocks_xdr.go index b581c9bb7..92ec1b6f9 100644 --- a/scanner/blocks_xdr.go +++ b/scanner/blocks_xdr.go @@ -13,10 +13,14 @@ func (o Block) EncodeXDR(w io.Writer) (int, error) { } func (o Block) MarshalXDR() []byte { - var buf bytes.Buffer - var xw = xdr.NewWriter(&buf) + return o.AppendXDR(make([]byte, 0, 128)) +} + +func (o Block) AppendXDR(bs []byte) []byte { + var aw = xdr.AppendWriter(bs) + var xw = xdr.NewWriter(&aw) o.encodeXDR(xw) - return buf.Bytes() + return []byte(aw) } func (o Block) encodeXDR(xw *xdr.Writer) (int, error) { @@ -32,8 +36,8 @@ func (o *Block) DecodeXDR(r io.Reader) error { } func (o *Block) UnmarshalXDR(bs []byte) error { - var buf = bytes.NewBuffer(bs) - var xr = xdr.NewReader(buf) + var br = bytes.NewReader(bs) + var xr = xdr.NewReader(br) return o.decodeXDR(xr) } diff --git a/scanner/file_xdr.go b/scanner/file_xdr.go index 2009c074b..af7ab4296 100644 --- a/scanner/file_xdr.go +++ b/scanner/file_xdr.go @@ -13,10 +13,14 @@ func (o File) EncodeXDR(w io.Writer) (int, error) { } func (o File) MarshalXDR() []byte { - var buf bytes.Buffer - var xw = xdr.NewWriter(&buf) + return o.AppendXDR(make([]byte, 0, 128)) +} + +func (o File) AppendXDR(bs []byte) []byte { + var aw = xdr.AppendWriter(bs) + var xw = xdr.NewWriter(&aw) o.encodeXDR(xw) - return buf.Bytes() + return []byte(aw) } func (o File) encodeXDR(xw *xdr.Writer) (int, error) { @@ -39,8 +43,8 @@ func (o *File) DecodeXDR(r io.Reader) error { } func (o *File) UnmarshalXDR(bs []byte) error { - var buf = bytes.NewBuffer(bs) - var xr = xdr.NewReader(buf) + var br = bytes.NewReader(bs) + var xr = xdr.NewReader(br) return o.decodeXDR(xr) } diff --git a/xdr/cmd/coder/main.go b/xdr/cmd/genxdr/main.go similarity index 98% rename from xdr/cmd/coder/main.go rename to xdr/cmd/genxdr/main.go index 4f3a25c42..dac3fec5b 100644 --- a/xdr/cmd/coder/main.go +++ b/xdr/cmd/genxdr/main.go @@ -50,7 +50,11 @@ func (o {{.TypeName}}) EncodeXDR(w io.Writer) (int, error) { }//+n func (o {{.TypeName}}) MarshalXDR() []byte { - var aw = make(xdr.AppendWriter, 0, 128) + return o.AppendXDR(make([]byte, 0, 128)) +}//+n + +func (o {{.TypeName}}) AppendXDR(bs []byte) []byte { + var aw = xdr.AppendWriter(bs) var xw = xdr.NewWriter(&aw) o.encodeXDR(xw) return []byte(aw)