Skip to content

Commit

Permalink
fix: profile version calculation (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
muktihari authored Dec 29, 2023
1 parent cf34ba8 commit 79381ab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
32 changes: 24 additions & 8 deletions internal/cmd/fitgen/profile/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ package profile

import (
"fmt"
"math"
"path/filepath"
"runtime"
"strconv"
"strings"
"text/template"
"unicode"

"github.com/muktihari/fit/internal/cmd/fitgen/builder"
"github.com/muktihari/fit/internal/cmd/fitgen/builder/shared"
Expand Down Expand Up @@ -144,11 +145,26 @@ func transformBaseType(s string) string {
}

func toProfileVersion(s string) string {
s = strings.Map(func(r rune) rune {
if !unicode.IsDigit(r) {
return -1
}
return r
}, s)
return s
// On error, use panic so we can get stack trace, should not generate when version is invalid.
parts := strings.Split(s, ".")
if len(parts) < 2 {
panic(fmt.Errorf("malformed sdkversion, should in the form of <major>.<minor>, got: %s", s))
}

major, err := strconv.ParseUint(parts[0], 10, 64)
if err != nil {
panic(fmt.Errorf("invalid major version: %w", err))
}
minor, err := strconv.ParseUint(parts[1], 10, 64)
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))
}

return fmt.Sprintf("%s // (Major * 1000) + Minor", strconv.FormatUint(version, 10))
}
2 changes: 1 addition & 1 deletion 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 79381ab

Please sign in to comment.