Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/fitprint/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ File Header:
`,
fileHeader.Size,
fmt.Sprintf("%d.%d",
proto.VersionMajor(fileHeader.ProtocolVersion),
proto.VersionMinor(fileHeader.ProtocolVersion)),
fileHeader.ProtocolVersion.Major(),
fileHeader.ProtocolVersion.Minor()),
fileHeader.ProtocolVersion,
fileHeader.ProfileVersion,
fileHeader.DataSize,
Expand Down
10 changes: 1 addition & 9 deletions decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,7 @@ type Option func(o *options)

// WithFactory sets custom factory.
func WithFactory(factory Factory) Option {
return func(o *options) {
if factory != nil {
o.factory = factory
}
}
return func(o *options) { o.factory = factory }
}

// WithMesgListener adds listeners to the listener pool, where each listener will receive
Expand Down Expand Up @@ -490,10 +486,6 @@ func (d *Decoder) decodeFileHeader() error {
DataType: proto.DataTypeFIT,
}

if err := proto.Validate(d.fileHeader.ProtocolVersion); err != nil {
return err
}

if d.fileHeader.DataSize == 0 {
return fmt.Errorf("invalid data size: %w", ErrNotFITFile)
}
Expand Down
20 changes: 0 additions & 20 deletions decoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1143,26 +1143,6 @@ func TestDecodeFileHeader(t *testing.T) {
}(),
err: io.EOF,
},
{
name: "decode invalid protocol",
r: func() io.Reader {
var buf, cur = append(buf[:0:0], buf...), 0
buf[1] = 100 // invalid protocol
return fnReader(func(b []byte) (n int, err error) {
m := len(buf)
if cur == m {
return 0, io.EOF
}
if cur+len(b) < m {
m = cur + len(b)
}
n = copy(b, buf[cur:m])
cur += n
return
})
}(),
err: proto.ErrProtocolVersionNotSupported,
},
{
name: "decode data type not `.FIT`",
r: func() io.Reader {
Expand Down
18 changes: 3 additions & 15 deletions encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,14 @@ type Option func(o *options)
// it will use proto.V1. This option overrides the FileHeader's ProtocolVersion and forces all FIT
// files to use this ProtocolVersion during encoding.
//
// NOTE: If the given protocolVersion is not supported, the Protocol Version will not be changed.
// Please validate using proto.Validate when putting user-defined Protocol Version to check
// whether it is supported or not. Or just use predefined Protocol Version constants such as
// proto.V1, proto.V2, etc, which the validity is ensured.
// Available options: proto.V1 and proto.V2
func WithProtocolVersion(protocolVersion proto.Version) Option {
return func(o *options) {
if proto.Validate(protocolVersion) == nil {
o.protocolVersion = protocolVersion
}
}
return func(o *options) { o.protocolVersion = protocolVersion }
}

// WithMessageValidator directs the Encoder to use this message validator instead of the default one.
func WithMessageValidator(validator MessageValidator) Option {
return func(o *options) {
if validator != nil {
o.messageValidator = validator
}
}
return func(o *options) { o.messageValidator = validator }
}

// WithBigEndian directs the Encoder to encode values in Big-Endian bytes order (default: Little-Endian).
Expand Down Expand Up @@ -678,7 +667,6 @@ func (e *Encoder) encodeMessagesWithContext(ctx context.Context, messages []prot
e.n, i, mesg.Num, mesg.Num.String(), err)
}
}

return nil
}

Expand Down
43 changes: 12 additions & 31 deletions proto/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,21 @@ package proto
type Version byte

const (
ErrProtocolVersionNotSupported = errorString("protocol version not supported")
vMajorShift = 4
vMinorMask = 1<<vMajorShift - 1

MajorVersionShift = 4
MajorVersionMask = 0x0F << MajorVersionShift
MinorVersionMask = 0x0F

V1 Version = 1 << MajorVersionShift // V1 is Version 1.0
V2 Version = 2 << MajorVersionShift // V2 is Version 2.0
Vmax = V2 // Vmax is an alias for the current latest version.
V1 Version = 1 << vMajorShift // V1 is Version 1.0
V2 Version = 2 << vMajorShift // V2 is Version 2.0
Vmax = V2 // Vmax is an alias for the current latest version.
)

// CreateVersion creates version from major and minor value, it can only create version up < Vmax.
func CreateVersion(major, minor byte) (Version, bool) {
version := Version((major << MajorVersionShift) | minor)
if version > Vmax {
return 0, false
}
return version, true
// CreateVersion creates version from major and minor value, it can only create version up to < Vmax.
func CreateVersion(major, minor byte) Version {
return Version((major << vMajorShift) | minor)
}

// Validate checks whether given version is a valid version.
func Validate(version Version) error {
if VersionMajor(version) > VersionMajor(Vmax) {
return ErrProtocolVersionNotSupported
}
return nil
}
// Major returns major value.
func (v Version) Major() byte { return byte(v >> vMajorShift) }

// VersionMajor returns major value of given version
func VersionMajor(version Version) byte {
return byte(version >> MajorVersionShift)
}

// VersionMinor returns minor value of given version
func VersionMinor(version Version) byte {
return byte(version & MinorVersionMask)
}
// Minor returns minor value.
func (v Version) Minor() byte { return byte(v & vMinorMask) }
40 changes: 5 additions & 35 deletions proto/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package proto_test

import (
"errors"
"fmt"
"testing"

Expand All @@ -18,65 +17,36 @@ func TestCreateVersion(t *testing.T) {
major byte
minor byte
version proto.Version
ok bool
}{
{
major: 1,
minor: 1,
version: proto.Version((1 << 4) | 1),
ok: true,
},
{
major: 2,
minor: 1,
version: proto.Version((1 << 4) | 1),
ok: false,
version: proto.Version((2 << 4) | 1),
},
}

for _, tc := range tt {
t.Run(fmt.Sprintf("%d, %d", tc.major, tc.minor), func(t *testing.T) {
version, ok := proto.CreateVersion(tc.major, tc.minor)
if ok != tc.ok {
t.Fatalf("expected: %t, got: %t", tc.ok, ok)
}
if !ok {
return
}
if diff := cmp.Diff(version, tc.version); diff != "" {
v := proto.CreateVersion(tc.major, tc.minor)
if diff := cmp.Diff(v, tc.version); diff != "" {
t.Fatal(diff)
}
})
}
}

func TestValidateVersion(t *testing.T) {
tt := []struct {
version proto.Version
err error
}{
{version: 32, err: nil},
{version: 64, err: proto.ErrProtocolVersionNotSupported},
}

for _, tc := range tt {
t.Run(fmt.Sprintf("%d", tc.version), func(t *testing.T) {
err := proto.Validate(tc.version)
if !errors.Is(err, tc.err) {
t.Fatalf("expected err: %v, got: %v", tc.err, err)
}

})
}
}

func TestVersionMajorMinor(t *testing.T) {
major := proto.VersionMajor(32)
major := proto.Version(32).Major()
if major != 2 {
t.Fatalf("expected major: 2, got: %d", major)
}

minor := proto.VersionMinor(7)
minor := proto.Version(7).Minor()
if minor != 7 {
t.Fatalf("expected minor: 7, got: %d", minor)
}
Expand Down