Skip to content

Commit 3ff0cc3

Browse files
committed
reorg. pkg. struct., move files to internal
1 parent 1ae6f55 commit 3ff0cc3

File tree

7 files changed

+50
-44
lines changed

7 files changed

+50
-44
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,10 @@ $ go test -cpuprofile cpu.prof -memprofile mem.prof -bench . -benchmem
234234
```
235235
```
236236
goos: linux
237-
goarch: amd64
237+
goarch: arm64
238238
pkg: github.com/encodingx/binary
239-
cpu: Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
240-
BenchmarkMarshal 2145660 552.5 ns/op 64 B/op 6 allocs/op
241-
BenchmarkUnmarshal 1878840 655.3 ns/op 64 B/op 8 allocs/op
239+
BenchmarkMarshal-2 3435181 349.9 ns/op 64 B/op 6 allocs/op
240+
BenchmarkUnmarshal-2 3004425 396.4 ns/op 64 B/op 8 allocs/op
242241
PASS
243-
ok github.com/encodingx/binary 3.873s
242+
ok github.com/encodingx/binary 3.288s
244243
```

binary.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ import (
55
"fmt"
66
"io"
77

8+
"github.com/encodingx/binary/internal/codecs"
89
"github.com/encodingx/binary/internal/validation"
910
)
1011

11-
const (
12-
wordLengthUpperLimitBytes = 8
13-
)
14-
1512
var (
16-
defaultCodec = newCodec()
13+
defaultCodec codecs.Codec = codecs.NewCodec()
1714
)
1815

1916
func Marshal(iface interface{}) (bytes []byte, e error) {
@@ -22,7 +19,7 @@ func Marshal(iface interface{}) (bytes []byte, e error) {
2219
)
2320

2421
var (
25-
operation codecOperation
22+
operation codecs.CodecOperation
2623
)
2724

2825
defer func() {
@@ -39,12 +36,12 @@ func Marshal(iface interface{}) (bytes []byte, e error) {
3936
return
4037
}()
4138

42-
operation, e = defaultCodec.newOperation(iface)
39+
operation, e = defaultCodec.NewOperation(iface)
4340
if e != nil {
4441
return
4542
}
4643

47-
bytes, e = operation.marshal()
44+
bytes, e = operation.Marshal()
4845
if e != nil {
4946
return
5047
}
@@ -58,7 +55,7 @@ func Unmarshal(bytes []byte, iface interface{}) (e error) {
5855
)
5956

6057
var (
61-
operation codecOperation
58+
operation codecs.CodecOperation
6259
)
6360

6461
defer func() {
@@ -75,12 +72,12 @@ func Unmarshal(bytes []byte, iface interface{}) (e error) {
7572
return
7673
}()
7774

78-
operation, e = defaultCodec.newOperation(iface)
75+
operation, e = defaultCodec.NewOperation(iface)
7976
if e != nil {
8077
return
8178
}
8279

83-
e = operation.unmarshal(bytes)
80+
e = operation.Unmarshal(bytes)
8481
if e != nil {
8582
return
8683
}

codec.go renamed to internal/codecs/codec.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
package binary
1+
package codecs
22

33
import (
44
"reflect"
55

6+
"github.com/encodingx/binary/internal/codecs/metadata"
67
"github.com/encodingx/binary/internal/validation"
78
)
89

9-
type codec struct {
10-
formatMetadataCache map[reflect.Type]formatMetadata
10+
type Codec struct {
11+
formatMetadataCache map[reflect.Type]metadata.FormatMetadata
1112
}
1213

13-
func newCodec() (c codec) {
14-
c = codec{
15-
formatMetadataCache: make(map[reflect.Type]formatMetadata),
14+
func NewCodec() (c Codec) {
15+
c = Codec{
16+
formatMetadataCache: make(map[reflect.Type]metadata.FormatMetadata),
1617
}
1718

1819
return
1920
}
2021

21-
func (c codec) formatMetadataFromTypeReflection(reflection reflect.Type) (
22-
format formatMetadata, e error,
22+
func (c Codec) formatMetadataFromTypeReflection(reflection reflect.Type) (
23+
format metadata.FormatMetadata, e error,
2324
) {
2425
var (
2526
inCache bool
@@ -43,7 +44,7 @@ func (c codec) formatMetadataFromTypeReflection(reflection reflect.Type) (
4344
return
4445
}
4546

46-
format, e = newFormatMetadataFromTypeReflection(
47+
format, e = metadata.NewFormatMetadataFromTypeReflection(
4748
reflection.Elem(),
4849
)
4950
if e != nil {
@@ -55,8 +56,8 @@ func (c codec) formatMetadataFromTypeReflection(reflection reflect.Type) (
5556
return
5657
}
5758

58-
func (c codec) newOperation(iface interface{}) (
59-
operation codecOperation, e error,
59+
func (c Codec) NewOperation(iface interface{}) (
60+
operation CodecOperation, e error,
6061
) {
6162
operation.format, e = c.formatMetadataFromTypeReflection(
6263
reflect.TypeOf(iface),
@@ -70,21 +71,21 @@ func (c codec) newOperation(iface interface{}) (
7071
return
7172
}
7273

73-
type codecOperation struct {
74-
format formatMetadata
74+
type CodecOperation struct {
75+
format metadata.FormatMetadata
7576
valueReflection reflect.Value
7677
}
7778

78-
func (c codecOperation) marshal() (bytes []byte, e error) {
79-
bytes = c.format.marshal(c.valueReflection)
79+
func (c CodecOperation) Marshal() (bytes []byte, e error) {
80+
bytes = c.format.Marshal(c.valueReflection)
8081

8182
return
8283
}
8384

84-
func (c codecOperation) unmarshal(bytes []byte) (e error) {
85-
if len(bytes) != c.format.lengthInBytes {
85+
func (c CodecOperation) Unmarshal(bytes []byte) (e error) {
86+
if len(bytes) != c.format.LengthInBytes() {
8687
e = validation.NewLengthOfByteSliceNotEqualToFormatLengthError(
87-
uint(c.format.lengthInBytes),
88+
uint(c.format.LengthInBytes()),
8889
uint(len(bytes)),
8990
)
9091

@@ -95,7 +96,7 @@ func (c codecOperation) unmarshal(bytes []byte) (e error) {
9596
return
9697
}
9798

98-
c.format.unmarshal(bytes, c.valueReflection)
99+
c.format.Unmarshal(bytes, c.valueReflection)
99100

100101
return
101102
}

metadata-bit-field.go renamed to internal/codecs/metadata/bit-field.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package binary
1+
package metadata
22

33
import (
44
"encoding/binary"

internal/codecs/metadata/constants.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package metadata
2+
3+
const (
4+
wordLengthUpperLimitBytes = 8
5+
)

metadata-format.go renamed to internal/codecs/metadata/format.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
package binary
1+
package metadata
22

33
import (
44
"reflect"
55

66
"github.com/encodingx/binary/internal/validation"
77
)
88

9-
type formatMetadata struct {
9+
type FormatMetadata struct {
1010
words []wordMetadata
1111
lengthInBytes int
1212
}
1313

14-
func newFormatMetadataFromTypeReflection(reflection reflect.Type) (
15-
format formatMetadata, e error,
14+
func NewFormatMetadataFromTypeReflection(reflection reflect.Type) (
15+
format FormatMetadata, e error,
1616
) {
1717
var (
1818
i int
@@ -32,7 +32,7 @@ func newFormatMetadataFromTypeReflection(reflection reflect.Type) (
3232
return
3333
}
3434

35-
format = formatMetadata{
35+
format = FormatMetadata{
3636
words: make([]wordMetadata,
3737
reflection.NumField(),
3838
),
@@ -52,7 +52,7 @@ func newFormatMetadataFromTypeReflection(reflection reflect.Type) (
5252
return
5353
}
5454

55-
func (m formatMetadata) marshal(reflection reflect.Value) (bytes []byte) {
55+
func (m FormatMetadata) Marshal(reflection reflect.Value) (bytes []byte) {
5656
// Merge byte slices marshalled from words,
5757
// in the order they appear in the format.
5858

@@ -78,7 +78,7 @@ func (m formatMetadata) marshal(reflection reflect.Value) (bytes []byte) {
7878
return
7979
}
8080

81-
func (m formatMetadata) unmarshal(bytes []byte, reflection reflect.Value) {
81+
func (m FormatMetadata) Unmarshal(bytes []byte, reflection reflect.Value) {
8282
var (
8383
i int
8484
j int
@@ -105,3 +105,7 @@ func (m formatMetadata) unmarshal(bytes []byte, reflection reflect.Value) {
105105

106106
return
107107
}
108+
109+
func (m FormatMetadata) LengthInBytes() int {
110+
return m.lengthInBytes
111+
}

metadata-word.go renamed to internal/codecs/metadata/word.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package binary
1+
package metadata
22

33
import (
44
"encoding/binary"

0 commit comments

Comments
 (0)