-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
autogenerate name & maxElevation in the elevation profile (#20)
- Loading branch information
Showing
6 changed files
with
132 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,114 @@ | ||
package activity | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"reflect" | ||
"strings" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
var ( | ||
ErrFieldNotFound = errors.New("field not found in struct") | ||
ErrTypeNotAssert = errors.New("could not assert type in struct") | ||
) | ||
|
||
type Header struct { | ||
Meta HeaderMeta `yaml:"meta"` | ||
Activity HeaderActivity `yaml:"activity"` | ||
Layout Layout `yaml:"layout"` | ||
Stats Stats `yaml:"stats"` | ||
Meta struct { | ||
Version string `yaml:"version,omitempty"` | ||
} `yaml:"meta"` | ||
Activity struct { | ||
Wandern bool `yaml:"wandern,omitempty"` | ||
Skitour bool `yaml:"skitour,omitempty"` | ||
MTB bool `yaml:"mtb,omitempty"` | ||
Type string `yaml:"type"` | ||
Date string `yaml:"date"` | ||
Title string `yaml:"title"` | ||
PointOfOrigin struct { | ||
Name string `yaml:"name"` | ||
Qr string `yaml:"qr"` | ||
Region string `yaml:"region"` | ||
} `yaml:"pointOfOrigin"` | ||
Season string `yaml:"season"` | ||
Rating string `yaml:"rating"` | ||
Company string `yaml:"company"` | ||
Restaurant string `yaml:"restaurant"` | ||
Difficulty string `yaml:"difficulty,omitempty"` | ||
LLB string `yaml:"llb,omitempty"` | ||
MaxElevation string `yaml:"maxElevation"` | ||
} `yaml:"activity"` | ||
Layout struct { | ||
HeadElevationProfile bool `yaml:"headElevationProfile"` | ||
ElevationProfileType string `yaml:"elevationProfileType,omitempty"` | ||
ElevationProfileRightMargin float32 `yaml:"elevationProfileRightMargin"` | ||
TableSize float32 `yaml:"tableSize"` | ||
MapSize float32 `yaml:"mapSize"` | ||
MapHeight int `yaml:"mapHeight"` | ||
Linespread float32 `yaml:"linespread"` | ||
} `yaml:"layout"` | ||
Stats struct { | ||
Ascent string `yaml:"ascent"` | ||
Distance string `yaml:"distance"` | ||
MovingTime string `yaml:"movingTime"` | ||
OverallTime string `yaml:"overallTime"` | ||
StartTime string `yaml:"startTime"` | ||
SummitTime string `yaml:"summitTime"` | ||
Puls string `yaml:"puls,omitempty"` | ||
} `yaml:"stats"` | ||
} | ||
|
||
type HeaderMeta struct { | ||
Version string `yaml:"version,omitempty"` | ||
} | ||
func GetFromHeader[T any](dir string, field string) (T, error) { //nolint:ireturn | ||
var zero T | ||
|
||
type HeaderActivity struct { | ||
Wandern bool `yaml:"wandern,omitempty"` | ||
Type string `yaml:"type"` | ||
Date string `yaml:"date"` | ||
Title string `yaml:"title"` | ||
PointOfOrigin PointOfOrigin `yaml:"pointOfOrigin"` | ||
Season string `yaml:"season"` | ||
Rating string `yaml:"rating"` | ||
Company string `yaml:"company"` | ||
Restaurant string `yaml:"restaurant"` | ||
MaxElevation string `yaml:"maxElevation"` | ||
} | ||
data, err := os.ReadFile(dir + "/header.yaml") | ||
if err != nil { | ||
return zero, fmt.Errorf("error reading file: %w", err) | ||
} | ||
|
||
type PointOfOrigin struct { | ||
Name string `yaml:"name"` | ||
Qr string `yaml:"qr"` | ||
Region string `yaml:"region"` | ||
} | ||
var act Header | ||
|
||
err = yaml.Unmarshal(data, &act) | ||
if err != nil { | ||
return zero, fmt.Errorf("error unmarshalling YAML: %w", err) | ||
} | ||
|
||
type Stats struct { | ||
Ascent string `yaml:"ascent"` | ||
Distance string `yaml:"distance"` | ||
MovingTime string `yaml:"movingTime"` | ||
OverallTime string `yaml:"overallTime"` | ||
StartTime string `yaml:"startTime"` | ||
SummitTime string `yaml:"summitTime"` | ||
Puls string `yaml:"puls,omitempty"` | ||
value, err := searchField[T](&act, field) | ||
if err != nil { | ||
return zero, fmt.Errorf("error searching field: %w", err) | ||
} | ||
|
||
return value, nil | ||
} | ||
|
||
type Layout struct { | ||
HeadElevationProfile bool `yaml:"headElevationProfile"` | ||
ElevationProfileType string `yaml:"elevationProfileType,omitempty"` | ||
ElevationProfileRightMargin float32 `yaml:"elevationProfileRightMargin"` | ||
TableSize float32 `yaml:"tableSize"` | ||
MapSize float32 `yaml:"mapSize"` | ||
MapHeight int `yaml:"mapHeight"` | ||
Linespread float32 `yaml:"linespread"` | ||
func searchField[T any](v interface{}, path string) (T, error) { //nolint:ireturn | ||
keys := strings.Split(path, ".") | ||
header := reflect.ValueOf(v) | ||
|
||
// Traverse the struct hierarchy using the keys | ||
for _, key := range keys { | ||
// Check if we are dealing with a pointer and dereference it | ||
if header.Kind() == reflect.Ptr { | ||
header = header.Elem() | ||
} | ||
|
||
// Get the field by name | ||
header = header.FieldByName(key) | ||
if !header.IsValid() { | ||
var zero T | ||
|
||
return zero, fmt.Errorf("field %s not found: %w", key, ErrFieldNotFound) | ||
} | ||
} | ||
|
||
// Type assert the value to the desired type T | ||
value, ok := header.Interface().(T) | ||
if !ok { | ||
var zero T | ||
|
||
return zero, fmt.Errorf("field %s cannot be asserted to the expected type: %w", path, ErrTypeNotAssert) | ||
} | ||
|
||
return value, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters