Skip to content

Commit

Permalink
reorg. pkg. struct., move files to internal
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-ling committed Feb 10, 2022
1 parent 1ae6f55 commit 3ff0cc3
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 44 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,10 @@ $ go test -cpuprofile cpu.prof -memprofile mem.prof -bench . -benchmem
```
```
goos: linux
goarch: amd64
goarch: arm64
pkg: github.com/encodingx/binary
cpu: Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
BenchmarkMarshal 2145660 552.5 ns/op 64 B/op 6 allocs/op
BenchmarkUnmarshal 1878840 655.3 ns/op 64 B/op 8 allocs/op
BenchmarkMarshal-2 3435181 349.9 ns/op 64 B/op 6 allocs/op
BenchmarkUnmarshal-2 3004425 396.4 ns/op 64 B/op 8 allocs/op
PASS
ok github.com/encodingx/binary 3.873s
ok github.com/encodingx/binary 3.288s
```
19 changes: 8 additions & 11 deletions binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ import (
"fmt"
"io"

"github.com/encodingx/binary/internal/codecs"
"github.com/encodingx/binary/internal/validation"
)

const (
wordLengthUpperLimitBytes = 8
)

var (
defaultCodec = newCodec()
defaultCodec codecs.Codec = codecs.NewCodec()
)

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

var (
operation codecOperation
operation codecs.CodecOperation
)

defer func() {
Expand All @@ -39,12 +36,12 @@ func Marshal(iface interface{}) (bytes []byte, e error) {
return
}()

operation, e = defaultCodec.newOperation(iface)
operation, e = defaultCodec.NewOperation(iface)
if e != nil {
return
}

bytes, e = operation.marshal()
bytes, e = operation.Marshal()
if e != nil {
return
}
Expand All @@ -58,7 +55,7 @@ func Unmarshal(bytes []byte, iface interface{}) (e error) {
)

var (
operation codecOperation
operation codecs.CodecOperation
)

defer func() {
Expand All @@ -75,12 +72,12 @@ func Unmarshal(bytes []byte, iface interface{}) (e error) {
return
}()

operation, e = defaultCodec.newOperation(iface)
operation, e = defaultCodec.NewOperation(iface)
if e != nil {
return
}

e = operation.unmarshal(bytes)
e = operation.Unmarshal(bytes)
if e != nil {
return
}
Expand Down
39 changes: 20 additions & 19 deletions codec.go → internal/codecs/codec.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package binary
package codecs

import (
"reflect"

"github.com/encodingx/binary/internal/codecs/metadata"
"github.com/encodingx/binary/internal/validation"
)

type codec struct {
formatMetadataCache map[reflect.Type]formatMetadata
type Codec struct {
formatMetadataCache map[reflect.Type]metadata.FormatMetadata
}

func newCodec() (c codec) {
c = codec{
formatMetadataCache: make(map[reflect.Type]formatMetadata),
func NewCodec() (c Codec) {
c = Codec{
formatMetadataCache: make(map[reflect.Type]metadata.FormatMetadata),
}

return
}

func (c codec) formatMetadataFromTypeReflection(reflection reflect.Type) (
format formatMetadata, e error,
func (c Codec) formatMetadataFromTypeReflection(reflection reflect.Type) (
format metadata.FormatMetadata, e error,
) {
var (
inCache bool
Expand All @@ -43,7 +44,7 @@ func (c codec) formatMetadataFromTypeReflection(reflection reflect.Type) (
return
}

format, e = newFormatMetadataFromTypeReflection(
format, e = metadata.NewFormatMetadataFromTypeReflection(
reflection.Elem(),
)
if e != nil {
Expand All @@ -55,8 +56,8 @@ func (c codec) formatMetadataFromTypeReflection(reflection reflect.Type) (
return
}

func (c codec) newOperation(iface interface{}) (
operation codecOperation, e error,
func (c Codec) NewOperation(iface interface{}) (
operation CodecOperation, e error,
) {
operation.format, e = c.formatMetadataFromTypeReflection(
reflect.TypeOf(iface),
Expand All @@ -70,21 +71,21 @@ func (c codec) newOperation(iface interface{}) (
return
}

type codecOperation struct {
format formatMetadata
type CodecOperation struct {
format metadata.FormatMetadata
valueReflection reflect.Value
}

func (c codecOperation) marshal() (bytes []byte, e error) {
bytes = c.format.marshal(c.valueReflection)
func (c CodecOperation) Marshal() (bytes []byte, e error) {
bytes = c.format.Marshal(c.valueReflection)

return
}

func (c codecOperation) unmarshal(bytes []byte) (e error) {
if len(bytes) != c.format.lengthInBytes {
func (c CodecOperation) Unmarshal(bytes []byte) (e error) {
if len(bytes) != c.format.LengthInBytes() {
e = validation.NewLengthOfByteSliceNotEqualToFormatLengthError(
uint(c.format.lengthInBytes),
uint(c.format.LengthInBytes()),
uint(len(bytes)),
)

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

c.format.unmarshal(bytes, c.valueReflection)
c.format.Unmarshal(bytes, c.valueReflection)

return
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package binary
package metadata

import (
"encoding/binary"
Expand Down
5 changes: 5 additions & 0 deletions internal/codecs/metadata/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package metadata

const (
wordLengthUpperLimitBytes = 8
)
18 changes: 11 additions & 7 deletions metadata-format.go → internal/codecs/metadata/format.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package binary
package metadata

import (
"reflect"

"github.com/encodingx/binary/internal/validation"
)

type formatMetadata struct {
type FormatMetadata struct {
words []wordMetadata
lengthInBytes int
}

func newFormatMetadataFromTypeReflection(reflection reflect.Type) (
format formatMetadata, e error,
func NewFormatMetadataFromTypeReflection(reflection reflect.Type) (
format FormatMetadata, e error,
) {
var (
i int
Expand All @@ -32,7 +32,7 @@ func newFormatMetadataFromTypeReflection(reflection reflect.Type) (
return
}

format = formatMetadata{
format = FormatMetadata{
words: make([]wordMetadata,
reflection.NumField(),
),
Expand All @@ -52,7 +52,7 @@ func newFormatMetadataFromTypeReflection(reflection reflect.Type) (
return
}

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

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

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

return
}

func (m FormatMetadata) LengthInBytes() int {
return m.lengthInBytes
}
2 changes: 1 addition & 1 deletion metadata-word.go → internal/codecs/metadata/word.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package binary
package metadata

import (
"encoding/binary"
Expand Down

0 comments on commit 3ff0cc3

Please sign in to comment.