This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjp_prefecture.go
364 lines (321 loc) · 12.8 KB
/
jp_prefecture.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
package jp_prefecture
import (
"strings"
)
const (
JISCodeHokkaido = 1
JISCodeAomori = 2
JISCodeIwate = 3
JISCodeMiyagi = 4
JISCodeAkita = 5
JISCodeYamagata = 6
JISCodeFukushima = 7
JISCodeIbaraki = 8
JISCodeTochigi = 9
JISCodeGunma = 10
JISCodeSaitama = 11
JISCodeChiba = 12
JISCodeTokyo = 13
JISCodeKanagawa = 14
JISCodeNiigata = 15
JISCodeToyama = 16
JISCodeIshikawa = 17
JISCodeFukui = 18
JISCodeYamanashi = 19
JISCodeNagano = 20
JISCodeGifu = 21
JISCodeShizuoka = 22
JISCodeAichi = 23
JISCodeMie = 24
JISCodeShiga = 25
JISCodeKyoto = 26
JISCodeOsaka = 27
JISCodeHyogo = 28
JISCodeNara = 29
JISCodeWakayama = 30
JISCodeTottori = 31
JISCodeShimane = 32
JISCodeOkayama = 33
JISCodeHiroshima = 34
JISCodeYamaguchi = 35
JISCodeTokushima = 36
JISCodeKagawa = 37
JISCodeEhime = 38
JISCodeKochi = 39
JISCodeFukuoka = 40
JISCodeSaga = 41
JISCodeNagasaki = 42
JISCodeKumamoto = 43
JISCodeOita = 44
JISCodeMiyazaki = 45
JISCodeKagoshima = 46
JISCodeOkinawa = 47
)
const (
prefectureSize = 47
findMapKeyKanji = 0
findMayKeyKana = 1
findMapKeyRoma = 2
)
// prefectureMap は都道府県の情報を”コード:名称のMap(漢字、かな、ローマ字)”形式で定義した値です。
var prefectureMap = map[int]*prefecture{
JISCodeHokkaido: {code: JISCodeHokkaido, kanji: "北海道", kana: "ほっかいどう", roma: "hokkaido"},
JISCodeAomori: {code: JISCodeAomori, kanji: "青森県", kana: "あおもりけん", roma: "aomori-ken"},
JISCodeIwate: {code: JISCodeIwate, kanji: "岩手県", kana: "いわてけん", roma: "iwate-ken"},
JISCodeMiyagi: {code: JISCodeMiyagi, kanji: "宮城県", kana: "みやぎけん", roma: "miyagi-ken"},
JISCodeAkita: {code: JISCodeAkita, kanji: "秋田県", kana: "あきたけん", roma: "akita-ken"},
JISCodeYamagata: {code: JISCodeYamagata, kanji: "山形県", kana: "やまがたけん", roma: "yamagata-ken"},
JISCodeFukushima: {code: JISCodeFukushima, kanji: "福島県", kana: "ふくしまけん", roma: "fukushima-ken"},
JISCodeIbaraki: {code: JISCodeIbaraki, kanji: "茨城県", kana: "いばらきけん", roma: "ibaraki-ken"},
JISCodeTochigi: {code: JISCodeTochigi, kanji: "栃木県", kana: "とちぎけん", roma: "tochigi-ken"},
JISCodeGunma: {code: JISCodeGunma, kanji: "群馬県", kana: "ぐんまけん", roma: "gunma-ken"},
JISCodeSaitama: {code: JISCodeSaitama, kanji: "埼玉県", kana: "さいたまけん", roma: "saitama-ken"},
JISCodeChiba: {code: JISCodeChiba, kanji: "千葉県", kana: "ちばけん", roma: "chiba-ken"},
JISCodeTokyo: {code: JISCodeTokyo, kanji: "東京都", kana: "とうきょうと", roma: "tokyo-to"},
JISCodeKanagawa: {code: JISCodeKanagawa, kanji: "神奈川県", kana: "かながわけん", roma: "kanagawa-ken"},
JISCodeNiigata: {code: JISCodeNiigata, kanji: "新潟県", kana: "にいがたけん", roma: "niigata-ken"},
JISCodeToyama: {code: JISCodeToyama, kanji: "富山県", kana: "とやまけん", roma: "toyama-ken"},
JISCodeIshikawa: {code: JISCodeIshikawa, kanji: "石川県", kana: "いしかわけん", roma: "ishikawa-ken"},
JISCodeFukui: {code: JISCodeFukui, kanji: "福井県", kana: "ふくいけん", roma: "fukui-ken"},
JISCodeYamanashi: {code: JISCodeYamanashi, kanji: "山梨県", kana: "やまなしけん", roma: "yamanashi-ken"},
JISCodeNagano: {code: JISCodeNagano, kanji: "長野県", kana: "ながのけん", roma: "nagano-ken"},
JISCodeGifu: {code: JISCodeGifu, kanji: "岐阜県", kana: "ぎふけん", roma: "gifu-ken"},
JISCodeShizuoka: {code: JISCodeShizuoka, kanji: "静岡県", kana: "しずおかけん", roma: "shizuoka-ken"},
JISCodeAichi: {code: JISCodeAichi, kanji: "愛知県", kana: "あいちけん", roma: "aichi-ken"},
JISCodeMie: {code: JISCodeMie, kanji: "三重県", kana: "みえけん", roma: "mie-ken"},
JISCodeShiga: {code: JISCodeShiga, kanji: "滋賀県", kana: "しがけん", roma: "shiga-ken"},
JISCodeKyoto: {code: JISCodeKyoto, kanji: "京都府", kana: "きょうとふ", roma: "kyoto-fu"},
JISCodeOsaka: {code: JISCodeOsaka, kanji: "大阪府", kana: "おおさかふ", roma: "osaka-fu"},
JISCodeHyogo: {code: JISCodeHyogo, kanji: "兵庫県", kana: "ひょうごけん", roma: "hyogo-ken"},
JISCodeNara: {code: JISCodeNara, kanji: "奈良県", kana: "ならけん", roma: "nara-ken"},
JISCodeWakayama: {code: JISCodeWakayama, kanji: "和歌山県", kana: "わかやまけん", roma: "wakayama-ken"},
JISCodeTottori: {code: JISCodeTottori, kanji: "鳥取県", kana: "とっとりけん", roma: "tottori-ken"},
JISCodeShimane: {code: JISCodeShimane, kanji: "島根県", kana: "しまねけん", roma: "shimane-ken"},
JISCodeOkayama: {code: JISCodeOkayama, kanji: "岡山県", kana: "おかやまけん", roma: "okayama-ken"},
JISCodeHiroshima: {code: JISCodeHiroshima, kanji: "広島県", kana: "ひろしまけん", roma: "hiroshima-ken"},
JISCodeYamaguchi: {code: JISCodeYamaguchi, kanji: "山口県", kana: "やまぐちけん", roma: "yamaguchi-ken"},
JISCodeTokushima: {code: JISCodeTokushima, kanji: "徳島県", kana: "とくしまけん", roma: "tokushima-ken"},
JISCodeKagawa: {code: JISCodeKagawa, kanji: "香川県", kana: "かがわけん", roma: "kagawa-ken"},
JISCodeEhime: {code: JISCodeEhime, kanji: "愛媛県", kana: "えひめけん", roma: "ehime-ken"},
JISCodeKochi: {code: JISCodeKochi, kanji: "高知県", kana: "こうちけん", roma: "kochi-ken"},
JISCodeFukuoka: {code: JISCodeFukuoka, kanji: "福岡県", kana: "ふくおかけん", roma: "fukuoka-ken"},
JISCodeSaga: {code: JISCodeSaga, kanji: "佐賀県", kana: "さがけん", roma: "saga-ken"},
JISCodeNagasaki: {code: JISCodeNagasaki, kanji: "長崎県", kana: "ながさきけん", roma: "nagasaki-ken"},
JISCodeKumamoto: {code: JISCodeKumamoto, kanji: "熊本県", kana: "くまもとけん", roma: "kumamoto-ken"},
JISCodeOita: {code: JISCodeOita, kanji: "大分県", kana: "おおいたけん", roma: "oita-ken"},
JISCodeMiyazaki: {code: JISCodeMiyazaki, kanji: "宮崎県", kana: "みやざきけん", roma: "miyazaki-ken"},
JISCodeKagoshima: {code: JISCodeKagoshima, kanji: "鹿児島県", kana: "かごしまけん", roma: "kagoshima-ken"},
JISCodeOkinawa: {code: JISCodeOkinawa, kanji: "沖縄県", kana: "おきなわけん", roma: "okinawa-ken"},
}
// hokkaido は北海道地方の情報を定義した値です。
var hokkaido = ®ion{
kanji: "北海道",
kana: "ほっかいどう",
roma: "hokkaidou",
prefectureCodes: []int{
JISCodeHokkaido,
},
}
// tohoku は東北地方の情報を定義した値です。
var tohoku = ®ion{
kanji: "東北",
kana: "とうほく",
roma: "tohoku",
prefectureCodes: []int{
JISCodeAomori,
JISCodeIwate,
JISCodeMiyagi,
JISCodeAkita,
JISCodeYamagata,
JISCodeFukushima,
},
}
// kanto は関東地方の情報を定義した値です。
var kanto = ®ion{
kanji: "関東",
kana: "かんとう",
roma: "kanto",
prefectureCodes: []int{
JISCodeIbaraki,
JISCodeTochigi,
JISCodeGunma,
JISCodeSaitama,
JISCodeChiba,
JISCodeTokyo,
JISCodeKanagawa,
},
}
// chubu は中部地方の情報を定義した値です。
var chubu = ®ion{
kanji: "中部",
kana: "ちゅうぶ",
roma: "chubu",
prefectureCodes: []int{
JISCodeNiigata,
JISCodeToyama,
JISCodeIshikawa,
JISCodeFukui,
JISCodeYamanashi,
JISCodeNagano,
JISCodeGifu,
JISCodeShizuoka,
JISCodeAichi,
},
}
// kansai は関西地方の情報を定義した値です。
var kansai = ®ion{
kanji: "関西",
kana: "かんさい",
roma: "kansai",
prefectureCodes: []int{
JISCodeMie,
JISCodeShiga,
JISCodeKyoto,
JISCodeOsaka,
JISCodeHyogo,
JISCodeNara,
JISCodeWakayama,
},
}
// chugoku は中国地方の情報を定義した値です。
var chugoku = ®ion{
kanji: "中国",
kana: "ちゅうごく",
roma: "chugoku",
prefectureCodes: []int{
JISCodeTottori,
JISCodeShimane,
JISCodeOkayama,
JISCodeHiroshima,
JISCodeYamaguchi,
},
}
// shikoku は四国地方の情報を定義した値です。
var shikoku = ®ion{
kanji: "四国",
kana: "しこく",
roma: "shikoku",
prefectureCodes: []int{
JISCodeTokushima,
JISCodeKagawa,
JISCodeEhime,
JISCodeKochi,
},
}
// kyushu は九州地方の情報を定義した値です。
var kyushu = ®ion{
kanji: "九州",
kana: "きゅうしゅう",
roma: "kyushu",
prefectureCodes: []int{
JISCodeFukuoka,
JISCodeSaga,
JISCodeNagasaki,
JISCodeKumamoto,
JISCodeOita,
JISCodeMiyazaki,
JISCodeKagoshima,
JISCodeOkinawa,
},
}
// nameFindMap は漢字、かな、ローマ字で都道府県情報を検索しやすくするためのインデックスマップです。
var nameFindMap = func() map[uint8]map[string]*prefecture {
findMap := map[uint8]map[string]*prefecture{
findMapKeyKanji: make(map[string]*prefecture, prefectureSize),
findMayKeyKana: make(map[string]*prefecture, prefectureSize),
findMapKeyRoma: make(map[string]*prefecture, prefectureSize),
}
for code, prefecture := range prefectureMap {
findMap[findMapKeyKanji][prefecture.kanji] = prefecture
findMap[findMayKeyKana][prefecture.kana] = prefecture
findMap[findMapKeyRoma][prefecture.roma] = prefecture
switch code {
case JISCodeHokkaido:
// 北海道は短縮名が存在しないので処理をスキップする
continue
case JISCodeTokyo:
kanjiIndex := strings.TrimSuffix(prefecture.kanji, "都")
findMap[findMapKeyKanji][kanjiIndex] = prefecture
kanaIndex := strings.TrimSuffix(prefecture.kana, "と")
findMap[findMayKeyKana][kanaIndex] = prefecture
romaIndex := strings.TrimSuffix(prefecture.roma, "-to")
findMap[findMapKeyRoma][romaIndex] = prefecture
case JISCodeKyoto, JISCodeOsaka:
kanjiIndex := strings.TrimSuffix(prefecture.kanji, "府")
findMap[findMapKeyKanji][kanjiIndex] = prefecture
kanaIndex := strings.TrimSuffix(prefecture.kana, "ふ")
findMap[findMayKeyKana][kanaIndex] = prefecture
romaIndex := strings.TrimSuffix(prefecture.roma, "-fu")
findMap[findMapKeyRoma][romaIndex] = prefecture
default:
kanjiIndex := strings.TrimSuffix(prefecture.kanji, "県")
findMap[findMapKeyKanji][kanjiIndex] = prefecture
kanaIndex := strings.TrimSuffix(prefecture.kana, "けん")
findMap[findMayKeyKana][kanaIndex] = prefecture
romaIndex := strings.TrimSuffix(prefecture.roma, "-ken")
findMap[findMapKeyRoma][romaIndex] = prefecture
}
}
return findMap
}()
// List は全ての都道府県情報を返します。
func List() []Prefecture {
var prefectures = make([]Prefecture, prefectureSize)
for code, prefecture := range prefectureMap {
prefectures[code-1] = prefecture
}
return prefectures
}
// RegionList は地域のリストを返します。
func RegionList() []Region {
return []Region{
hokkaido,
tohoku,
kanto,
chubu,
kansai,
chugoku,
shikoku,
kyushu,
}
}
// FindByCode は与えた都道府県コードに対応する都道府県情報を返します。
// 対応する都道府県情報が見つからない場合、`ok`に`false`が設定され、都道府県情報は`nil`で返されます。
func FindByCode(code int) (Prefecture, bool) {
prefecture, ok := prefectureMap[code]
if !ok {
return nil, false
}
return prefecture, true
}
// FindByKanji は与えた都道府県の漢字名に対応する都道府県情報を返します。
// 与える値の末尾にある「都」、「府」、「県」は省略可能です。
// 対応する都道府県情報が見つからない場合、`ok`に`false`が設定され、都道府県情報は`nil`で返されます。
func FindByKanji(kanji string) (Prefecture, bool) {
prefecture, ok := nameFindMap[findMapKeyKanji][kanji]
if !ok {
return nil, false
}
return prefecture, true
}
// FindByKana は与えた都道府県のかな名に対応する都道府県情報を返します。
// 与える値の末尾にある「と」、「ふ」、「けん」は省略可能です。
// 対応する都道府県情報が見つからない場合、`ok`に`false`が設定され、都道府県情報は`nil`で返されます。
func FindByKana(kana string) (Prefecture, bool) {
prefecture, ok := nameFindMap[findMayKeyKana][kana]
if !ok {
return nil, false
}
return prefecture, true
}
// FindByRoma は与えた都道府県のローマ字名に対応する都道府県情報を返します。
// 与える値の末尾にある「-to」、「-fu」、「-ken」は省略可能です。
// 与える値はUpperCamelCase、LowerCamelCaseを問いません。
// 対応する都道府県情報が見つからない場合、`ok`に`false`が設定され、都道府県情報は`nil`で返されます。
func FindByRoma(roma string) (Prefecture, bool) {
prefecture, ok := nameFindMap[findMapKeyRoma][strings.ToLower(roma)]
if !ok {
return nil, false
}
return prefecture, true
}