From 5e342c01c78a9f9eebb71fa0454240108137fc42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dario=20Casta=C3=B1=C3=A9?= Date: Sat, 25 Aug 2018 11:10:52 +0200 Subject: [PATCH] Fixed linting warnings --- cmd/genxdr/main.go | 2 +- common.go | 9 +++++---- marshaller.go | 14 +++++++------- unmarshal.go | 16 ++++++++++++++++ 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/cmd/genxdr/main.go b/cmd/genxdr/main.go index ddcace9..079159d 100644 --- a/cmd/genxdr/main.go +++ b/cmd/genxdr/main.go @@ -165,7 +165,7 @@ func (o {{.Name}}) MarshalXDRInto(m *xdr.Marshaller) error { } {{end}} -// Unmarshal parses the XDR-encoded data and stores the result in the +// UnmarshalXDR parses the XDR-encoded data and stores the result in the // struct. func (o *{{.Name}}) UnmarshalXDR(bs []byte) error { u := &xdr.Unmarshaller{Data: bs} diff --git a/common.go b/common.go index bef0a24..47c3010 100644 --- a/common.go +++ b/common.go @@ -11,7 +11,7 @@ import ( var padBytes = []byte{0, 0, 0} -// Pad returns the number of bytes that should be added to an item of length l +// Padding returns the number of bytes that should be added to an item of length l // bytes to conform to the XDR padding standard. This function is used by the // generated marshalling code. func Padding(l int) int { @@ -28,12 +28,13 @@ func ElementSizeExceeded(field string, size, limit int) error { return fmt.Errorf("%s exceeds size limit; %d > %d", field, size, limit) } -type XDRSizer interface { +// Sizer is a value that can return its XDR serialized size. +type Sizer interface { XDRSize() int } // SizeOfSlice returns the XDR encoded size of the given []T. Supported types -// for T are string, []byte and types implementing XDRSizer. SizeOfSlice +// for T are string, []byte and types implementing Sizer. SizeOfSlice // panics if the parameter is not a slice or if T is not one of the supported // types. This function is used by the generated marshalling code. func SizeOfSlice(ss interface{}) int { @@ -52,7 +53,7 @@ func SizeOfSlice(ss interface{}) int { default: v := reflect.ValueOf(ss) for i := 0; i < v.Len(); i++ { - l += v.Index(i).Interface().(XDRSizer).XDRSize() + l += v.Index(i).Interface().(Sizer).XDRSize() } } diff --git a/marshaller.go b/marshaller.go index 643290c..922c5fd 100644 --- a/marshaller.go +++ b/marshaller.go @@ -6,7 +6,7 @@ package xdr import "io" -// The Marshaller is a thin wrapper around a byte buffer. The buffer must be +// Marshaller is a thin wrapper around a byte buffer. The buffer must be // of sufficient size to hold the complete marshalled object, or an // io.ErrShortBuffer error will result. The Marshal... methods don't // individually return an error - the intention is that multiple fields are @@ -50,7 +50,7 @@ func (m *Marshaller) MarshalString(s string) { m.offset += copy(m.Data[m.offset:], padBytes[:Padding(len(s))]) } -// MarshalString appends the bytes to the buffer, with a size prefix and +// MarshalBytes appends the bytes to the buffer, with a size prefix and // correct padding. func (m *Marshaller) MarshalBytes(bs []byte) { if m.Error != nil { @@ -66,7 +66,7 @@ func (m *Marshaller) MarshalBytes(bs []byte) { m.offset += copy(m.Data[m.offset:], padBytes[:Padding(len(bs))]) } -// MarshalString appends the bool to the buffer, as an uint32. +// MarshalBool appends the bool to the buffer, as an uint32. func (m *Marshaller) MarshalBool(v bool) { if v { m.MarshalUint8(1) @@ -75,17 +75,17 @@ func (m *Marshaller) MarshalBool(v bool) { } } -// MarshalString appends the uint8 to the buffer, as an uint32. +// MarshalUint8 appends the uint8 to the buffer, as an uint32. func (m *Marshaller) MarshalUint8(v uint8) { m.MarshalUint32(uint32(v)) } -// MarshalString appends the uint16 to the buffer, as an uint32. +// MarshalUint16 appends the uint16 to the buffer, as an uint32. func (m *Marshaller) MarshalUint16(v uint16) { m.MarshalUint32(uint32(v)) } -// MarshalString appends the uint32 to the buffer. +// MarshalUint32 appends the uint32 to the buffer. func (m *Marshaller) MarshalUint32(v uint32) { if m.Error != nil { return @@ -102,7 +102,7 @@ func (m *Marshaller) MarshalUint32(v uint32) { m.offset += 4 } -// MarshalString appends the uint64 to the buffer. +// MarshalUint64 appends the uint64 to the buffer. func (m *Marshaller) MarshalUint64(v uint64) { if m.Error != nil { return diff --git a/unmarshal.go b/unmarshal.go index 63abcde..840ae29 100644 --- a/unmarshal.go +++ b/unmarshal.go @@ -6,11 +6,18 @@ package xdr import "io" +// Unmarshaller is a thin wrapper around a byte buffer. The Unmarshal... methods +// don't individually return an error - the intention is that multiple fields are +// unmarshalled in rapid succession, followed by a check of the Error field on +// the Unmarshaller. type Unmarshaller struct { Error error Data []byte } +// UnmarshalRaw returns a byte slice of length l from the buffer, +// without a size prefix or padding. This is suitable for retrieving +// data already in XDR format. func (u *Unmarshaller) UnmarshalRaw(l int) []byte { if u.Error != nil { return nil @@ -26,10 +33,12 @@ func (u *Unmarshaller) UnmarshalRaw(l int) []byte { return v } +// UnmarshalString returns a string from the buffer. func (u *Unmarshaller) UnmarshalString() string { return u.UnmarshalStringMax(0) } +// UnmarshalStringMax returns a string up to a max length from the buffer. func (u *Unmarshaller) UnmarshalStringMax(max int) string { buf := u.UnmarshalBytesMax(max) if len(buf) == 0 || u.Error != nil { @@ -39,10 +48,12 @@ func (u *Unmarshaller) UnmarshalStringMax(max int) string { return string(buf) } +// UnmarshalBytes returns a byte slice from the buffer. func (u *Unmarshaller) UnmarshalBytes() []byte { return u.UnmarshalBytesMax(0) } +// UnmarshalBytesMax returns a byte slice up to a max length from the buffer. func (u *Unmarshaller) UnmarshalBytesMax(max int) []byte { if u.Error != nil { return nil @@ -73,10 +84,12 @@ func (u *Unmarshaller) UnmarshalBytesMax(max int) []byte { return v } +// UnmarshalBool returns a bool from the buffer. func (u *Unmarshaller) UnmarshalBool() bool { return u.UnmarshalUint8() != 0 } +// UnmarshalUint8 returns a uint8 from the buffer. func (u *Unmarshaller) UnmarshalUint8() uint8 { if u.Error != nil { return 0 @@ -92,6 +105,7 @@ func (u *Unmarshaller) UnmarshalUint8() uint8 { return v } +// UnmarshalUint16 returns a uint16 from the buffer. func (u *Unmarshaller) UnmarshalUint16() uint16 { if u.Error != nil { return 0 @@ -107,6 +121,7 @@ func (u *Unmarshaller) UnmarshalUint16() uint16 { return v } +// UnmarshalUint32 returns a uint32 from the buffer. func (u *Unmarshaller) UnmarshalUint32() uint32 { if u.Error != nil { return 0 @@ -122,6 +137,7 @@ func (u *Unmarshaller) UnmarshalUint32() uint32 { return v } +// UnmarshalUint64 returns a uint64 from the buffer. func (u *Unmarshaller) UnmarshalUint64() uint64 { if u.Error != nil { return 0