city.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package ipdb
  2. import (
  3. "reflect"
  4. "time"
  5. "os"
  6. )
  7. // CityInfo is City Database Content
  8. type CityInfo struct {
  9. CountryName string `json:"country_name"`
  10. RegionName string `json:"region_name"`
  11. CityName string `json:"city_name"`
  12. OwnerDomain string `json:"owner_domain"`
  13. IspDomain string `json:"isp_domain"`
  14. Latitude string `json:"latitude"`
  15. Longitude string `json:"longitude"`
  16. Timezone string `json:"timezone"`
  17. UtcOffset string `json:"utc_offset"`
  18. ChinaAdminCode string `json:"china_admin_code"`
  19. IddCode string `json:"idd_code"`
  20. CountryCode string `json:"country_code"`
  21. ContinentCode string `json:"continent_code"`
  22. IDC string `json:"idc"`
  23. BaseStation string `json:"base_station"`
  24. CountryCode3 string `json:"country_code3"`
  25. EuropeanUnion string `json:"european_union"`
  26. CurrencyCode string `json:"currency_code"`
  27. CurrencyName string `json:"currency_name"`
  28. Anycast string `json:"anycast"`
  29. }
  30. // City struct
  31. type City struct {
  32. reader *reader
  33. }
  34. // NewCity initialize
  35. func NewCity(name string) (*City, error) {
  36. r, e := newReader(name, &CityInfo{})
  37. if e != nil {
  38. return nil, e
  39. }
  40. return &City{
  41. reader: r,
  42. }, nil
  43. }
  44. // Reload the database
  45. func (db *City) Reload(name string) error {
  46. _, err := os.Stat(name)
  47. if err != nil {
  48. return err
  49. }
  50. reader, err := newReader(name, &CityInfo{})
  51. if err != nil {
  52. return err
  53. }
  54. db.reader = reader
  55. return nil
  56. }
  57. // Find query with addr
  58. func (db *City) Find(addr, language string) ([]string, error) {
  59. return db.reader.find1(addr, language)
  60. }
  61. // FindMap query with addr
  62. func (db *City) FindMap(addr, language string) (map[string]string, error) {
  63. data, err := db.reader.find1(addr, language)
  64. if err != nil {
  65. return nil, err
  66. }
  67. info := make(map[string]string, len(db.reader.meta.Fields))
  68. for k, v := range data {
  69. info[db.reader.meta.Fields[k]] = v
  70. }
  71. return info, nil
  72. }
  73. // FindInfo query with addr
  74. func (db *City) FindInfo(addr, language string) (*CityInfo, error) {
  75. data, err := db.reader.FindMap(addr, language)
  76. if err != nil {
  77. return nil, err
  78. }
  79. info := &CityInfo{}
  80. for k, v := range data {
  81. sv := reflect.ValueOf(info).Elem()
  82. sfv := sv.FieldByName(db.reader.refType[k])
  83. if !sfv.IsValid() {
  84. continue
  85. }
  86. if !sfv.CanSet() {
  87. continue
  88. }
  89. sft := sfv.Type()
  90. fv := reflect.ValueOf(v)
  91. if sft == fv.Type() {
  92. sfv.Set(fv)
  93. }
  94. }
  95. return info, nil
  96. }
  97. // IsIPv4 whether support ipv4
  98. func (db *City) IsIPv4() bool {
  99. return db.reader.IsIPv4Support()
  100. }
  101. // IsIPv6 whether support ipv6
  102. func (db *City) IsIPv6() bool {
  103. return db.reader.IsIPv6Support()
  104. }
  105. // Languages return support languages
  106. func (db *City) Languages() []string {
  107. return db.reader.Languages()
  108. }
  109. // Fields return support fields
  110. func (db *City) Fields() []string {
  111. return db.reader.meta.Fields
  112. }
  113. // BuildTime return database build Time
  114. func (db *City) BuildTime() time.Time {
  115. return db.reader.Build()
  116. }