Skip to content

Commit

Permalink
fix: export profile version, remove Version() function (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
muktihari authored Apr 10, 2024
1 parent c78236a commit 50cc763
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion decoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func TestCheckIntegrity(t *testing.T) {
h := proto.FileHeader{
Size: 14,
ProtocolVersion: byte(proto.V2),
ProfileVersion: profile.Version(),
ProfileVersion: profile.Version,
DataSize: 0,
DataType: proto.DataTypeFIT,
}
Expand Down
2 changes: 1 addition & 1 deletion encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func New(w io.Writer, opts ...Option) *Encoder {
defaultFileHeader: proto.FileHeader{
Size: proto.DefaultFileHeaderSize,
ProtocolVersion: byte(options.protocolVersion),
ProfileVersion: profile.Version(),
ProfileVersion: profile.Version,
DataSize: 0, // calculated during encoding
DataType: proto.DataTypeFIT,
CRC: 0, // calculated during encoding
Expand Down
2 changes: 1 addition & 1 deletion encoder/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ func TestEncodeHeader(t *testing.T) {
0, 0, // crc checksum will be calculated
}

binary.LittleEndian.PutUint16(b[2:4], profile.Version())
binary.LittleEndian.PutUint16(b[2:4], profile.Version)

crc := crc16.New(crc16.MakeFitTable())
crc.Write(b[:12])
Expand Down
33 changes: 18 additions & 15 deletions internal/cmd/fitgen/profile/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package profile

import (
"fmt"
"math"
"path/filepath"
"runtime"
"strconv"
Expand Down Expand Up @@ -138,39 +137,43 @@ func (b *profilebuilder) buildVersion() builder.Data {
TemplateExec: "version",
Path: b.path,
Filename: "version_gen.go",
Data: VersionData{
ProfileVersion: b.profileVersion,
Package: "profile",
Version: toVersion(b.profileVersion),
},
Data: createVersionData(b.profileVersion),
}
}

func transformBaseType(s string) string {
return "basetype." + s
}

func toVersion(profileVersion string) uint16 {
func createVersionData(profileVersion string) VersionData {
// On error, use panic so we can get stack trace, should not generate when version is invalid.
parts := strings.Split(profileVersion, ".")
if len(parts) < 2 {
panic(fmt.Errorf("malformed profile version, should in the form of <major>.<minor>, got: %s", profileVersion))
}
var (
majorPart = parts[0]
minorPart = parts[1]
)

major, err := strconv.ParseUint(parts[0], 10, 64)
major, err := strconv.ParseUint(majorPart, 10, 16)
if err != nil {
panic(fmt.Errorf("invalid major version: %w", err))
}
minor, err := strconv.ParseUint(parts[1], 10, 64)
minor, err := strconv.ParseUint(minorPart, 10, 16)
if err != nil {
panic(fmt.Errorf("invalid minor version: %w", err))
}

version := (major * 1000) + minor

if version >= math.MaxUint16 {
panic(fmt.Errorf("version should not exceed max uint16, expected < %d, got: %d", math.MaxUint16, version))
version, err := strconv.ParseUint(majorPart+minorPart, 10, 16)
if err != nil {
panic(fmt.Errorf("invalid version: %w", err))
}

return uint16(version)
return VersionData{
Package: "profile",
ProfileVersion: profileVersion,
Major: uint16(major),
Minor: uint16(minor),
Version: uint16(version),
}
}
2 changes: 2 additions & 0 deletions internal/cmd/fitgen/profile/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ type ProfileTypeBaseType struct {
type VersionData struct {
ProfileVersion string
Package string
Major uint16
Minor uint16
Version uint16
}
8 changes: 3 additions & 5 deletions internal/cmd/fitgen/profile/profile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ func (p ProfileType) BaseType() basetype.BaseType {

package {{ .Package }}

const version = {{ .Version }}
// Version is the current profile version, v{{ .ProfileVersion }}, in uint16 representation.
// -> "{{ .Major }}" + "{{ .Minor }}" = "{{ .Version }}" -> {{ .Version }}.
const Version uint16 = {{ .Version }}

// Version returns the current profile version, v{{ .ProfileVersion }}, in its uint16 form: (Major * 1000) + Minor = {{ .Version }}.
func Version() uint16 {
return version
}

{{ end }}
2 changes: 2 additions & 0 deletions profile/profile_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions profile/version_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 50cc763

Please sign in to comment.