Skip to content

Commit

Permalink
Change all uints to uint64
Browse files Browse the repository at this point in the history
This reduces the number of conversions

Signed-off-by: Jo Vandeginste <[email protected]>
  • Loading branch information
jovandeginste committed Jan 19, 2025
1 parent 6128b1c commit 25ec103
Show file tree
Hide file tree
Showing 16 changed files with 43 additions and 40 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,18 @@ build-templates:
templ generate

test-templates:
go test ./views/*/
go test ./views/...

test-commands:
go test ./cmd/...

format-templates:
find . -type f -name '*.templ' -exec templ fmt -v {} \;

serve:
$(WT_OUTPUT_FILE)

test: test-go test-assets test-templates
test: test-go test-assets test-templates test-commands

test-assets:
prettier --check .
Expand Down
12 changes: 6 additions & 6 deletions cmd/wt-debug/workouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (c *cli) workoutsDiagCmd() *cobra.Command {
t := table.New(os.Stdout)
t.SetHeaders("ID", "Name", "Issues")

var ids []uint
var ids []uint64

if err := c.getDatabase().Model(&database.Workout{}).Pluck("ID", &ids).Error; err != nil {
return err
Expand All @@ -40,7 +40,7 @@ func (c *cli) workoutsDiagCmd() *cobra.Command {
for _, id := range ids {
issues := []string{}

wo, err := database.GetWorkout(c.getDatabase(), int(id))
wo, err := database.GetWorkout(c.getDatabase(), id)
if err != nil {
issues = append(issues, err.Error())
}
Expand All @@ -49,7 +49,7 @@ func (c *cli) workoutsDiagCmd() *cobra.Command {
issues = []string{"OK"}
}

t.AddRow(strconv.FormatUint(uint64(id), 10), wo.Name, strings.Join(issues, "; "))
t.AddRow(strconv.FormatUint(id, 10), wo.Name, strings.Join(issues, "; "))
}

t.Render()
Expand All @@ -73,7 +73,7 @@ func (c *cli) workoutsListCmd() *cobra.Command {
}

for _, wo := range workouts {
t.AddRow(strconv.FormatUint(uint64(wo.ID), 10), wo.Date.String(), wo.Name)
t.AddRow(strconv.FormatUint(wo.ID, 10), wo.Date.String(), wo.Name)
}

t.Render()
Expand All @@ -89,7 +89,7 @@ func (c *cli) workoutsShowCmd() *cobra.Command {
Short: "Show information about a workout",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
id, err := strconv.Atoi(args[0])
id, err := strconv.ParseUint(args[0], 10, 32)
if err != nil {
return err
}
Expand All @@ -102,7 +102,7 @@ func (c *cli) workoutsShowCmd() *cobra.Command {
return err
}

t.AddRow("ID", strconv.FormatUint(uint64(wo.ID), 10))
t.AddRow("ID", strconv.FormatUint(wo.ID, 10))
t.AddRow("Date", wo.Date.String())
t.AddRow("Name", wo.Name)

Expand Down
4 changes: 2 additions & 2 deletions pkg/app/background.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (a *App) updateRouteSegments(l *slog.Logger) {
}

func (a *App) updateWorkout(l *slog.Logger) {
var wID []int
var wID []uint64

db := a.db.Preload("Data.Details").Preload("User")

Expand All @@ -263,7 +263,7 @@ func (a *App) updateWorkout(l *slog.Logger) {
}
}

func (a *App) UpdateWorkout(i int) error {
func (a *App) UpdateWorkout(i uint64) error {
w, err := database.GetWorkoutWithGPX(a.db, i)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/workouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (a *App) addWorkout(c echo.Context) error {
workout.Data.Creator = "web-interface"

var equipmentIDS struct {
EquipmentIDs []uint `form:"equipment"`
EquipmentIDs []uint64 `form:"equipment"`
}

if err := c.Bind(&equipmentIDS); err != nil {
Expand Down Expand Up @@ -206,7 +206,7 @@ func (a *App) workoutsUpdateHandler(c echo.Context) error {
d.Update(workout)

var equipmentIDS struct {
EquipmentIDs []uint `form:"equipment"`
EquipmentIDs []uint64 `form:"equipment"`
}

if err := c.Bind(&equipmentIDS); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/app/workouts_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (a *App) workoutsHandler(c echo.Context) error {
func (a *App) workoutsShowHandler(c echo.Context) error {
a.setContext(c)

id, err := strconv.Atoi(c.Param("id"))
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
return a.redirectWithError(c, a.echo.Reverse("workouts"), err)
}
Expand Down Expand Up @@ -210,7 +210,7 @@ func (a *App) workoutsEditHandler(c echo.Context) error {
}

func (a *App) workoutsCreateRouteSegmentFromWorkoutHandler(c echo.Context) error {
id, err := strconv.Atoi(c.Param("id"))
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
return a.redirectWithError(c, a.echo.Reverse("workouts"), err)
}
Expand Down Expand Up @@ -244,7 +244,7 @@ func (a *App) workoutsCreateRouteSegmentFromWorkoutHandler(c echo.Context) error
func (a *App) workoutsCreateRouteSegmentHandler(c echo.Context) error {
a.setContext(c)

id, err := strconv.Atoi(c.Param("id"))
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
return a.redirectWithError(c, a.echo.Reverse("workouts"), err)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/database/equipment.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ type Equipment struct {

User User

UserID uint `gorm:"not null;index"` // The ID of the user who owns the workout
Active bool `gorm:"default:true" json:"active" form:"active"` // Whether this equipment is active
UserID uint64 `gorm:"not null;index"` // The ID of the user who owns the workout
Active bool `gorm:"default:true" json:"active" form:"active"` // Whether this equipment is active
}

type WorkoutEquipment struct {
Model
Workout Workout
Equipment Equipment
WorkoutID uint `gorm:"not null;uniqueIndex:idx_workout_equipment"` // The ID of the workout
EquipmentID uint `gorm:"not null;uniqueIndex:idx_workout_equipment"` // The ID of the equipment
WorkoutID uint64 `gorm:"not null;uniqueIndex:idx_workout_equipment"` // The ID of the workout
EquipmentID uint64 `gorm:"not null;uniqueIndex:idx_workout_equipment"` // The ID of the equipment
}

func GetEquipment(db *gorm.DB, id int) (*Equipment, error) {
Expand Down Expand Up @@ -60,7 +60,7 @@ func (e *Equipment) Save(db *gorm.DB) error {
return db.Omit(clause.Associations).Save(e).Error
}

func GetEquipmentByIDs(db *gorm.DB, userID uint, ids []uint) ([]*Equipment, error) {
func GetEquipmentByIDs(db *gorm.DB, userID uint64, ids []uint64) ([]*Equipment, error) {
var equipment []*Equipment

if len(ids) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var ErrUnsuportedDriver = errors.New("unsupported driver")
type Model struct {
CreatedAt time.Time
UpdatedAt time.Time
ID uint `gorm:"primaryKey"`
ID uint64 `gorm:"primaryKey"`
}

func Connect(driver, dsn string, debug bool, logger *slog.Logger) (*gorm.DB, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Profile struct {
TotalsShow WorkoutType `form:"totals_show"` // What workout type of totals to show
Timezone string `form:"timezone"` // The user's preferred timezone
AutoImportDirectory string `form:"auto_import_directory"` // The user's preferred directory for auto-import
UserID uint // The ID of the user who owns this profile
UserID uint64 // The ID of the user who owns this profile
APIActive bool `form:"api_active"` // Whether the user's API key is active
SocialsDisabled bool `form:"socials_disabled"` // Whether social sharing buttons are disabled when viewing a workout
PreferFullDate bool `form:"prefer_full_date"` // Whether to show full dates in the workout details
Expand Down
4 changes: 2 additions & 2 deletions pkg/database/route_segment_matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type RouteSegmentMatch struct {
first, last MapPoint // The first and last point of the route
end MapPoint // The last point of the workout

RouteSegmentID uint `gorm:"primaryKey"` // The ID of the route segment
WorkoutID uint `gorm:"primaryKey"` // The ID of the workout
RouteSegmentID uint64 `gorm:"primaryKey"` // The ID of the route segment
WorkoutID uint64 `gorm:"primaryKey"` // The ID of the workout
FirstID, LastID int // The index of the first and last point of the route
Distance float64 // The total distance of the route segment for this workout
Duration time.Duration // The total duration of the route segment for this workout
Expand Down
6 changes: 3 additions & 3 deletions pkg/database/user_statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type (
Statistics struct {
Buckets map[WorkoutType]Buckets // The statistics buckets
BucketFormat string // The bucket format in strftime format
UserID uint // The user ID
UserID uint64 // The user ID
}

Buckets struct {
Expand Down Expand Up @@ -46,14 +46,14 @@ type (
Float64Record struct {
Date time.Time // The timestamp of the record
Value float64 // The value of the record
ID uint // The workout ID of the record
ID uint64 // The workout ID of the record
}

// DurationRecord is a single record if the value is a time.Duration
DurationRecord struct {
Date time.Time // The timestamp of the record
Value time.Duration // The value of the record
ID uint // The workout ID of the record
ID uint64 // The workout ID of the record
}

// WorkoutRecord is the collection of records for a single workout type
Expand Down
14 changes: 7 additions & 7 deletions pkg/database/workouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Workout struct {
Type WorkoutType // The type of the workout
Equipment []Equipment `json:",omitempty" gorm:"constraint:OnDelete:CASCADE;many2many:workout_equipment"` // Which equipment is used for this workout
RouteSegmentMatches []*RouteSegmentMatch `gorm:"constraint:OnDelete:CASCADE" json:",omitempty"` // Which route segments match
UserID uint `gorm:"not null;index;uniqueIndex:idx_start_user"` // The ID of the user who owns the workout
UserID uint64 `gorm:"not null;index;uniqueIndex:idx_start_user"` // The ID of the user who owns the workout
Dirty bool // Whether the workout has been modified and the details should be re-rendered
}

Expand All @@ -43,7 +43,7 @@ type GPXData struct {
Filename string // The filename of the file
Content []byte `gorm:"type:text"` // The file content
Checksum []byte `gorm:"not null;uniqueIndex"` // The checksum of the content
WorkoutID uint `gorm:"not null;uniqueIndex"` // The ID of the workout
WorkoutID uint64 `gorm:"not null;uniqueIndex"` // The ID of the workout
}

func (w *Workout) GetDate() time.Time {
Expand Down Expand Up @@ -415,15 +415,15 @@ func GetWorkoutWithGPXByUUID(db *gorm.DB, u uuid.UUID) (*Workout, error) {
return GetWorkoutByUUID(db.Preload("GPX"), u)
}

func GetWorkoutWithGPX(db *gorm.DB, id int) (*Workout, error) {
func GetWorkoutWithGPX(db *gorm.DB, id uint64) (*Workout, error) {
return GetWorkout(db.Preload("GPX").Preload("Data.Details"), id)
}

func GetWorkoutDetailsByUUID(db *gorm.DB, u uuid.UUID) (*Workout, error) {
return GetWorkoutWithGPXByUUID(db.Preload("Data.Details"), u)
}

func GetWorkoutDetails(db *gorm.DB, id int) (*Workout, error) {
func GetWorkoutDetails(db *gorm.DB, id uint64) (*Workout, error) {
return GetWorkoutWithGPX(db.Preload("Data.Details"), id)
}

Expand All @@ -450,7 +450,7 @@ func GetWorkoutByUUID(db *gorm.DB, u uuid.UUID) (*Workout, error) {
return &w, nil
}

func GetWorkout(db *gorm.DB, id int) (*Workout, error) {
func GetWorkout(db *gorm.DB, id uint64) (*Workout, error) {
var w Workout

if err := db.
Expand Down Expand Up @@ -655,8 +655,8 @@ func (w *Workout) HasExtraMetric(name string) bool {
return slices.Contains(w.Data.ExtraMetrics, name)
}

func (w *Workout) EquipmentIDs() []uint {
ids := make([]uint, 0, len(w.Equipment))
func (w *Workout) EquipmentIDs() []uint64 {
ids := make([]uint64, 0, len(w.Equipment))

for _, e := range w.Equipment {
ids = append(ids, e.ID)
Expand Down
4 changes: 2 additions & 2 deletions pkg/database/workouts_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type MapData struct {
Name string // The name of the workout
AddressString string // The generic location of the workout
Center MapCenter `gorm:"serializer:json"` // The center of the workout (in coordinates)
WorkoutID uint `gorm:"not null;uniqueIndex"` // The workout this data belongs to
WorkoutID uint64 `gorm:"not null;uniqueIndex"` // The workout this data belongs to
TotalDistance float64 // The total distance of the workout
TotalDuration time.Duration // The total duration of the workout
MaxSpeed float64 // The maximum speed of the workout
Expand All @@ -89,7 +89,7 @@ type MapDataDetails struct {
MapData *MapData `gorm:"foreignKey:MapDataID" json:"-"`
Points []MapPoint `gorm:"serializer:json"` // The GPS points of the workout

MapDataID uint `gorm:"not null;uniqueIndex"` // The ID of the map data these details belong to
MapDataID uint64 `gorm:"not null;uniqueIndex"` // The ID of the map data these details belong to
}

// MapCenter is the center of the workout
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/workouts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestWorkout_SaveAndGet(t *testing.T) {
require.NoError(t, w.Save(db))
assert.NotZero(t, w.UpdatedAt)

newW, err := GetWorkoutDetails(db, int(w.ID))
newW, err := GetWorkoutDetails(db, w.ID)
require.NoError(t, err)
assert.Equal(t, w.ID, newW.ID)
assert.Equal(t, w.Data.Details.Points, newW.Data.Details.Points)
Expand Down
2 changes: 1 addition & 1 deletion views/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func A2S(v any) string {
}

return "false"
case int, uint:
case int, uint64:
return fmt.Sprintf("%d", e)
case float64:
return fmt.Sprintf("%.2f", e)
Expand Down
2 changes: 1 addition & 1 deletion views/user/stats.templ
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ templ StatsRecordDistanceDate(workout *database.Float64Record) {
@StatsRecordDate(workout.ID, &workout.Date)
}

templ StatsRecordDate(id uint, date *time.Time) {
templ StatsRecordDate(id uint64, date *time.Time) {
<span class="hidden xl:inline">
<a href={ templ.SafeURL(helpers.RouteFor(ctx, "workout-show", id)) }>
{ helpers.LocalDate(ctx, date ) }
Expand Down
2 changes: 1 addition & 1 deletion views/user/stats_templ.go

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

0 comments on commit 25ec103

Please sign in to comment.