Skip to content

Commit 67124ba

Browse files
committed
Add support for options to Open and FromBytes
This will be used in the future for things like cache configuration. Adding now as it is a breaking change.
1 parent d5e3203 commit 67124ba

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 2.0.0-beta.4
44

5+
- `Open` and `FromBytes` now accept options.
56
- `IncludeNetworksWithoutData` and `IncludeAliasedNetworks` now return a
67
`NetworksOption` rather than being one themselves. This was done to improve
78
the documentation organization.

reader.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,21 @@ type Metadata struct {
5050
RecordSize uint `maxminddb:"record_size"`
5151
}
5252

53-
// Open takes a string path to a MaxMind DB file and returns a Reader
54-
// structure or an error. The database file is opened using a memory map
55-
// on supported platforms. On platforms without memory map support, such
53+
type readerOptions struct{}
54+
55+
// ReaderOption are options for [Open] and [FromBytes].
56+
//
57+
// This was added to allow for future options, e.g., for caching, without
58+
// causing a breaking API change.
59+
type ReaderOption func(*readerOptions)
60+
61+
// Open takes a string path to a MaxMind DB file and any options. It returns a
62+
// Reader structure or an error. The database file is opened using a memory
63+
// map on supported platforms. On platforms without memory map support, such
5664
// as WebAssembly or Google App Engine, or if the memory map attempt fails
5765
// due to lack of support from the filesystem, the database is loaded into memory.
5866
// Use the Close method on the Reader object to return the resources to the system.
59-
func Open(file string) (*Reader, error) {
67+
func Open(file string, options ...ReaderOption) (*Reader, error) {
6068
mapFile, err := os.Open(file)
6169
if err != nil {
6270
return nil, err
@@ -88,12 +96,12 @@ func Open(file string) (*Reader, error) {
8896
if err != nil {
8997
return nil, err
9098
}
91-
return FromBytes(data)
99+
return FromBytes(data, options...)
92100
}
93101
return nil, err
94102
}
95103

96-
reader, err := FromBytes(data)
104+
reader, err := FromBytes(data, options...)
97105
if err != nil {
98106
_ = munmap(data)
99107
return nil, err
@@ -122,9 +130,14 @@ func (r *Reader) Close() error {
122130
return err
123131
}
124132

125-
// FromBytes takes a byte slice corresponding to a MaxMind DB file and returns
126-
// a Reader structure or an error.
127-
func FromBytes(buffer []byte) (*Reader, error) {
133+
// FromBytes takes a byte slice corresponding to a MaxMind DB file and any
134+
// options. It returns a Reader structure or an error.
135+
func FromBytes(buffer []byte, options ...ReaderOption) (*Reader, error) {
136+
opts := &readerOptions{}
137+
for _, option := range options {
138+
option(opts)
139+
}
140+
128141
metadataStart := bytes.LastIndex(buffer, metadataStartMarker)
129142

130143
if metadataStart == -1 {

0 commit comments

Comments
 (0)