Skip to content

Commit 6b62e31

Browse files
committed
Fixed golint on base library.
1 parent 05b4403 commit 6b62e31

File tree

5 files changed

+79
-39
lines changed

5 files changed

+79
-39
lines changed

base.go

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,30 @@ type Layer interface {
3333
type Payload []byte
3434

3535
// LayerType returns LayerTypePayload
36-
func (p Payload) LayerType() LayerType { return LayerTypePayload }
37-
func (p Payload) LayerContents() []byte { return []byte(p) }
38-
func (p Payload) LayerPayload() []byte { return nil }
39-
func (p Payload) Payload() []byte { return []byte(p) }
40-
func (p Payload) String() string { return fmt.Sprintf("%d byte(s)", len(p)) }
41-
func (p Payload) GoString() string { return LongBytesGoString([]byte(p)) }
42-
func (p Payload) CanDecode() LayerClass { return LayerTypePayload }
36+
func (p Payload) LayerType() LayerType { return LayerTypePayload }
37+
38+
// LayerContents returns the bytes making up this layer.
39+
func (p Payload) LayerContents() []byte { return []byte(p) }
40+
41+
// LayerPayload returns the payload within this layer.
42+
func (p Payload) LayerPayload() []byte { return nil }
43+
44+
// Payload returns this layer as bytes.
45+
func (p Payload) Payload() []byte { return []byte(p) }
46+
47+
// String implements fmt.Stringer.
48+
func (p Payload) String() string { return fmt.Sprintf("%d byte(s)", len(p)) }
49+
50+
// GoString implements fmt.GoStringer.
51+
func (p Payload) GoString() string { return LongBytesGoString([]byte(p)) }
52+
53+
// CanDecode implements DecodingLayer.
54+
func (p Payload) CanDecode() LayerClass { return LayerTypePayload }
55+
56+
// NextLayerType implements DecodingLayer.
4357
func (p Payload) NextLayerType() LayerType { return LayerTypeZero }
58+
59+
// DecodeFromBytes implements DecodingLayer.
4460
func (p *Payload) DecodeFromBytes(data []byte, df DecodeFeedback) error {
4561
*p = Payload(data)
4662
return nil
@@ -74,13 +90,27 @@ func decodePayload(data []byte, p PacketBuilder) error {
7490
type Fragment []byte
7591

7692
// LayerType returns LayerTypeFragment
77-
func (p *Fragment) LayerType() LayerType { return LayerTypeFragment }
78-
func (p *Fragment) LayerContents() []byte { return []byte(*p) }
79-
func (p *Fragment) LayerPayload() []byte { return nil }
80-
func (p *Fragment) Payload() []byte { return []byte(*p) }
81-
func (p *Fragment) String() string { return fmt.Sprintf("%d byte(s)", len(*p)) }
82-
func (p *Fragment) CanDecode() LayerClass { return LayerTypeFragment }
93+
func (p *Fragment) LayerType() LayerType { return LayerTypeFragment }
94+
95+
// LayerContents implements Layer.
96+
func (p *Fragment) LayerContents() []byte { return []byte(*p) }
97+
98+
// LayerPayload implements Layer.
99+
func (p *Fragment) LayerPayload() []byte { return nil }
100+
101+
// Payload returns this layer as a byte slice.
102+
func (p *Fragment) Payload() []byte { return []byte(*p) }
103+
104+
// String implements fmt.Stringer.
105+
func (p *Fragment) String() string { return fmt.Sprintf("%d byte(s)", len(*p)) }
106+
107+
// CanDecode implements DecodingLayer.
108+
func (p *Fragment) CanDecode() LayerClass { return LayerTypeFragment }
109+
110+
// NextLayerType implements DecodingLayer.
83111
func (p *Fragment) NextLayerType() LayerType { return LayerTypeZero }
112+
113+
// DecodeFromBytes implements DecodingLayer.
84114
func (p *Fragment) DecodeFromBytes(data []byte, df DecodeFeedback) error {
85115
*p = Fragment(data)
86116
return nil

decode.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ type Decoder interface {
7979
// DecodeFunc wraps a function to make it a Decoder.
8080
type DecodeFunc func([]byte, PacketBuilder) error
8181

82+
// Decode implements Decoder by calling itself.
8283
func (d DecodeFunc) Decode(data []byte, p PacketBuilder) error {
8384
// function, call thyself.
8485
return d(data, p)
@@ -99,18 +100,18 @@ var DecodeFragment Decoder = DecodeFunc(decodeFragment)
99100

100101
// LayerTypeZero is an invalid layer type, but can be used to determine whether
101102
// layer type has actually been set correctly.
102-
var LayerTypeZero LayerType = RegisterLayerType(0, LayerTypeMetadata{"Unknown", DecodeUnknown})
103+
var LayerTypeZero = RegisterLayerType(0, LayerTypeMetadata{Name: "Unknown", Decoder: DecodeUnknown})
103104

104105
// LayerTypeDecodeFailure is the layer type for the default error layer.
105-
var LayerTypeDecodeFailure LayerType = RegisterLayerType(1, LayerTypeMetadata{"DecodeFailure", DecodeUnknown})
106+
var LayerTypeDecodeFailure = RegisterLayerType(1, LayerTypeMetadata{Name: "DecodeFailure", Decoder: DecodeUnknown})
106107

107108
// LayerTypePayload is the layer type for a payload that we don't try to decode
108109
// but treat as a success, IE: an application-level payload.
109-
var LayerTypePayload LayerType = RegisterLayerType(2, LayerTypeMetadata{"Payload", DecodePayload})
110+
var LayerTypePayload = RegisterLayerType(2, LayerTypeMetadata{Name: "Payload", Decoder: DecodePayload})
110111

111112
// LayerTypeFragment is the layer type for a fragment of a layer transported
112113
// by an underlying layer that supports fragmentation.
113-
var LayerTypeFragment LayerType = RegisterLayerType(3, LayerTypeMetadata{"Fragment", DecodeFragment})
114+
var LayerTypeFragment = RegisterLayerType(3, LayerTypeMetadata{Name: "Fragment", Decoder: DecodeFragment})
114115

115116
// DecodeFailure is a packet layer created if decoding of the packet data failed
116117
// for some reason. It implements ErrorLayer. LayerContents will be the entire
@@ -123,12 +124,20 @@ type DecodeFailure struct {
123124
}
124125

125126
// Error returns the error encountered during decoding.
126-
func (d *DecodeFailure) Error() error { return d.err }
127+
func (d *DecodeFailure) Error() error { return d.err }
128+
129+
// LayerContents implements Layer.
127130
func (d *DecodeFailure) LayerContents() []byte { return d.data }
128-
func (d *DecodeFailure) LayerPayload() []byte { return nil }
131+
132+
// LayerPayload implements Layer.
133+
func (d *DecodeFailure) LayerPayload() []byte { return nil }
134+
135+
// String implements fmt.Stringer.
129136
func (d *DecodeFailure) String() string {
130137
return "Packet decoding error: " + d.Error().Error()
131138
}
139+
140+
// Dump implements Dumper.
132141
func (d *DecodeFailure) Dump() (s string) {
133142
if d.stack != nil {
134143
s = string(d.stack)

flows.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ type Endpoint struct {
3636
}
3737

3838
// EndpointType returns the endpoint type associated with this endpoint.
39-
func (e Endpoint) EndpointType() EndpointType { return e.typ }
39+
func (a Endpoint) EndpointType() EndpointType { return a.typ }
4040

4141
// Raw returns the raw bytes of this endpoint. These aren't human-readable
4242
// most of the time, but they are faster than calling String.
43-
func (e Endpoint) Raw() []byte { return e.raw[:e.len] }
43+
func (a Endpoint) Raw() []byte { return a.raw[:a.len] }
4444

4545
// LessThan provides a stable ordering for all endpoints. It sorts first based
4646
// on the EndpointType of an endpoint, then based on the raw bytes of that
@@ -130,11 +130,11 @@ func (e EndpointType) String() string {
130130
return strconv.Itoa(int(e))
131131
}
132132

133-
func (e Endpoint) String() string {
134-
if t, ok := endpointTypes[e.typ]; ok && t.Formatter != nil {
135-
return t.Formatter(e.raw[:e.len])
133+
func (a Endpoint) String() string {
134+
if t, ok := endpointTypes[a.typ]; ok && t.Formatter != nil {
135+
return t.Formatter(a.raw[:a.len])
136136
}
137-
return fmt.Sprintf("%v:%v", e.typ, e.raw)
137+
return fmt.Sprintf("%v:%v", a.typ, a.raw)
138138
}
139139

140140
// Flow represents the direction of traffic for a packet layer, as a source and destination Endpoint.
@@ -164,11 +164,11 @@ func FlowFromEndpoints(src, dst Endpoint) (_ Flow, err error) {
164164
//
165165
// The output of FastHash is not guaranteed to remain the same through future
166166
// code revisions, so should not be used to key values in persistent storage.
167-
func (a Flow) FastHash() (h uint64) {
167+
func (f Flow) FastHash() (h uint64) {
168168
// This combination must be commutative. We don't use ^, since that would
169169
// give the same hash for all A->A flows.
170-
h = fnvHash(a.src[:a.slen]) + fnvHash(a.dst[:a.dlen])
171-
h ^= uint64(a.typ)
170+
h = fnvHash(f.src[:f.slen]) + fnvHash(f.dst[:f.dlen])
171+
h ^= uint64(f.typ)
172172
h *= fnvPrime
173173
return
174174
}
@@ -225,12 +225,12 @@ func NewFlow(t EndpointType, src, dst []byte) (f Flow) {
225225

226226
// EndpointInvalid is an endpoint type used for invalid endpoints, IE endpoints
227227
// that are specified incorrectly during creation.
228-
var EndpointInvalid EndpointType = RegisterEndpointType(0, EndpointTypeMetadata{"invalid", func(b []byte) string {
228+
var EndpointInvalid = RegisterEndpointType(0, EndpointTypeMetadata{Name: "invalid", Formatter: func(b []byte) string {
229229
return fmt.Sprintf("%v", b)
230230
}})
231231

232232
// InvalidEndpoint is a singleton Endpoint of type EndpointInvalid.
233-
var InvalidEndpoint Endpoint = NewEndpoint(EndpointInvalid, nil)
233+
var InvalidEndpoint = NewEndpoint(EndpointInvalid, nil)
234234

235235
// InvalidFlow is a singleton Flow of type EndpointInvalid.
236-
var InvalidFlow Flow = NewFlow(EndpointInvalid, nil, nil)
236+
var InvalidFlow = NewFlow(EndpointInvalid, nil, nil)

layerclass.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ type LayerClass interface {
1818
LayerTypes() []LayerType
1919
}
2020

21-
// Make LayerType itself be a LayerClass.
21+
// Contains implements LayerClass.
2222
func (l LayerType) Contains(a LayerType) bool {
2323
return l == a
2424
}
2525

26+
// LayerTypes implements LayerClass.
2627
func (l LayerType) LayerTypes() []LayerType {
2728
return []LayerType{l}
2829
}

packet.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ func layerGoString(i interface{}, b *bytes.Buffer) {
359359
t := v.Type()
360360
b.WriteString(t.String())
361361
b.WriteByte('{')
362-
for i := 0; i < v.NumField(); i += 1 {
362+
for i := 0; i < v.NumField(); i++ {
363363
if i > 0 {
364364
b.WriteString(", ")
365365
}
@@ -424,11 +424,11 @@ type eagerPacket struct {
424424
packet
425425
}
426426

427-
var nilDecoderError = errors.New("NextDecoder passed nil decoder, probably an unsupported decode type")
427+
var errNilDecoder = errors.New("NextDecoder passed nil decoder, probably an unsupported decode type")
428428

429429
func (p *eagerPacket) NextDecoder(next Decoder) error {
430430
if next == nil {
431-
return nilDecoderError
431+
return errNilDecoder
432432
}
433433
if p.last == nil {
434434
return errors.New("NextDecoder called, but no layers added yet")
@@ -495,7 +495,7 @@ type lazyPacket struct {
495495

496496
func (p *lazyPacket) NextDecoder(next Decoder) error {
497497
if next == nil {
498-
return nilDecoderError
498+
return errNilDecoder
499499
}
500500
p.next = next
501501
return nil
@@ -624,13 +624,13 @@ type DecodeOptions struct {
624624
// though, so beware. If you can guarantee that the packet will only be used
625625
// by one goroutine at a time, set Lazy decoding. If you can guarantee that
626626
// the underlying slice won't change, set NoCopy decoding.
627-
var Default DecodeOptions = DecodeOptions{}
627+
var Default = DecodeOptions{}
628628

629629
// Lazy is a DecodeOptions with just Lazy set.
630-
var Lazy DecodeOptions = DecodeOptions{Lazy: true}
630+
var Lazy = DecodeOptions{Lazy: true}
631631

632632
// NoCopy is a DecodeOptions with just NoCopy set.
633-
var NoCopy DecodeOptions = DecodeOptions{NoCopy: true}
633+
var NoCopy = DecodeOptions{NoCopy: true}
634634

635635
// NewPacket creates a new Packet object from a set of bytes. The
636636
// firstLayerDecoder tells it how to interpret the first layer from the bytes,

0 commit comments

Comments
 (0)