Skip to content

Commit ea192bb

Browse files
committed
Document v2 migration guidance
1 parent c05e7c5 commit ea192bb

File tree

3 files changed

+121
-2
lines changed

3 files changed

+121
-2
lines changed

CHANGELOG.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
# 2.0.0 -
22

3+
- **BREAKING CHANGE**: Lookup methods now require `netip.Addr`, return typed
4+
`Names`, and provide `HasData()` helpers while always populating
5+
`Network`/`IPAddress` fields so network topology remains accessible.
6+
- **BREAKING CHANGE**: Struct field casing now matches MaxMind responses (for
7+
example `IsoCode``ISOCode`), location coordinates use pointers, and JSON
8+
tags rely on Go 1.24 `omitzero` support—upgrade your toolchain before
9+
adopting v2.
10+
- Added `MIGRATION.md` with detailed guidance for upgrading from v1.
311
- Updated dependency on `github.com/oschwald/maxminddb-golang/v2` to `v2.0.0`.
4-
- Add configurable `Option` helpers so `Open` and `OpenBytes` can accept
5-
future options.
12+
- Added configurable `Option` helpers so `Open` and `OpenBytes` can accept
13+
future options without forcing a v3 release.
614
- **BREAKING CHANGE**: Removed deprecated `FromBytes` method. Use `OpenBytes`
715
instead.
816

MIGRATION.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Migrating to geoip2-golang v2
2+
3+
Version 2.0 modernizes the public API, improves performance, and tightens
4+
parity with current MaxMind databases. This guide highlights the most important
5+
changes and shows how to update existing v1 code.
6+
7+
## Upgrade Checklist
8+
9+
1. **Update imports**
10+
11+
```go
12+
// v1
13+
import "github.com/oschwald/geoip2-golang"
14+
15+
// v2
16+
import "github.com/oschwald/geoip2-golang/v2"
17+
```
18+
19+
2. **Switch to `netip.Addr`**
20+
21+
Lookup methods now take `netip.Addr` instead of `net.IP`. Use
22+
`netip.ParseAddr` (errors on invalid input) or `netip.MustParseAddr` for
23+
trusted literals. This avoids ambiguity around IPv4-in-IPv6 addresses and
24+
removes heap allocations for most lookups.
25+
26+
```go
27+
// v1
28+
ip := net.ParseIP("81.2.69.142")
29+
record, err := db.City(ip)
30+
31+
// v2
32+
ip, err := netip.ParseAddr("81.2.69.142")
33+
if err != nil {
34+
return err
35+
}
36+
record, err := db.City(ip)
37+
```
38+
39+
3. **Adopt the new `HasData()` helpers**
40+
41+
All result structs have a `HasData()` method that ignores the always-filled
42+
`Network` and `IPAddress` fields. Use it instead of comparing against zero
43+
values.
44+
45+
```go
46+
record, err := db.City(ip)
47+
if err != nil {
48+
return err
49+
}
50+
if !record.HasData() {
51+
// handle missing GeoIP data
52+
}
53+
```
54+
55+
4. **Use structured `Names` fields**
56+
57+
Localized names are now strongly typed instead of map lookups. Access the
58+
language-specific struct field directly.
59+
60+
```go
61+
// v1
62+
cityEn := record.City.Names["en"]
63+
cityPtBr := record.City.Names["pt-BR"]
64+
65+
// v2
66+
cityEn := record.City.Names.English
67+
cityPtBr := record.City.Names.BrazilianPortuguese
68+
```
69+
70+
Supported fields include `English`, `German`, `Spanish`, `French`,
71+
`Japanese`, `BrazilianPortuguese`, `Russian`, and `SimplifiedChinese`.
72+
73+
5. **Expect `Network` and `IPAddress` on every result**
74+
75+
Each lookup populates the queried IP address and the enclosing network on
76+
every result struct, even if `HasData()` returns false. Existing code that
77+
depended on empty `Network` for “no data” should switch to `HasData()`.
78+
79+
6. **Adjust renamed fields**
80+
81+
`IsoCode` is now `ISOCode` across all structs. Update code accordingly.
82+
83+
```go
84+
// v1
85+
code := record.Country.IsoCode
86+
87+
// v2
88+
code := record.Country.ISOCode
89+
```
90+
91+
7. **Build with Go 1.24 or newer**
92+
93+
The module now depends on Go 1.24 features such as `omitzero` struct tags.
94+
Update your toolchain (`go env GOVERSION`) before upgrading.
95+
96+
## Additional Notes
97+
98+
- `Open` and `OpenBytes` accept optional `Option` values, allowing future
99+
extensions without new breaking releases.
100+
- The legacy `FromBytes` helper is removed. Use `OpenBytes` instead.
101+
- JSON output mirrors MaxMind database naming thanks to the new `omitzero` tags
102+
and typed structs.
103+
104+
Following the checklist above should cover most migrations. For edge cases,
105+
review `models.go` for field definitions and `example_test.go` for idiomatic
106+
usage with the v2 API.

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ Version 2.0 includes several major improvements:
3838
- **Go 1.24 Support**: Uses `omitzero` JSON tags to match MaxMind database
3939
behavior
4040

41+
## Migration
42+
43+
See [MIGRATION.md](MIGRATION.md) for step-by-step guidance on upgrading from
44+
v1.
45+
4146
## Usage
4247

4348
[See GoDoc](https://pkg.go.dev/github.com/oschwald/geoip2-golang/v2) for

0 commit comments

Comments
 (0)