-
Notifications
You must be signed in to change notification settings - Fork 18
/
meta.go
95 lines (80 loc) · 2.44 KB
/
meta.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
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package iplibrary
type Country struct {
Id uint16 `json:"id"`
Name string `json:"name"`
Codes []string `json:"codes"`
}
type Province struct {
Id uint16 `json:"id"`
Name string `json:"name"`
Codes []string `json:"codes"`
}
type City struct {
Id uint32 `json:"id"`
Name string `json:"name"`
Codes []string `json:"codes"`
}
type Town struct {
Id uint32 `json:"id"`
Name string `json:"name"`
Codes []string `json:"codes"`
}
type Provider struct {
Id uint16 `json:"id"`
Name string `json:"name"`
Codes []string `json:"codes"`
}
type Meta struct {
Version int `json:"version"` // IP库版本
Code string `json:"code"` // 代号,用来区分不同的IP库
Author string `json:"author"`
Countries []*Country `json:"countries"`
Provinces []*Province `json:"provinces"`
Cities []*City `json:"cities"`
Towns []*Town `json:"towns"`
Providers []*Provider `json:"providers"`
CreatedAt int64 `json:"createdAt"`
countryMap map[uint16]*Country // id => *Country
provinceMap map[uint16]*Province // id => *Province
cityMap map[uint32]*City // id => *City
townMap map[uint32]*Town // id => *Town
providerMap map[uint16]*Provider // id => *Provider
}
func (this *Meta) Init() {
this.countryMap = map[uint16]*Country{}
this.provinceMap = map[uint16]*Province{}
this.cityMap = map[uint32]*City{}
this.townMap = map[uint32]*Town{}
this.providerMap = map[uint16]*Provider{}
for _, country := range this.Countries {
this.countryMap[country.Id] = country
}
for _, province := range this.Provinces {
this.provinceMap[province.Id] = province
}
for _, city := range this.Cities {
this.cityMap[city.Id] = city
}
for _, town := range this.Towns {
this.townMap[town.Id] = town
}
for _, provider := range this.Providers {
this.providerMap[provider.Id] = provider
}
}
func (this *Meta) CountryWithId(countryId uint16) *Country {
return this.countryMap[countryId]
}
func (this *Meta) ProvinceWithId(provinceId uint16) *Province {
return this.provinceMap[provinceId]
}
func (this *Meta) CityWithId(cityId uint32) *City {
return this.cityMap[cityId]
}
func (this *Meta) TownWithId(townId uint32) *Town {
return this.townMap[townId]
}
func (this *Meta) ProviderWithId(providerId uint16) *Provider {
return this.providerMap[providerId]
}