Skip to content

Commit

Permalink
decoder: reset on Decode, make Next as optional
Browse files Browse the repository at this point in the history
  • Loading branch information
muktihari committed Sep 12, 2024
1 parent 01c389e commit e55bd82
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
24 changes: 15 additions & 9 deletions decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ func WithReadBufferSize(size int) Option {
// The FIT protocol allows for multiple FIT files to be chained together in a single FIT file.
// Each FIT file in the chain must be a properly formatted FIT file (header, data records, CRC).
//
// To decode chained FIT files, use Next() to check if r hasn't reach EOF and next bytes are still a valid FIT sequences.
// To decode a chained FIT file containing multiple FIT data, invoke Decode() or DecodeWithContext()
// method multiple times. For convenience, we can wrap it with the Next() method as follows (optional):
//
// for dec.Next() {
// fit, err := dec.Decode()
Expand Down Expand Up @@ -364,14 +365,14 @@ func (d *Decoder) Next() bool {
if !d.sequenceCompleted {
return true
}
d.reset() // reset values for the next chained FIT file
d.sequenceCompleted = false
// err is saved in the func, any exported will call this func anyway.
return d.decodeFileHeaderOnce() == nil
}

// Decode method decodes `r` into FIT data. One invocation will produce one valid FIT data or
// an error if it occurs. To decode a chained FIT file containing multiple FIT data, invoke this
// method multiple times, however, the invocation must be wrapped with Next() method as follows:
// method multiple times. For convenience, we can wrap it with the Next() method as follows (optional):
//
// for dec.Next() {
// fit, err := dec.Decode()
Expand All @@ -393,12 +394,14 @@ func (d *Decoder) Decode() (*proto.FIT, error) {
if d.err = d.decodeCRC(); d.err != nil {
return nil, d.err
}
d.sequenceCompleted = true
return &proto.FIT{
fit := &proto.FIT{
FileHeader: d.fileHeader,
Messages: d.messages,
CRC: d.crc,
}, nil
}
d.reset()
d.sequenceCompleted = true
return fit, nil
}

// Discard discards a single FIT file sequence and returns any error encountered. This method directs the Decoder to
Expand Down Expand Up @@ -440,6 +443,7 @@ func (d *Decoder) Discard() error {
if _, d.err = d.readN(2); d.err != nil { // Discard File CRC
return d.err
}
d.reset()
d.sequenceCompleted = true
return d.err
}
Expand Down Expand Up @@ -1009,12 +1013,14 @@ func (d *Decoder) DecodeWithContext(ctx context.Context) (*proto.FIT, error) {
if d.err = d.decodeCRC(); d.err != nil {
return nil, d.err
}
d.sequenceCompleted = true
return &proto.FIT{
fit := &proto.FIT{
FileHeader: d.fileHeader,
Messages: d.messages,
CRC: d.crc,
}, nil
}
d.reset()
d.sequenceCompleted = true
return fit, nil
}

func checkContext(ctx context.Context) error {
Expand Down
2 changes: 1 addition & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func main() {

### Decode Chained FIT Files

A single invocation of `Decode()` will process exactly one FIT sequence. To decode chained FIT files, wrap the decode process with a loop and use `dec.Next()` to check whether next sequence of bytes are still a valid FIT sequence.
A single invocation of `Decode()` will process exactly one FIT sequence. To decode a chained FIT file containing multiple FIT data, invoke Decode() or DecodeWithContext() method multiple times. For convenience, we can wrap it with the Next() method as follows (optional):

```go
...
Expand Down

0 comments on commit e55bd82

Please sign in to comment.