Skip to content

Commit 2f81fca

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 73c4705 commit 2f81fca

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-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: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,17 @@ 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+
type ReaderOption func(*readerOptions)
56+
57+
// Open takes a string path to a MaxMind DB file and any options. It returns a
58+
// Reader structure or an error. The database file is opened using a memory
59+
// map on supported platforms. On platforms without memory map support, such
5660
// as WebAssembly or Google App Engine, or if the memory map attempt fails
5761
// due to lack of support from the filesystem, the database is loaded into memory.
5862
// Use the Close method on the Reader object to return the resources to the system.
59-
func Open(file string) (*Reader, error) {
63+
func Open(file string, options ...ReaderOption) (*Reader, error) {
6064
mapFile, err := os.Open(file)
6165
if err != nil {
6266
return nil, err
@@ -88,12 +92,12 @@ func Open(file string) (*Reader, error) {
8892
if err != nil {
8993
return nil, err
9094
}
91-
return FromBytes(data)
95+
return FromBytes(data, options...)
9296
}
9397
return nil, err
9498
}
9599

96-
reader, err := FromBytes(data)
100+
reader, err := FromBytes(data, options...)
97101
if err != nil {
98102
_ = munmap(data)
99103
return nil, err
@@ -122,9 +126,14 @@ func (r *Reader) Close() error {
122126
return err
123127
}
124128

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) {
129+
// FromBytes takes a byte slice corresponding to a MaxMind DB file and any
130+
// options. It returns a Reader structure or an error.
131+
func FromBytes(buffer []byte, options ...ReaderOption) (*Reader, error) {
132+
opts := &readerOptions{}
133+
for _, option := range options {
134+
option(opts)
135+
}
136+
128137
metadataStart := bytes.LastIndex(buffer, metadataStartMarker)
129138

130139
if metadataStart == -1 {

0 commit comments

Comments
 (0)