forked from svicknesh/myschoollist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
79 lines (63 loc) · 1.75 KB
/
state.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
package myschoollist
import (
"encoding/json"
"os"
"path/filepath"
"strings"
)
// State - information about a state in Malaysia
type State struct {
ID int `json:"id"`
ShortName string `json:"shortname"`
Name string `json:"name"`
ISO string `json:"iso"`
}
// states - slice of states in Malaysia and important mappings
type states struct {
items []State
mapStatesID map[int]int
mapStatesShortname map[string]int
}
// newStates - creates new instance of States with proper mapping
func newStates(jsonDir string) (statesV *states, err error) {
statesV = new(states)
file, err := os.Open(filepath.Join(jsonDir, "state.json"))
if err != nil {
return
}
defer file.Close()
err = json.NewDecoder(file).Decode(&statesV.items)
if nil != err {
return
}
statesV.mapStatesID = make(map[int]int)
statesV.mapStatesShortname = make(map[string]int)
// we map the slice index to a map by shortname for quicker access intead of looping through the slice one by one
for index, state := range statesV.items {
statesV.mapStatesID[state.ID] = index
statesV.mapStatesShortname[state.ShortName] = index
}
return
}
// GetByID - returns state information given its id
func (states *states) GetByID(id int) (state State, found bool) {
index, found := states.mapStatesID[id]
if !found {
return
}
state = states.items[index]
return
}
// GetByShortname - returns state information given its short name
func (states *states) GetByShortname(shortname string) (state State, found bool) {
index, found := states.mapStatesShortname[strings.ToUpper(shortname)]
if !found {
return
}
state = states.items[index]
return
}
// GetAll - returns all states
func (states *states) GetAll() (allStates []State) {
return states.items
}