@@ -50,13 +50,21 @@ type Metadata struct {
50
50
RecordSize uint `maxminddb:"record_size"`
51
51
}
52
52
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
56
64
// as WebAssembly or Google App Engine, or if the memory map attempt fails
57
65
// due to lack of support from the filesystem, the database is loaded into memory.
58
66
// 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 ) {
60
68
mapFile , err := os .Open (file )
61
69
if err != nil {
62
70
return nil , err
@@ -88,12 +96,12 @@ func Open(file string) (*Reader, error) {
88
96
if err != nil {
89
97
return nil , err
90
98
}
91
- return FromBytes (data )
99
+ return FromBytes (data , options ... )
92
100
}
93
101
return nil , err
94
102
}
95
103
96
- reader , err := FromBytes (data )
104
+ reader , err := FromBytes (data , options ... )
97
105
if err != nil {
98
106
_ = munmap (data )
99
107
return nil , err
@@ -122,9 +130,14 @@ func (r *Reader) Close() error {
122
130
return err
123
131
}
124
132
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
+
128
141
metadataStart := bytes .LastIndex (buffer , metadataStartMarker )
129
142
130
143
if metadataStart == - 1 {
0 commit comments