Skip to content

Commit 29b974f

Browse files
authored
fix gometalinter reports (#8)
* update version to 1.2.0 add change log and todo list fix gometalinter reports
1 parent aaffe64 commit 29b974f

14 files changed

+589
-521
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ go:
44
before_install:
55
- go get github.com/mattn/goveralls
66
- go get golang.org/x/tools/cmd/cover
7+
#- go get github.com/alecthomas/gometalinter
8+
#- $HOME/gopath/bin/gometalinter --install --update
79
- go get github.com/vipally/binary
810
script:
911
- go vet
1012
- go test
1113
- $HOME/gopath/bin/goveralls -repotoken X9dt3MTIoRZ1nTCVh5ueEJeGrPFDQr23z
14+
#- $HOME/gopath/bin/gometalinter ./...

README.md

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# binary
22

3-
[![Build Status](https://travis-ci.org/vipally/binary.svg?branch=master)](https://travis-ci.org/vipally/binary) [![Coverage Status](https://coveralls.io/repos/github/vipally/binary/badge.svg?branch=master)](https://coveralls.io/github/vipally/binary?branch=master) [![GoDoc](https://godoc.org/github.com/vipally/binary?status.svg)](https://godoc.org/github.com/vipally/binary) ![Version](https://img.shields.io/badge/version-1.2.0-green.svg)
3+
![Version][version-img] [![Build status][travis-img]][travis-url] [![Coverage Status][coverage-img]][coverage-url] [![Go Report Card][report-img]][report-url] [![GoDoc][doc-img]][doc-url] [![License][license-img]][license-url]
44

55
***
66

@@ -17,16 +17,18 @@
1717

1818
***
1919

20-
CopyRight 2017 @Ally Dale. All rights reserved.
21-
22-
Author : [Ally Dale(vipally@gmail.com)](mailto://vipally@gmail.com)
20+
## Install
2321

24-
Blog : [http://blog.csdn.net/vipally](http://blog.csdn.net/vipally)
22+
```bash
23+
$ go get -u github.com/vipally/binary
24+
```
25+
import(
26+
"github.com/vipally/binary"
27+
)
2528

26-
Site : [https://github.com/vipally](https://github.com/vipally)
29+
***
2730

28-
****
29-
# change log:
31+
# Change log:
3032
## v1.2.0
3133
1.use field tag `binary:"packed"` to encode ints value as varint/uvarint
3234
for reged structs.
@@ -45,10 +47,23 @@ Site : [https://github.com/vipally](https://github.com/vipally)
4547
6.take both advantages of std.binary and gob.
4648
7.recommended using in net protocol serialization and DB serialization.
4749

48-
# TODO:
50+
# Todo:
4951
1.[Encoder/Decoder].RegStruct to speed up local Coder.
5052
2.[Encoder/Decoder].RegSerializer to speed up BinarySerializer search.
5153
3.reg interface to using BinarySerializer interface.
54+
55+
***
56+
57+
## CopyRight
58+
59+
CopyRight 2017 @Ally Dale. All rights reserved.
60+
61+
Author : [Ally Dale(vipally@gmail.com)](mailto://vipally@gmail.com)
62+
63+
Blog : [http://blog.csdn.net/vipally](http://blog.csdn.net/vipally)
64+
65+
Site : [https://github.com/vipally](https://github.com/vipally)
66+
5267
****
5368

5469
# 1. Support all serialize-able basic types:
@@ -274,9 +289,22 @@ Site : [https://github.com/vipally](https://github.com/vipally)
274289

275290
# License.
276291
Under MIT license.
277-
292+
278293
Copyright (c) 2017 Ally Dale<vipally@gmail.com>
279294
Author : Ally Dale<vipally@gmail.com>
280295
Site : https://github.com/vipally
281296
Origin : https://github.com/vipally/binary
282297

298+
[travis-img]: https://travis-ci.org/vipally/binary.svg?branch=master
299+
[travis-url]: https://travis-ci.org/vipally/binary
300+
[coverage-img]: https://coveralls.io/repos/github/vipally/binary/badge.svg?branch=master
301+
[coverage-url]: https://coveralls.io/github/vipally/binary?branch=master
302+
[license-img]: http://img.shields.io/badge/license-MIT-green.svg?style=flat-square
303+
[license-url]: http://opensource.org/licenses/MIT
304+
[doc-img2]: http://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square
305+
[doc-img]: https://godoc.org/github.com/vipally/binary?status.svg
306+
[doc-url]: https://godoc.org/github.com/vipally/binary
307+
[report-img]: https://goreportcard.com/badge/github.com/vipally/binary
308+
[report-url]: https://goreportcard.com/report/github.com/vipally/binary
309+
[version-img]: https://img.shields.io/badge/version-1.2.0-green.svg
310+

bench_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,20 +168,20 @@ func BenchmarkUnackString(b *testing.B) {
168168
testBenchDecode(b, &data, &strW, "BenchmarkUnackString")
169169
}
170170

171-
func newSame(v reflect.Value) (value reflect.Value) {
172-
vv := reflect.Indirect(v)
173-
t := vv.Type()
174-
switch t.Kind() {
175-
case reflect.Bool, reflect.Int8, reflect.Uint8, reflect.Int16,
176-
reflect.Uint16, reflect.Int32, reflect.Uint32, reflect.Int64,
177-
reflect.Uint64, reflect.Float32, reflect.Float64, reflect.Complex64,
178-
reflect.Complex128, reflect.String, reflect.Array, reflect.Struct:
179-
value = reflect.New(t)
180-
case reflect.Slice:
181-
value = reflect.MakeSlice(t, 0, 0).Addr() //make a default slice
182-
}
183-
return
184-
}
171+
//func newSame(v reflect.Value) (value reflect.Value) {
172+
// vv := reflect.Indirect(v)
173+
// t := vv.Type()
174+
// switch t.Kind() {
175+
// case reflect.Bool, reflect.Int8, reflect.Uint8, reflect.Int16,
176+
// reflect.Uint16, reflect.Int32, reflect.Uint32, reflect.Int64,
177+
// reflect.Uint64, reflect.Float32, reflect.Float64, reflect.Complex64,
178+
// reflect.Complex128, reflect.String, reflect.Array, reflect.Struct:
179+
// value = reflect.New(t)
180+
// case reflect.Slice:
181+
// value = reflect.MakeSlice(t, 0, 0).Addr() //make a default slice
182+
// }
183+
// return
184+
//}
185185

186186
func testBenchGobEncode(b *testing.B, data interface{}, caseName string) {
187187
buffer.Reset()

binary_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,10 @@ func TestWriteSlice(t *testing.T) {
147147
}
148148

149149
func TestReadBool(t *testing.T) {
150-
var res bool
151-
var err error
150+
var (
151+
res bool
152+
err error
153+
)
152154
err = Read(bytes.NewReader([]byte{0}), BigEndian, &res)
153155
checkResult(t, "ReadBool", BigEndian, err, res, false)
154156
res = false
@@ -403,10 +405,10 @@ func testPutUint64SmallSliceLengthPanics() (panicked bool) {
403405
}
404406

405407
func TestEarlyBoundsChecks(t *testing.T) {
406-
if testUint64SmallSliceLengthPanics() != true {
408+
if !testUint64SmallSliceLengthPanics() {
407409
t.Errorf("binary.LittleEndian.Uint64 expected to panic for small slices, but didn't")
408410
}
409-
if testPutUint64SmallSliceLengthPanics() != true {
411+
if !testPutUint64SmallSliceLengthPanics() {
410412
t.Errorf("binary.LittleEndian.PutUint64 expected to panic for small slices, but didn't")
411413
}
412414
}

coder.go

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
)
88

99
var (
10+
// ErrNotEnoughSpace buffer not enough
1011
ErrNotEnoughSpace = errors.New("not enough space")
1112
)
1213

@@ -19,70 +20,76 @@ type coder struct {
1920
endian Endian
2021
}
2122

23+
func (cder *coder) setEndian(endian Endian) {
24+
cder.endian = endian
25+
}
26+
2227
// Buffer returns the byte slice that has been encoding/decoding.
23-
func (this *coder) Buffer() []byte {
24-
return this.buff[:this.pos]
28+
func (cder *coder) Buffer() []byte {
29+
return cder.buff[:cder.pos]
2530
}
2631

2732
// Len returns unmber of bytes that has been encoding/decoding.
28-
func (this *coder) Len() int {
29-
return this.pos
33+
func (cder *coder) Len() int {
34+
return cder.pos
3035
}
3136

32-
// Cap returns number total bytes of this coder buffer.
33-
func (this *coder) Cap() int {
34-
return len(this.buff)
37+
// Cap returns number total bytes of cder coder buffer.
38+
func (cder *coder) Cap() int {
39+
return len(cder.buff)
3540
}
3641

3742
// Skip ignore the next size of bytes for encoding/decoding and
3843
// set skiped bytes to 0.
3944
// It will panic if space not enough.
4045
// It will return -1 if size <= 0.
41-
func (this *coder) Skip(size int) int {
42-
newPos := this.pos + size
43-
if size >= 0 && newPos <= this.Cap() {
44-
for i, b := int(size-1), this.buff[this.pos:newPos]; i >= 0; i-- { //zero skiped bytes
46+
func (cder *coder) Skip(size int) int {
47+
newPos := cder.pos + size
48+
if size >= 0 && newPos <= cder.Cap() {
49+
for i, b := size-1, cder.buff[cder.pos:newPos]; i >= 0; i-- { //zero skiped bytes
4550
b[i] = 0
4651
}
47-
this.pos = newPos
52+
cder.pos = newPos
4853
return size
4954
}
5055
return -1
5156
}
5257

5358
// Reset move the read/write pointer to the beginning of buffer
5459
// and set all reseted bytes to 0.
55-
func (this *coder) Reset() {
56-
for i := int(this.pos - 1); i >= 0; i-- { //zero encoded bytes
57-
this.buff[i] = 0
60+
func (cder *coder) Reset() {
61+
for i := cder.pos - 1; i >= 0; i-- { //zero encoded bytes
62+
cder.buff[i] = 0
5863
}
59-
this.pos = 0
60-
this.resetBoolCoder()
64+
cder.pos = 0
65+
cder.resetBoolCoder()
6166
}
6267

6368
// reset the state of bool coder
64-
func (this *coder) resetBoolCoder() {
65-
this.boolPos = -1
66-
this.boolBit = 0
69+
func (cder *coder) resetBoolCoder() {
70+
cder.boolPos = -1
71+
cder.boolBit = 0
6772
}
6873

6974
// reserve returns next size bytes for encoding/decoding.
7075
// it will panic if not enough space.
71-
func (this *coder) reserve(size int) []byte {
72-
newPos := this.pos + size
73-
if newPos > this.Cap() {
74-
panic(fmt.Errorf("binary.Coder:buffer overflow pos=%d cap=%d require=%d, not enough space!", this.pos, this.Cap(), size))
76+
func (cder *coder) reserve(size int) []byte {
77+
newPos := cder.pos + size
78+
if newPos > cder.Cap() {
79+
panic(fmt.Errorf("binary.Coder:buffer overflow pos=%d cap=%d require=%d, not enough space", cder.pos, cder.Cap(), size))
7580
}
76-
if size > 0 && newPos <= this.Cap() {
77-
b := this.buff[this.pos:newPos]
78-
this.pos = newPos
81+
if size > 0 && newPos <= cder.Cap() {
82+
b := cder.buff[cder.pos:newPos]
83+
cder.pos = newPos
7984
return b
8085
}
8186
return nil
8287
}
8388

89+
// BytesReader transform bytes as Reader
8490
type BytesReader []byte
8591

92+
// Read from bytes
8693
func (p *BytesReader) Read(data []byte) (n int, err error) {
8794
n = copy(data, *p)
8895
if n == len(*p) {
@@ -92,8 +99,10 @@ func (p *BytesReader) Read(data []byte) (n int, err error) {
9299
return
93100
}
94101

102+
// BytesWriter transform bytes as Writer
95103
type BytesWriter []byte
96104

105+
// Write to bytes
97106
func (p *BytesWriter) Write(data []byte) (n int, err error) {
98107
n = copy(*p, data)
99108
if n < len(data) {

coder_test.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,33 @@ func TestEncode(t *testing.T) {
361361
//}
362362
}
363363

364+
func TestEncodeBig(t *testing.T) {
365+
v := reflect.ValueOf(full)
366+
vt := v.Type()
367+
n := v.NumField()
368+
check := bigFull
369+
for i := 0; i < n; i++ {
370+
if !validField(vt.Field(i)) {
371+
continue
372+
}
373+
size := Sizeof(v.Field(i).Interface())
374+
encoder := NewEncoderEndian(size, BigEndian)
375+
err := encoder.Value(v.Field(i).Interface())
376+
b := encoder.Buffer()
377+
c := check[:len(b)]
378+
check = check[len(b):]
379+
if err != nil {
380+
t.Error(err)
381+
}
382+
//fmt.Printf("//field#%d|%s|%#v\n$% x,\n", i+1, vt.Field(i).Name, v.Field(i).Interface(), b)
383+
if vt.Field(i).Type.Kind() != reflect.Map && //map keys will be got as unspecified order, byte order may change but it doesn't matter
384+
!reflect.DeepEqual(b, c) {
385+
//fmt.Printf("%d %s\ngot%#v\n%need#v\n", i, vt.Field(i).Type.String(), b, c)
386+
t.Errorf("field %d %s got %+v\nneed %+v\n", i, vt.Field(i).Name, b, c)
387+
}
388+
}
389+
}
390+
364391
func TestDecode(t *testing.T) {
365392
var v fullStruct
366393
err := Decode(littleFullAll, &v)
@@ -819,17 +846,17 @@ func TestDecodeUvarintOverflow(t *testing.T) {
819846

820847
type sizerOnly struct{ A uint8 }
821848

822-
func (this sizerOnly) Size() int { return 1 }
849+
func (obj sizerOnly) Size() int { return 1 }
823850

824851
type encoderOnly struct{ B uint8 }
825852

826-
func (this encoderOnly) Encode(buffer []byte) ([]byte, error) { return nil, nil }
853+
func (obj encoderOnly) Encode(buffer []byte) ([]byte, error) { return nil, nil }
827854

828855
type decoderOnly struct {
829856
C uint8
830857
}
831858

832-
func (this *decoderOnly) Decode(buffer []byte) error { return nil }
859+
func (obj *decoderOnly) Decode(buffer []byte) error { return nil }
833860

834861
type sizeencoderOnly struct {
835862
sizerOnly
@@ -852,7 +879,7 @@ type fullSerializerError struct {
852879
fullSerializer
853880
}
854881

855-
func (this *fullSerializerError) Decode(buffer []byte) error {
882+
func (obj *fullSerializerError) Decode(buffer []byte) error {
856883
return fmt.Errorf("expected error")
857884
}
858885

0 commit comments

Comments
 (0)