-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
195 lines (162 loc) · 4 KB
/
db.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
package usda
import (
"bufio"
"fmt"
"os"
"path"
"reflect"
"strconv"
"strings"
)
type DB struct {
FoodGroups []FoodGroup
Nutrients []NutrientDef
NutrientValues []NutrientValue
WeightValues []WeightValue
Foods map[string]*FoodDesc
}
type ParseError struct {
idx int
kind reflect.Kind
}
func (e *ParseError) Error() string {
return "usda: Parse error at position " + string(e.idx) + ", type != " + e.kind.String()
}
func (db *DB) Read(dir string) error {
db.Foods = make(map[string]*FoodDesc)
// food groups
if err := scanFile(path.Join(dir, "FD_GROUP.txt"), 2, func(parts []string) error {
foodGroup := &FoodGroup{}
if err := fillStruct(foodGroup, parts); err != nil {
return err
}
db.FoodGroups = append(db.FoodGroups, *foodGroup)
return nil
}); err != nil {
return err
}
// food descriptions
if err := scanFile(path.Join(dir, "FOOD_DES.txt"), 14, func(parts []string) error {
foodDesc := &FoodDesc{}
if err := fillStruct(foodDesc, parts); err != nil {
return err
}
db.Foods[foodDesc.FoodId] = foodDesc
return nil
}); err != nil {
return err
}
// nutrient definitions
if err := scanFile(path.Join(dir, "NUTR_DEF.txt"), 6, func(parts []string) error {
nutrientDef := &NutrientDef{}
if err := fillStruct(nutrientDef, parts); err != nil {
return err
}
db.Nutrients = append(db.Nutrients, *nutrientDef)
return nil
}); err != nil {
return err
}
// nutrient data
if err := scanFile(path.Join(dir, "NUT_DATA.txt"), 18, func(parts []string) error {
nutrientValue := &NutrientValue{}
if err := fillStruct(nutrientValue, parts); err != nil {
return err
}
db.NutrientValues = append(db.NutrientValues, *nutrientValue)
return nil
}); err != nil {
return err
}
// weight data
if err := scanFile(path.Join(dir, "WEIGHT.txt"), 7, func(parts []string) error {
weightValue := &WeightValue{}
if err := fillStruct(weightValue, parts); err != nil {
return err
}
db.WeightValues = append(db.WeightValues, *weightValue)
return nil
}); err != nil {
return err
}
return nil
}
func scanFile(path string, columns int, lf func([]string) error) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
parts, err := splitLine(line, columns)
if err != nil {
return err
}
err = lf(parts)
if err != nil {
return err
}
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}
func splitLine(line string, columns int) ([]string, error) {
parts := strings.Split(line, "^")
if len(parts) != columns {
return nil, fmt.Errorf("len(parts) = %d, expected = %d", len(parts), columns)
}
for i := 0; i < len(parts); i++ {
parts[i] = strings.Trim(parts[i], "~")
}
return parts, nil
}
func fillStruct(m interface{}, parts []string) error {
typ := reflect.TypeOf(m)
if typ.Kind() != reflect.Ptr {
return fmt.Errorf("%s must be a pointer to a struct: ", typ.Kind())
}
typ = typ.Elem()
if typ.Kind() != reflect.Struct {
return fmt.Errorf("m must be a pointer to a struct: ", typ.Kind())
}
ps := reflect.ValueOf(m)
s := ps.Elem()
for i := 0; i < s.NumField(); i++ {
tag := typ.Field(i).Tag.Get("field")
if tag == "" {
// non tagged struct field
continue
}
f := s.Field(i)
idx, err := strconv.Atoi(tag)
if err != nil {
return fmt.Errorf("usda: Field tag \"%S\" type != int", f.String())
}
if idx < 0 || len(parts) < idx {
return fmt.Errorf("usda: Field tag out of range: %s:%d", f.String(), idx)
}
if parts[idx] != "" && f.IsValid() && f.CanSet() {
switch f.Kind() {
case reflect.Int:
value, err := strconv.ParseInt(parts[idx], 10, 32)
if err != nil {
return &ParseError{idx, f.Kind()}
}
f.SetInt(value)
case reflect.String:
f.SetString(parts[idx])
case reflect.Float32:
value, err := strconv.ParseFloat(parts[idx], 32)
if err != nil {
return &ParseError{idx, f.Kind()}
}
f.SetFloat(value)
}
}
}
return nil
}