-
Notifications
You must be signed in to change notification settings - Fork 0
/
carb.go
55 lines (48 loc) · 1.17 KB
/
carb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"fmt"
"strconv"
)
// Carb contains food Name, CarbFactor (multiply per 1g for total carbs), PerChoice (amount of food with 15g carbs)
type Carb struct {
Name string `json:"name"`
CarbFactor float64 `json:"carb-factor"`
PerChoice float64 `json:"per-choice"`
}
// Carbs slice of Carb structs
type Carbs []Carb
type Nutrient struct {
NutrientID int `json:"nutrient_id,string"`
Value float64 `json:"value,string"`
}
type NutrientAPIResponse struct {
Report struct {
Food struct {
Ndbno int `json:"ndbno,string"`
Name string `json:"name"`
Nutrients []Nutrient
}
}
}
type CarbAPIResponse struct {
ID int
Name string
CarbFactor float64
PerChoice int
}
func (n NutrientAPIResponse) transform() CarbAPIResponse {
var response CarbAPIResponse
response.Name = n.Report.Food.Name
response.ID = n.Report.Food.Ndbno
for _, v := range n.Report.Food.Nutrients {
if v.NutrientID == 205 {
var carbfactor, err = strconv.ParseFloat(fmt.Sprintf("%.2f", v.Value/100), 64)
if err != nil {
panic(err)
}
response.CarbFactor = carbfactor
response.PerChoice = int(v.Value * 15)
}
}
return response
}