Skip to content

Commit c661b4a

Browse files
committed
Add reader options
Introduce a geoip2 Option type for use in Open/OpenBytes to support any future open options.
1 parent 79dbfb9 commit c661b4a

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

CHANGELOG.md

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

33
- Updated dependency on `github.com/oschwald/maxminddb-golang/v2` to `v2.0.0`.
4-
- Allow `Open` and `OpenBytes` to forward reader options to `maxminddb-golang`,
5-
enabling advanced reader configuration.
4+
- Add configurable `Option` helpers so `Open` and `OpenBytes` can accept
5+
future options.
66

77
# 2.0.0-beta.4 - 2025-08-23
88

reader.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ type Reader struct {
7878
databaseType databaseType
7979
}
8080

81+
// Option configures Reader behavior.
82+
type Option func(*readerOptions)
83+
84+
type readerOptions struct {
85+
maxminddb []maxminddb.ReaderOption
86+
}
87+
88+
func applyOptions(options []Option) []maxminddb.ReaderOption {
89+
var opts readerOptions
90+
for _, option := range options {
91+
if option != nil {
92+
option(&opts)
93+
}
94+
}
95+
return opts.maxminddb
96+
}
97+
8198
// InvalidMethodError is returned when a lookup method is called on a
8299
// database that it does not support. For instance, calling the ISP method
83100
// on a City database.
@@ -104,10 +121,9 @@ func (e UnknownDatabaseTypeError) Error() string {
104121

105122
// Open takes a string path to a file and returns a Reader struct or an error.
106123
// The database file is opened using a memory map. Use the Close method on the
107-
// Reader object to return the resources to the system. Any reader options
108-
// provided are passed directly to maxminddb.Open.
109-
func Open(file string, options ...maxminddb.ReaderOption) (*Reader, error) {
110-
reader, err := maxminddb.Open(file, options...)
124+
// Reader object to return the resources to the system.
125+
func Open(file string, options ...Option) (*Reader, error) {
126+
reader, err := maxminddb.Open(file, applyOptions(options)...)
111127
if err != nil {
112128
return nil, err
113129
}
@@ -118,10 +134,9 @@ func Open(file string, options ...maxminddb.ReaderOption) (*Reader, error) {
118134
// OpenBytes takes a byte slice corresponding to a GeoIP2/GeoLite2 database
119135
// file and returns a Reader struct or an error. Note that the byte slice is
120136
// used directly; any modification of it after opening the database will result
121-
// in errors while reading from the database. Any reader options provided are
122-
// passed directly to maxminddb.OpenBytes.
123-
func OpenBytes(bytes []byte, options ...maxminddb.ReaderOption) (*Reader, error) {
124-
reader, err := maxminddb.OpenBytes(bytes, options...)
137+
// in errors while reading from the database.
138+
func OpenBytes(bytes []byte, options ...Option) (*Reader, error) {
139+
reader, err := maxminddb.OpenBytes(bytes, applyOptions(options)...)
125140
if err != nil {
126141
return nil, err
127142
}
@@ -136,7 +151,7 @@ func OpenBytes(bytes []byte, options ...maxminddb.ReaderOption) (*Reader, error)
136151
//
137152
// Deprecated: Use OpenBytes instead. FromBytes will be removed in a future
138153
// version.
139-
func FromBytes(bytes []byte, options ...maxminddb.ReaderOption) (*Reader, error) {
154+
func FromBytes(bytes []byte, options ...Option) (*Reader, error) {
140155
return OpenBytes(bytes, options...)
141156
}
142157

0 commit comments

Comments
 (0)