-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse.go
713 lines (653 loc) · 16.5 KB
/
parse.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
package gocronometer
import (
"encoding/csv"
"fmt"
"io"
"strconv"
"strings"
"time"
)
type ServingRecord struct {
RecordedTime time.Time
Group string
FoodName string
QuantityValue float64
QuantityUnits string
EnergyKcal float64
CaffeineMg float64
WaterG float64
B1Mg float64
B2Mg float64
B3Mg float64
B5Mg float64
B6Mg float64
B12Mg float64
BiotinUg float64
CholineMg float64
FolateUg float64
VitaminAUI float64
VitaminCMg float64
VitaminDUI float64
VitaminEMg float64
VitaminKMg float64
CalciumMg float64
ChromiumUg float64
CopperMg float64
FluorideUg float64
IodineUg float64
MagnesiumMg float64
ManganeseMg float64
PhosphorusMg float64
PotassiumMg float64
SeleniumUg float64
SodiumMg float64
ZincMg float64
CarbsG float64
FiberG float64
FructoseG float64
GalactoseG float64
GlucoseG float64
LactoseG float64
MaltoseG float64
StarchG float64
SucroseG float64
SugarsG float64
NetCarbsG float64
FatG float64
CholesterolMg float64
MonounsaturatedG float64
PolyunsaturatedG float64
SaturatedG float64
TransFatG float64
Omega3G float64
Omega6G float64
CystineG float64
HistidineG float64
IsoleucineG float64
LeucineG float64
LysineG float64
MethionineG float64
PhenylalanineG float64
ThreonineG float64
TryptophanG float64
TyrosineG float64
ValineG float64
ProtienG float64
IronMg float64
Category string
}
type ServingRecords []ServingRecord
type ServingsExport struct {
Records ServingRecords
}
func ParseServingsExport(rawCSVReader io.Reader, location *time.Location) (ServingRecords, error) {
r := csv.NewReader(rawCSVReader)
lineNum := 0
headers := make(map[int]string)
servings := make(ServingRecords, 0, 0)
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
// Index all the headers.
if lineNum == 0 {
for i, v := range record {
headers[i] = v
}
lineNum++
continue
}
lineNum++
var date string
var timeStr string
serving := ServingRecord{}
for i, v := range record {
columnName := headers[i]
switch columnName {
case "Day":
date = v
case "Time":
timeStr = v
case "Group":
serving.Group = v
case "Food Name":
serving.FoodName = v
case "Amount":
s := strings.Split(v, " ")
quantityValue, err := parseFloat(s[0], 64)
if err != nil {
return nil, fmt.Errorf("parsing quantity value: %s", err)
}
serving.QuantityValue = quantityValue
serving.QuantityUnits = strings.Join(s[1:], "")
case "Energy (kcal)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing energy: %s", err)
}
serving.EnergyKcal = f
case "Caffeine (mg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing caffeine: %s", err)
}
serving.CaffeineMg = f
case "Water (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing water: %s", err)
}
serving.WaterG = f
case "B1 (Thiamine) (mg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing b1: %s", err)
}
serving.B1Mg = f
case "B2 (Riboflavin) (mg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing b2: %s", err)
}
serving.B2Mg = f
case "B3 (Niacin) (mg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing b3: %s", err)
}
serving.B3Mg = f
case "B5 (Pantothenic Acid) (mg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing b5: %s", err)
}
serving.B5Mg = f
case "B6 (Pyridoxine) (mg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing b6: %s", err)
}
serving.B6Mg = f
case "B12 (Cobalamin) (µg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing b12: %s", err)
}
serving.B12Mg = f
case "Biotin (µg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing biotin: %s", err)
}
serving.BiotinUg = f
case "Choline (mg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing choline: %s", err)
}
serving.CholineMg = f
case "Folate (µg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing folate: %s", err)
}
serving.FolateUg = f
case "Vitamin A (IU)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman a: %s", err)
}
serving.VitaminAUI = f
case "Vitamin C (mg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.VitaminCMg = f
case "Vitamin D (IU)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.VitaminDUI = f
case "Vitamin E (mg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.VitaminEMg = f
case "Vitamin K (µg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.VitaminKMg = f
case "Calcium (mg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.CalciumMg = f
case "Chromium (µg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.ChromiumUg = f
case "Copper (mg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.CopperMg = f
case "Fluoride (µg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.FluorideUg = f
case "Iodine (µg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.IodineUg = f
case "Iron (mg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.IronMg = f
case "Magnesium (mg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.MagnesiumMg = f
case "Manganese (mg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.ManganeseMg = f
case "Phosphorus (mg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.PhosphorusMg = f
case "Potassium (mg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.PotassiumMg = f
case "Selenium (µg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.SeleniumUg = f
case "Sodium (mg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.SodiumMg = f
case "Zinc (mg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.ZincMg = f
case "Carbs (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.CarbsG = f
case "Fiber (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.FiberG = f
case "Fructose (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.FructoseG = f
case "Galactose (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.GalactoseG = f
case "Glucose (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.GlucoseG = f
case "Lactose (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.LactoseG = f
case "Maltose (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.MaltoseG = f
case "Starch (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.StarchG = f
case "Sucrose (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.SucroseG = f
case "Sugars (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.SugarsG = f
case "Net Carbs (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.NetCarbsG = f
case "Fat (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.FatG = f
case "Cholesterol (mg)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.CholesterolMg = f
case "Monounsaturated (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.MonounsaturatedG = f
case "Polyunsaturated (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.PolyunsaturatedG = f
case "Saturated (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.SaturatedG = f
case "Trans-Fats (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.TransFatG = f
case "Omega-3 (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.Omega3G = f
case "Omega-6 (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.Omega6G = f
case "Cystine (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.CystineG = f
case "Histidine (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.HistidineG = f
case "Isoleucine (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.IsoleucineG = f
case "Leucine (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.LeucineG = f
case "Lysine (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.LysineG = f
case "Methionine (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.MethionineG = f
case "Phenylalanine (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.PhenylalanineG = f
case "Protein (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.ProtienG = f
case "Threonine (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.ThreonineG = f
case "Tryptophan (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.TryptophanG = f
case "Tyrosine (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.TyrosineG = f
case "Valine (g)":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing vitiman c: %s", err)
}
serving.ValineG = f
case "Category":
serving.Category = v
}
}
if timeStr == "" {
timeStr = "00:00 AM"
}
if location == nil {
location = time.UTC
}
serving.RecordedTime, err = time.ParseInLocation("2006-01-02 15:04 PM", date+" "+timeStr, location)
if err != nil {
return nil, fmt.Errorf("parsing record time: %s", err)
}
servings = append(servings, serving)
}
return servings, nil
}
// parseFloat wraps time.ParseFloat but interprites an empty string as 0.
func parseFloat(s string, bitSize int) (float64, error) {
if s == "" {
return 0, nil
}
return strconv.ParseFloat(s, bitSize)
}
type ExerciseRecord struct {
RecordedTime time.Time
Exercise string
Minutes float64
CaloriesBurned float64
}
type ExerciseRecords []ExerciseRecord
func ParseExerciseExport(rawCSVReader io.Reader, location *time.Location) (ExerciseRecords, error) {
r := csv.NewReader(rawCSVReader)
lineNum := 0
headers := make(map[int]string)
exercises := make(ExerciseRecords, 0, 0)
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
// Index all the headers.
if lineNum == 0 {
for i, v := range record {
headers[i] = v
}
lineNum++
continue
}
lineNum++
var date string
var timeStr string
exercise := ExerciseRecord{}
for i, v := range record {
columnName := headers[i]
switch columnName {
case "Day":
date = v
case "Time":
timeStr = v
case "Exercise":
exercise.Exercise = v
case "Minutes":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing energy: %s", err)
}
exercise.Minutes = f
case "Calories Burned":
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing caffeine: %s", err)
}
exercise.CaloriesBurned = f
}
}
if timeStr == "" {
timeStr = "00:00 AM"
}
if location == nil {
location = time.UTC
}
exercise.RecordedTime, err = time.ParseInLocation("2006-01-02 15:04 PM", date+" "+timeStr, location)
if err != nil {
return nil, fmt.Errorf("parsing record time: %s", err)
}
exercises = append(exercises, exercise)
}
return exercises, nil
}
type BiometricRecord struct {
RecordedTime time.Time
Metric string
Unit string
Amount float64
}
type BiometricRecords []BiometricRecord
func ParseBiometricRecordsExport(rawCSVReader io.Reader, location *time.Location) (BiometricRecords, error) {
r := csv.NewReader(rawCSVReader)
lineNum := 0
headers := make(map[int]string)
records := make(BiometricRecords, 0, 0)
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
// Index all the headers.
if lineNum == 0 {
for i, v := range record {
headers[i] = v
}
lineNum++
continue
}
lineNum++
var date string
var timeStr string
bioRecord := BiometricRecord{}
for i, v := range record {
columnName := headers[i]
switch columnName {
case "Day":
date = v
case "Time":
timeStr = v
case "Metric":
bioRecord.Metric = v
case "Unit":
bioRecord.Unit = v
case "Amount":
if !strings.Contains(v, "/") {
f, err := parseFloat(v, 64)
if err != nil {
return nil, fmt.Errorf("parsing energy: %s", err)
}
bioRecord.Amount = f
}
}
}
if timeStr == "" {
timeStr = "00:00 AM"
}
if location == nil {
location = time.UTC
}
bioRecord.RecordedTime, err = time.ParseInLocation("2006-01-02 15:04 PM", date+" "+timeStr, location)
if err != nil {
return nil, fmt.Errorf("parsing record time: %s", err)
}
records = append(records, bioRecord)
}
return records, nil
}