-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecipe.go
114 lines (105 loc) · 2.44 KB
/
recipe.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
package main
import (
"encoding/json"
"strings"
)
// constants in map form
var (
Meals = map[int]string{
1: "Breakfast",
2: "Lunch",
4: "Dinner",
}
MealsToInt = map[string]int{
"Breakfast": 1,
"Lunch": 2,
"Dinner": 4,
}
Seasons = map[int]string{
1: "Spring",
2: "Summer",
4: "Winter",
8: "Fall",
}
SeasonsToInt = map[string]int{
"Spring": 1,
"Summer": 2,
"Winter": 4,
"Fall": 8,
}
)
// Recipe represents a recipe.
type Recipe struct {
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Cuisine int `json:"cuisine"`
Mealtype int `json:"mealtype"`
Season int `json:"season"`
Ingredientlist string `json:"ingredientlist"`
Instructions string `json:"instructions"`
Picture []byte `json:"picture"`
}
// ToJSON turns a Recipe into a JSON string
func (recipe *Recipe) ToJSON() (result string) {
resultBytes, err := json.Marshal(recipe)
if err != nil {
result = "Chosen recipe cannot be formatted into json form"
} else {
result = string(resultBytes)
}
return
}
// ParseIngredients returns a list of ingredients from a string with
// delimiter ;
func ParseIngredients(ingredients string) []string {
things := strings.Split(ingredients, ";")
for k, v := range things {
things[k] = strings.TrimSpace(v)
}
return things
}
// ParseMealtype returns a map of whether or not the number
// represents a particular mealtype.
// This map is a map from meal to bool (t/f)
func ParseMealtype(mealtype int) map[string]bool {
result := make(map[string]bool)
for meal, name := range Meals {
if meal&mealtype > 0 {
result[name] = true
} else {
result[name] = false
}
}
return result
}
// ParseSeason returns a map of whether or not the number
// represents a particular season.
// The map maps seasons (string) to bool
func ParseSeason(season int) map[string]bool {
result := make(map[string]bool)
for s, name := range Seasons {
if s&season > 0 {
result[name] = true
} else {
result[name] = false
}
}
return result
}
// EncodeMealtype will encode a mealtype int
// based on the meals present.
func EncodeMealtype(meals []string) (mealtype int) {
for _, m := range meals {
mealtype += MealsToInt[m]
}
return
}
// EncodeSeason will encode a season int
// based on the seasons present.
func EncodeSeason(seasons []string) (season int) {
for _, s := range seasons {
season += SeasonsToInt[s]
}
return
}