2022-08-13 23:55:59 +08:00
|
|
|
|
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
|
|
|
|
|
|
|
|
|
|
|
package iplibrary
|
|
|
|
|
|
|
2022-08-14 19:42:05 +08:00
|
|
|
|
type Country struct {
|
2023-03-30 20:02:46 +08:00
|
|
|
|
Id uint16 `json:"id"`
|
2022-08-14 19:42:05 +08:00
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
Codes []string `json:"codes"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Province struct {
|
2023-03-30 20:02:46 +08:00
|
|
|
|
Id uint16 `json:"id"`
|
2022-08-14 19:42:05 +08:00
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
Codes []string `json:"codes"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type City struct {
|
2022-08-21 23:09:25 +08:00
|
|
|
|
Id uint32 `json:"id"`
|
2022-08-14 19:42:05 +08:00
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
Codes []string `json:"codes"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Town struct {
|
2022-08-21 23:09:25 +08:00
|
|
|
|
Id uint32 `json:"id"`
|
2022-08-14 19:42:05 +08:00
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
Codes []string `json:"codes"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Provider struct {
|
2023-03-30 20:02:46 +08:00
|
|
|
|
Id uint16 `json:"id"`
|
2022-08-14 19:42:05 +08:00
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
Codes []string `json:"codes"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-13 23:55:59 +08:00
|
|
|
|
type Meta struct {
|
|
|
|
|
|
Version int `json:"version"` // IP库版本
|
2022-08-23 14:54:53 +08:00
|
|
|
|
Code string `json:"code"` // 代号,用来区分不同的IP库
|
2022-08-13 23:55:59 +08:00
|
|
|
|
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"`
|
2022-08-14 19:42:05 +08:00
|
|
|
|
|
2023-03-30 20:02:46 +08:00
|
|
|
|
countryMap map[uint16]*Country // id => *Country
|
|
|
|
|
|
provinceMap map[uint16]*Province // id => *Province
|
2022-08-21 23:09:25 +08:00
|
|
|
|
cityMap map[uint32]*City // id => *City
|
|
|
|
|
|
townMap map[uint32]*Town // id => *Town
|
2023-03-30 20:02:46 +08:00
|
|
|
|
providerMap map[uint16]*Provider // id => *Provider
|
2022-08-13 23:55:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-14 19:42:05 +08:00
|
|
|
|
func (this *Meta) Init() {
|
2023-03-30 20:02:46 +08:00
|
|
|
|
this.countryMap = map[uint16]*Country{}
|
|
|
|
|
|
this.provinceMap = map[uint16]*Province{}
|
2022-08-21 23:09:25 +08:00
|
|
|
|
this.cityMap = map[uint32]*City{}
|
|
|
|
|
|
this.townMap = map[uint32]*Town{}
|
2023-03-30 20:02:46 +08:00
|
|
|
|
this.providerMap = map[uint16]*Provider{}
|
2022-08-14 19:42:05 +08:00
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
2022-08-13 23:55:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-30 20:02:46 +08:00
|
|
|
|
func (this *Meta) CountryWithId(countryId uint16) *Country {
|
2022-08-14 19:42:05 +08:00
|
|
|
|
return this.countryMap[countryId]
|
2022-08-13 23:55:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-30 20:02:46 +08:00
|
|
|
|
func (this *Meta) ProvinceWithId(provinceId uint16) *Province {
|
2022-08-14 19:42:05 +08:00
|
|
|
|
return this.provinceMap[provinceId]
|
2022-08-13 23:55:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-21 23:09:25 +08:00
|
|
|
|
func (this *Meta) CityWithId(cityId uint32) *City {
|
2022-08-14 19:42:05 +08:00
|
|
|
|
return this.cityMap[cityId]
|
2022-08-13 23:55:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-21 23:09:25 +08:00
|
|
|
|
func (this *Meta) TownWithId(townId uint32) *Town {
|
2022-08-14 19:42:05 +08:00
|
|
|
|
return this.townMap[townId]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-30 20:02:46 +08:00
|
|
|
|
func (this *Meta) ProviderWithId(providerId uint16) *Provider {
|
2022-08-14 19:42:05 +08:00
|
|
|
|
return this.providerMap[providerId]
|
2022-08-13 23:55:59 +08:00
|
|
|
|
}
|