Skip to content

Commit

Permalink
Fixed linting warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
darccio committed Aug 25, 2018
1 parent 8bf7e85 commit 5e342c0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/genxdr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
9 changes: 5 additions & 4 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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()
}
}

Expand Down
14 changes: 7 additions & 7 deletions marshaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 5e342c0

Please sign in to comment.