From 72cdc705c44ee457a57e422696bcb97004656910 Mon Sep 17 00:00:00 2001 From: Jo Vandeginste Date: Thu, 9 Jan 2025 09:28:25 +0100 Subject: [PATCH] refactor(converters): Improve name detection Some workout files don't have a name; in that case, we use the filename as the name of the workout. Signed-off-by: Jo Vandeginste --- pkg/converters/parse.go | 17 +++++++++++++++++ pkg/database/gpx_sample_1_test.go | 2 +- pkg/database/workouts_test.go | 2 +- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/pkg/converters/parse.go b/pkg/converters/parse.go index d22c5b8a..8e21ba69 100644 --- a/pkg/converters/parse.go +++ b/pkg/converters/parse.go @@ -16,6 +16,23 @@ func Parse(filename string, content []byte) (*gpx.GPX, error) { return ParseGPX(content) } + basename := path.Base(filename) + + c, err := parseContent(basename, content) + if err != nil { + return nil, err + } + + if c.Name != "" || len(c.Tracks) > 0 { + return c, nil + } + + c.Name = basename + + return c, nil +} + +func parseContent(filename string, content []byte) (*gpx.GPX, error) { suffix := path.Ext(filename) switch suffix { diff --git a/pkg/database/gpx_sample_1_test.go b/pkg/database/gpx_sample_1_test.go index 6f52ee07..ae6287fc 100644 --- a/pkg/database/gpx_sample_1_test.go +++ b/pkg/database/gpx_sample_1_test.go @@ -14,7 +14,7 @@ const GpxSample1 = ` - Untitled + Some name 25.600000381469727 diff --git a/pkg/database/workouts_test.go b/pkg/database/workouts_test.go index 70b1f0df..d94e8e17 100644 --- a/pkg/database/workouts_test.go +++ b/pkg/database/workouts_test.go @@ -64,7 +64,7 @@ func TestWorkout_Parse(t *testing.T) { assert.InDelta(t, 3125, w.Data.TotalDistance, 1) assert.InDelta(t, 3.297, w.Data.AverageSpeed, 0.01) assert.InDelta(t, 3.297, w.Data.AverageSpeedNoPause, 0.01) - assert.Equal(t, "Untitled", w.Name) + assert.Equal(t, "Some name", w.Name) assert.Nil(t, w.Data.Address) }