Skip to content

Commit e0f74e9

Browse files
committed
Remove (*Reader).Decode
And replace it with (*Reader).LookupOffset
1 parent 365e5de commit e0f74e9

File tree

2 files changed

+16
-25
lines changed

2 files changed

+16
-25
lines changed

reader.go

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -150,28 +150,14 @@ func (r *Reader) Lookup(ip netip.Addr) Result {
150150
}
151151
}
152152

153-
// Decode the record at |offset| into |result|. The result value pointed to
154-
// must be a data value that corresponds to a record in the database. This may
155-
// include a struct representation of the data, a map capable of holding the
156-
// data or an empty any value.
157-
//
158-
// If result is a pointer to a struct, the struct need not include a field
159-
// for every value that may be in the database. If a field is not present in
160-
// the structure, the decoder will not decode that field, reducing the time
161-
// required to decode the record.
162-
//
163-
// As a special case, a struct field of type uintptr will be used to capture
164-
// the offset of the value. Decode may later be used to extract the stored
165-
// value from the offset. MaxMind DBs are highly normalized: for example in
166-
// the City database, all records of the same country will reference a
167-
// single representative record for that country. This uintptr behavior allows
168-
// clients to leverage this normalization in their own sub-record caching.
169-
func (r *Reader) Decode(offset uintptr, result any) error {
153+
// LookupOffset returns the Result for the specified offset. Note that
154+
// netip.Prefix returned by Networks will be invalid when using LookupOffset.
155+
func (r *Reader) LookupOffset(offset uintptr) Result {
170156
if r.buffer == nil {
171-
return errors.New("cannot call Decode on a closed database")
157+
return Result{err: errors.New("cannot call Decode on a closed database")}
172158
}
173159

174-
return Result{decoder: r.decoder, offset: uint(offset)}.Decode(result)
160+
return Result{decoder: r.decoder, offset: uint(offset)}
175161
}
176162

177163
var zeroIP = netip.MustParseAddr("::")

reader_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ func TestDecoder(t *testing.T) {
311311
require.NoError(t, result.Err())
312312
require.True(t, result.Found())
313313

314-
require.NoError(t, reader.Decode(result.RecordOffset(), &testV))
314+
res := reader.LookupOffset(result.RecordOffset())
315+
require.NoError(t, res.Decode(&testV))
315316
verify(testV)
316317
}
317318

@@ -563,21 +564,25 @@ func TestNestedOffsetDecode(t *testing.T) {
563564
TimeZoneOffset uintptr `maxminddb:"time_zone"`
564565
} `maxminddb:"location"`
565566
}
566-
require.NoError(t, db.Decode(result.RecordOffset(), &root))
567+
res := db.LookupOffset(result.RecordOffset())
568+
require.NoError(t, res.Decode(&root))
567569
assert.InEpsilon(t, 51.5142, root.Location.Latitude, 1e-10)
568570

569571
var longitude float64
570-
require.NoError(t, db.Decode(root.Location.LongitudeOffset, &longitude))
572+
res = db.LookupOffset(root.Location.LongitudeOffset)
573+
require.NoError(t, res.Decode(&longitude))
571574
assert.InEpsilon(t, -0.0931, longitude, 1e-10)
572575

573576
var timeZone string
574-
require.NoError(t, db.Decode(root.Location.TimeZoneOffset, &timeZone))
577+
res = db.LookupOffset(root.Location.TimeZoneOffset)
578+
require.NoError(t, res.Decode(&timeZone))
575579
assert.Equal(t, "Europe/London", timeZone)
576580

577581
var country struct {
578582
IsoCode string `maxminddb:"iso_code"`
579583
}
580-
require.NoError(t, db.Decode(root.CountryOffset, &country))
584+
res = db.LookupOffset(root.CountryOffset)
585+
require.NoError(t, res.Decode(&country))
581586
assert.Equal(t, "GB", country.IsoCode)
582587

583588
require.NoError(t, db.Close())
@@ -680,7 +685,7 @@ func TestUsingClosedDatabase(t *testing.T) {
680685
err = reader.Lookup(addr).Decode(recordInterface)
681686
assert.Equal(t, "cannot call Lookup on a closed database", err.Error())
682687

683-
err = reader.Decode(0, recordInterface)
688+
err = reader.LookupOffset(0).Decode(recordInterface)
684689
assert.Equal(t, "cannot call Decode on a closed database", err.Error())
685690
}
686691

0 commit comments

Comments
 (0)