From bc4ce882ad31ae773947b0cbe1125fedeb55b8fd Mon Sep 17 00:00:00 2001 From: Manuel Tiago Pereira Date: Sat, 21 Oct 2017 10:25:51 +0100 Subject: [PATCH 1/3] Add String() to AthleteSummary. --- athletes.go | 16 ++++++++++++++ athletes_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/athletes.go b/athletes.go index 4877a5c..0a3e199 100644 --- a/athletes.go +++ b/athletes.go @@ -3,6 +3,7 @@ package strava import ( "encoding/json" "fmt" + "reflect" "time" ) @@ -40,6 +41,21 @@ type AthleteSummary struct { BadgeTypeId int `json:"badge_type_id"` } +func (a AthleteSummary) String() string { + template := "" + aValue := reflect.ValueOf(a) + typeOfA := aValue.Type() + + for i := 0; i < aValue.NumField(); i++ { + fieldName := typeOfA.Field(i).Name + fieldValue := aValue.Field(i).Interface() + line := fmt.Sprintf("%s : %v\n", fieldName, fieldValue) + template = template + line + } + + return fmt.Sprintf(template) +} + type AthleteMeta struct { Id int64 `json:"id"` } diff --git a/athletes_test.go b/athletes_test.go index dc8156e..02e3cfb 100644 --- a/athletes_test.go +++ b/athletes_test.go @@ -385,3 +385,58 @@ func TestAthletesBadJSON(t *testing.T) { t.Error("should return a bad json error") } } + +func TestAthleteSummaryString(t *testing.T) { + athlete := new(AthleteSummary) + expected := `AthleteMeta : {0} +FirstName : +LastName : +ProfileMedium : +Profile : +City : +State : +Country : +Gender : +Friend : +Follower : +Premium : false +CreatedAt : 0001-01-01 00:00:00 +0000 UTC +UpdatedAt : 0001-01-01 00:00:00 +0000 UTC +ApproveFollowers : false +BadgeTypeId : 0 +` + + if athlete.String() != expected { + t.Errorf("athlete summary incorrect, got:\n%v\nexpected\n%v", athlete, expected) + } + + client := newCassetteClient(testToken, "athlete_get") + athlete, err := NewAthletesService(client).Get(3545423).Do() + + if err != nil { + t.Fatalf("service error: %v", err) + } + + expected = `AthleteMeta : {3545423} +FirstName : Strava +LastName : Testing +ProfileMedium : avatar/athlete/medium.png +Profile : avatar/athlete/large.png +City : Palo Alto +State : CA +Country : United States +Gender : M +Friend : accepted +Follower : accepted +Premium : false +CreatedAt : 2013-12-26 19:19:36 +0000 UTC +UpdatedAt : 2014-01-12 00:20:58 +0000 UTC +ApproveFollowers : false +BadgeTypeId : 0 +` + + if athlete.String() != expected { + t.Errorf("athlete summary string incorrect, got:\n%v\nexpected\n%v", athlete, expected) + } + +} From 33f5a59867a98c840be6bdc9176c6c13b714a2c6 Mon Sep 17 00:00:00 2001 From: Manuel Tiago Pereira Date: Sat, 21 Oct 2017 10:57:06 +0100 Subject: [PATCH 2/3] Add String() to SegmentSummary. --- segments.go | 16 ++++++++++++++ segments_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/segments.go b/segments.go index 7a7a09c..8b29d63 100644 --- a/segments.go +++ b/segments.go @@ -3,6 +3,7 @@ package strava import ( "encoding/json" "fmt" + "reflect" "time" ) @@ -42,6 +43,21 @@ type SegmentSummary struct { Starred bool `json:"starred"` } +func (s SegmentSummary) String() string { + template := "" + sValue := reflect.ValueOf(s) + typeOfS := sValue.Type() + + for i := 0; i < sValue.NumField(); i++ { + fieldName := typeOfS.Field(i).Name + fieldValue := sValue.Field(i).Interface() + line := fmt.Sprintf("%s : %v\n", fieldName, fieldValue) + template = template + line + } + + return fmt.Sprintf(template) +} + type PersonalSegmentSummary struct { SegmentSummary AthletePR struct { diff --git a/segments_test.go b/segments_test.go index f9cb4cf..99abe16 100644 --- a/segments_test.go +++ b/segments_test.go @@ -378,3 +378,58 @@ func TestClimbCategory(t *testing.T) { t.Errorf("climb category string incorrect, got %v", s) } } + +func TestSegmentSummaryString(t *testing.T) { + segment := new(SegmentSummary) + expected := `Id : 0 +Name : +ActivityType : Activity +Distance : 0 +AverageGrade : 0 +MaximumGrade : 0 +ElevationHigh : 0 +ElevationLow : 0 +ClimbCategory : Not Categorized +StartLocation : [0.000000, 0.000000] +EndLocation : [0.000000, 0.000000] +City : +State : +Country : +Private : false +Starred : false +` + + if segment.String() != expected { + t.Errorf("segment summary string incorrect, got:\n%v\nexpected\n%v", segment, expected) + } + + client := newCassetteClient(testToken, "segment_get") + segmentDetailed, err := NewSegmentsService(client).Get(229781).Do() + segment = &segmentDetailed.SegmentSummary + + if err != nil { + t.Fatalf("service error: %v", err) + } + + expected = `Id : 229781 +Name : Hawk Hill +ActivityType : Ride +Distance : 2684.82 +AverageGrade : 5.7 +MaximumGrade : 14.2 +ElevationHigh : 245.3 +ElevationLow : 92.4 +ClimbCategory : Category 4 +StartLocation : [37.833112, -122.483436] +EndLocation : [37.828072, -122.498139] +City : San Francisco +State : CA +Country : United States +Private : false +Starred : false +` + + if segment.String() != expected { + t.Errorf("segment summary string incorrect, got:\n%v\nexpected\n%v", segment, expected) + } +} From a1fb785b01fc9b678a81851312a495b766e3c2f8 Mon Sep 17 00:00:00 2001 From: Manuel Tiago Pereira Date: Sun, 22 Oct 2017 14:11:05 +0100 Subject: [PATCH 3/3] Add String() to ActivitySummary. --- activities.go | 26 +++++++++ activities_test.go | 140 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) diff --git a/activities.go b/activities.go index 430443e..db83517 100644 --- a/activities.go +++ b/activities.go @@ -3,6 +3,8 @@ package strava import ( "encoding/json" "fmt" + "reflect" + "strings" "time" ) @@ -539,3 +541,27 @@ func (t ActivityType) String() string { func (l Location) String() string { return fmt.Sprintf("[%f, %f]", l[0], l[1]) } + +func (a ActivitySummary) String() string { + template := "" + aValue := reflect.ValueOf(a) + typeOfA := aValue.Type() + + for i := 0; i < aValue.NumField(); i++ { + line := "" + fieldName := typeOfA.Field(i).Name + fieldValue := aValue.Field(i).Interface() + if fieldName == "Map" { + continue + } + if fieldName == "Athlete" { + fieldValueString := strings.TrimSuffix(fmt.Sprintf("%v", fieldValue), "\n") + line = fmt.Sprintf("%s : {\n%v\n}\n", fieldName, fieldValueString) + } else { + line = fmt.Sprintf("%s : %v\n", fieldName, fieldValue) + } + template = template + line + } + + return fmt.Sprintf(template) +} diff --git a/activities_test.go b/activities_test.go index 5938c4e..d12f8d5 100644 --- a/activities_test.go +++ b/activities_test.go @@ -829,3 +829,143 @@ func TestActivityType(t *testing.T) { t.Errorf("activity type string incorrect, got %v", s) } } + +func TestActivitySummaryString(t *testing.T) { + activity := new(ActivitySummary) + expected := `Id : 0 +ExternalId : +UploadId : 0 +Athlete : { +AthleteMeta : {0} +FirstName : +LastName : +ProfileMedium : +Profile : +City : +State : +Country : +Gender : +Friend : +Follower : +Premium : false +CreatedAt : 0001-01-01 00:00:00 +0000 UTC +UpdatedAt : 0001-01-01 00:00:00 +0000 UTC +ApproveFollowers : false +BadgeTypeId : 0 +} +Name : +Distance : 0 +MovingTime : 0 +ElapsedTime : 0 +TotalElevationGain : 0 +Type : Activity +StartDate : 0001-01-01 00:00:00 +0000 UTC +StartDateLocal : 0001-01-01 00:00:00 +0000 UTC +TimeZone : +StartLocation : [0.000000, 0.000000] +EndLocation : [0.000000, 0.000000] +City : +State : +Country : +AchievementCount : 0 +KudosCount : 0 +CommentCount : 0 +AthleteCount : 0 +PhotoCount : 0 +Trainer : false +Commute : false +Manual : false +Private : false +Flagged : false +GearId : +AverageSpeed : 0 +MaximunSpeed : 0 +AverageCadence : 0 +AverageTemperature : 0 +AveragePower : 0 +WeightedAveragePower : 0 +Kilojoules : 0 +DeviceWatts : false +AverageHeartrate : 0 +MaximumHeartrate : 0 +Truncated : 0 +HasKudoed : false +` + + if activity.String() != expected { + t.Errorf("activity string incorrect, got:\n%v\nexpected\n%v", activity, expected) + } + + client := newCassetteClient(testToken, "activity_get") + activityDetailed, err := NewActivitiesService(client).Get(103221154).Do() + + if err != nil { + t.Fatalf("service error: %v", err) + } + + activity = &activityDetailed.ActivitySummary + expected = `Id : 103221154 +ExternalId : 2010-08-15-11-04-29.fit +UploadId : 112859609 +Athlete : { +AthleteMeta : {227615} +FirstName : John +LastName : Applestrava +ProfileMedium : http://dgalywyr863hv.cloudfront.net/pictures/athletes/227615/41555/3/medium.jpg +Profile : http://dgalywyr863hv.cloudfront.net/pictures/athletes/227615/41555/3/large.jpg +City : San Francisco +State : CA +Country : United States +Gender : M +Friend : accepted +Follower : accepted +Premium : true +CreatedAt : 2012-01-18 18:20:37 +0000 UTC +UpdatedAt : 2014-01-21 06:23:32 +0000 UTC +ApproveFollowers : false +BadgeTypeId : 0 +} +Name : 08/15/2010 Davis, CA +Distance : 20739.1 +MovingTime : 2836 +ElapsedTime : 3935 +TotalElevationGain : 22 +Type : Ride +StartDate : 2010-08-15 18:04:29 +0000 UTC +StartDateLocal : 2010-08-15 11:04:29 +0000 UTC +TimeZone : +StartLocation : [38.550000, -121.820000] +EndLocation : [38.560000, -121.780000] +City : Davis +State : CA +Country : United States +AchievementCount : 0 +KudosCount : 1 +CommentCount : 1 +AthleteCount : 2 +PhotoCount : 0 +Trainer : false +Commute : true +Manual : false +Private : false +Flagged : false +GearId : b77076 +AverageSpeed : 7.313 +MaximunSpeed : 13.7 +AverageCadence : 73.2 +AverageTemperature : 27 +AveragePower : 140.2 +WeightedAveragePower : 202 +Kilojoules : 397.5 +DeviceWatts : true +AverageHeartrate : 104.4 +MaximumHeartrate : 147 +Truncated : 0 +HasKudoed : false +` + + if activity.String() != expected { + t.Errorf("activity string incorrect, got:\n%v\nexpected\n%v", activity, expected) + } + +}