-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefecturejp.go
161 lines (142 loc) · 4.58 KB
/
prefecturejp.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
package prefecturejp
// Prefectures contains a list of prefecture.
type Prefectures []Prefecture
// Prefecture which defines codes for the names of the principal subdivisions
// (e.g.provinces or states) of all countries coded in ISO 3166-1.
//
// See: https://en.wikipedia.org/wiki/ISO_3166-2:JP
type Prefecture struct {
// The standard code, like: JP-01, JP-02, etc
Code string `json:"code"`
// The english name, like: Hokkaido, Aomori etc
Name string `json:"name"`
// The japanese name, like: 北海道, 岩手県 etc
Japanese string `json:"japanese"`
// The short name of japanese, like: 北海道, 青森 etc.
ShortName string `json:"short_name"`
Region string `json:"region"`
}
var codeIndexMap = map[string]int{}
var nameIndexMap = map[string]int{}
var japaneseIndexMap = map[string]int{}
var shortNameIndexMap = map[string]int{}
var regionIndexMap = map[string]int{}
func init() {
for i, prefecture := range prefectureData {
codeIndexMap[prefecture.Code] = i
nameIndexMap[prefecture.Name] = i
japaneseIndexMap[prefecture.Japanese] = i
shortNameIndexMap[prefecture.ShortName] = i
regionIndexMap[prefecture.Region] = i
}
}
// GetPrefectures return the Japan prefecture list.
func GetPrefectures() Prefectures {
return prefectureData
}
// GetPrefecturesCode return all the `Code`s of the Prefectures.
func GetPrefecturesCode() []string {
prefectureCode := []string{}
for _, prefecture := range prefectureData {
prefectureCode = append(prefectureCode, prefecture.Code)
}
return prefectureCode
}
// GetPrefectureJapaneses return all the `Japanese`s of the Prefectures.
func GetPrefectureJapaneses() []string {
prefectureJapaneses := []string{}
for _, prefecture := range prefectureData {
prefectureJapaneses = append(prefectureJapaneses, prefecture.Japanese)
}
return prefectureJapaneses
}
// GetPrefectureNames return the all the `Name`s of the Prefectures.
func GetPrefectureNames() []string {
prefectureNames := []string{}
for _, prefecture := range prefectureData {
prefectureNames = append(prefectureNames, prefecture.Name)
}
return prefectureNames
}
// GetPrefectureShortNames return all the `ShortName`s of the Prefectures.
func GetPrefectureShortNames() []string {
prefectureShortNames := []string{}
for _, prefecture := range prefectureData {
prefectureShortNames = append(prefectureShortNames, prefecture.ShortName)
}
return prefectureShortNames
}
// GetPrefecturesJSON is a wrapper helper to jsonhandlerfunc.
func GetPrefecturesJSON() (Prefectures, error) {
return GetPrefectures(), nil
}
// GetJapaneseByName returns the `Japanese` by the given `Name`.
func GetJapaneseByName(name string) (result string) {
prefecture := QueryPrefecture(PrefectureField_NAME, name)
if prefecture == nil {
return ""
}
return prefecture.Japanese
}
// GetShortNameByName returns the `ShortName` by the given `Name`.
func GetShortNameByName(name string) (result string) {
prefecture := QueryPrefecture(PrefectureField_NAME, name)
if prefecture == nil {
return ""
}
return prefecture.ShortName
}
// GetNameByJapanese returns the `Name` by the given `Japanese`.
func GetNameByJapanese(japanese string) (result string) {
prefecture := QueryPrefecture(PrefectureField_JAPANESE, japanese)
if prefecture == nil {
return ""
}
return prefecture.Name
}
// GetNameByShortName returns the `Name` by the given `ShortName`.
func GetNameByShortName(shortName string) (result string) {
prefecture := QueryPrefecture(PrefectureField_SHORT_NAME, shortName)
if prefecture == nil {
return ""
}
return prefecture.Name
}
// GetCodeByJapanese returns the `Code` by given `Japanese`
func GetCodeByJapanese(japanese string) (result string) {
prefecture := QueryPrefecture(PrefectureField_JAPANESE, japanese)
if prefecture == nil {
return ""
}
return prefecture.Code
}
func QueryPrefecture(field PrefectureField, value string) *Prefecture {
var prefectureIndex int
var isFound bool
switch field {
case PrefectureField_CODE:
prefectureIndex, isFound = codeIndexMap[value]
case PrefectureField_NAME:
prefectureIndex, isFound = nameIndexMap[value]
case PrefectureField_JAPANESE:
prefectureIndex, isFound = japaneseIndexMap[value]
case PrefectureField_SHORT_NAME:
prefectureIndex, isFound = shortNameIndexMap[value]
case PrefectureField_REGION:
prefectureIndex, isFound = regionIndexMap[value]
}
if !isFound {
return nil
}
newPrefecture := prefectureData[prefectureIndex]
return &newPrefecture
}
func QueryPrefectureByFields(fields []PrefectureField, value string) *Prefecture {
for _, f := range fields {
pref := QueryPrefecture(f, value)
if pref != nil {
return pref
}
}
return nil
}