Skip to content

Commit 89c6d7a

Browse files
committed
Refactor to bone mass and bone percent as base units
1 parent 7558e35 commit 89c6d7a

File tree

4 files changed

+38
-27
lines changed

4 files changed

+38
-27
lines changed

bodycomposition.go

+5-19
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func (bc BodyComposition) writeFitFile(writer io.Writer) error {
4040
Weight: fit.Weight(bc.Weight * 100),
4141
PercentFat: uint16(bc.PercentFat * 100),
4242
PercentHydration: uint16(bc.PercentHydration * 100),
43-
BoneMass: uint16(bc.BoneMass),
44-
MuscleMass: uint16(bc.MuscleMass),
43+
BoneMass: uint16(bc.BoneMass * 100),
44+
MuscleMass: uint16(bc.MuscleMass * 100),
4545
VisceralFatRating: uint8(bc.VisceralFatRating),
4646
PhysiqueRating: uint8(bc.PhysiqueRating),
4747
MetabolicAge: uint8(bc.MetabolicAge),
@@ -54,33 +54,19 @@ func (bc BodyComposition) writeFitFile(writer io.Writer) error {
5454
}
5555

5656
// NewBodyComposition creates a new BodyComposition instance
57-
func NewBodyComposition(weight, percentFat, percentHydration, percentBone, boneMass, percentMuscle, muscleMass ,visceralFatRating, physiqueRating, metabolicAge, caloriesActiveMet, bmi float64, timestamp int64) BodyComposition {
57+
func NewBodyComposition(weight, percentFat, percentHydration, boneMass, muscleMass, visceralFatRating, physiqueRating, metabolicAge, caloriesActiveMet, bmi float64, timestamp int64) BodyComposition {
5858
ts := time.Now()
5959
if timestamp != -1 {
6060
ts = time.Unix(timestamp, 0)
6161
}
62-
var bm float64
63-
var mm float64
64-
65-
if(percentBone != 0 ) {
66-
bm = weight * percentBone
67-
} else {
68-
bm = boneMass * 100
69-
}
70-
71-
if(percentMuscle != 0 ) {
72-
mm = weight * percentMuscle
73-
} else {
74-
mm = muscleMass * 100
75-
}
7662

7763
return BodyComposition{
7864
TimeStamp: ts,
7965
Weight: weight,
8066
PercentFat: percentFat,
8167
PercentHydration: percentHydration,
82-
BoneMass: bm,
83-
MuscleMass: mm,
68+
BoneMass: boneMass,
69+
MuscleMass: muscleMass,
8470
VisceralFatRating: visceralFatRating,
8571
PhysiqueRating: physiqueRating,
8672
MetabolicAge: metabolicAge,

bodycomposition_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
func TestNewBodyCompositionWithDefault(t *testing.T) {
99

10-
bc := NewBodyComposition(80, 14.4, 55.2, 37, 2.98, 45.5, 55, 21, 5, 23, 2250, 12.6, -1)
10+
bc := NewBodyComposition(80, 14.4, 55.2, 37, 45.5, 21, 5, 23, 2250, 12.6, -1)
1111

1212
now := time.Now()
1313

@@ -26,7 +26,7 @@ func TestNewBodyComposition(t *testing.T) {
2626
// set local time to UTC, no matter where executed
2727
time.Local = time.UTC
2828

29-
bc := NewBodyComposition(80, 14.4, 55.2, 37, 2.98, 45.5, 55, 21, 5, 23, 2250, 12.6, timeStamp)
29+
bc := NewBodyComposition(80, 14.4, 55.2, 37, 45.5, 21, 5, 23, 2250, 12.6, timeStamp)
3030

3131
year, month, date := bc.TimeStamp.Date()
3232
hour, min, sec := bc.TimeStamp.Clock()

cmd/upload.go

+30-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,32 @@ var uploadCmd = &cobra.Command{
3636
calories, _ := flags.GetFloat64("calories")
3737
bmi, _ := flags.GetFloat64("bmi")
3838

39-
bc := bodycomposition.NewBodyComposition(weight, fat, hydration, bone, boneKg, muscle, muscleKg, visceralFat, physiqueRating, metabolicAge, calories, bmi, ts)
39+
var boneMass float64
40+
var muscleMass float64
41+
42+
if bone != -1 && boneKg != -1 {
43+
cmd.PrintErrf("Cannot provide bone weight in percent and bone mass in kg! Use either of both!")
44+
os.Exit(1)
45+
}
46+
47+
if bone != -1 {
48+
boneMass = weight * bone / 100
49+
} else {
50+
boneMass = boneKg
51+
}
52+
53+
if muscle != -1 && muscleKg != -1 {
54+
cmd.PrintErrf("Cannot provide muscle weight in percent and muscle mass in kg! Use either of both!")
55+
os.Exit(1)
56+
}
57+
58+
if muscle != -1 {
59+
muscleMass = weight * muscle / 100
60+
} else {
61+
muscleMass = muscleKg
62+
}
63+
64+
bc := bodycomposition.NewBodyComposition(weight, fat, hydration, boneMass, muscleMass, visceralFat, physiqueRating, metabolicAge, calories, bmi, ts)
4065

4166
email, _ := cmd.Flags().GetString("email")
4267
password, _ := cmd.Flags().GetString("password")
@@ -71,10 +96,10 @@ func init() {
7196
flags.Float64P("weight", "w", -1, "Set your weight in kilograms")
7297
flags.Float64P("fat", "f", 0, "Set your fat in percent")
7398
flags.Float64("hydration", 0, "Set your hydration in percent")
74-
flags.Float64P("bone", "b", 0, "Set your bone mass in percent")
75-
flags.Float64("bone-mass", 0, "Set your bone mass in kilograms")
76-
flags.Float64P("muscle", "m", 0, "Set your muscle mass in percent")
77-
flags.Float64("muscle-mass", 0, "Set your muscle mass in kilograms")
99+
flags.Float64P("bone", "b", -1, "Set your bone mass in percent")
100+
flags.Float64("bone-mass", -1, "Set your bone mass in kilograms")
101+
flags.Float64P("muscle", "m", -1, "Set your muscle mass in percent")
102+
flags.Float64("muscle-mass", -1, "Set your muscle mass in kilograms")
78103
flags.Float64P("calories", "c", 0, "Set your caloric intake")
79104
flags.Float64("visceral-fat", 0, "Set your visceral fat rating (valid values: 1-60)")
80105
flags.Float64("metabolic-age", 0, "Set your metabolic age")

uploader_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestUploadWeightToGarminIntegration(t *testing.T) {
2424
t.Fatalf("Environment variable %s was not set. This is required.", garminPasswordEnvKey)
2525
}
2626

27-
var bc = NewBodyComposition(80, 14.4, 55.2, 37, 2.98, 45.5, 55, 21, 5, 23, 2250, 12.6, -1)
27+
bc := NewBodyComposition(80, 14.4, 55.2, 37, 45.5, 21, 5, 23, 2250, 12.6, -1)
2828

2929
err := Upload(email, passWord, bc)
3030

0 commit comments

Comments
 (0)