Documentation
¶
Overview ¶
Package mmdbdata provides low-level types and interfaces for custom MaxMind DB decoding.
This package allows custom decoding logic for applications that need fine-grained control over how MaxMind DB data is processed. For most use cases, the high-level maxminddb.Reader API is recommended instead.
Manual Decoding Example ¶
Custom types can implement the Unmarshaler interface for custom decoding:
type City struct {
Names map[string]string `maxminddb:"names"`
GeoNameID uint `maxminddb:"geoname_id"`
}
func (c *City) UnmarshalMaxMindDB(d *mmdbdata.Decoder) error {
mapIter, _, err := d.ReadMap()
if err != nil { return err }
for key, err := range mapIter {
if err != nil { return err }
switch string(key) {
case "names":
nameIter, size, err := d.ReadMap()
if err != nil { return err }
names := make(map[string]string, size) // Pre-allocate with size
for nameKey, nameErr := range nameIter {
if nameErr != nil { return nameErr }
value, valueErr := d.ReadString()
if valueErr != nil { return valueErr }
names[string(nameKey)] = value
}
c.Names = names
case "geoname_id":
geoID, err := d.ReadUint32()
if err != nil { return err }
c.GeoNameID = uint(geoID)
default:
if err := d.SkipValue(); err != nil { return err }
}
}
return nil
}
Types implementing Unmarshaler will automatically use custom decoding logic instead of reflection when used with maxminddb.Reader.Lookup, similar to how json.Unmarshaler works with encoding/json.
Direct Decoder Usage ¶
For even more control, you can use the Decoder directly:
decoder := mmdbdata.NewDecoder(buffer, offset)
value, err := decoder.ReadString()
if err != nil {
return err
}
Index ¶
Constants ¶
const ( KindExtended = decoder.KindExtended KindPointer = decoder.KindPointer KindString = decoder.KindString KindFloat64 = decoder.KindFloat64 KindBytes = decoder.KindBytes KindUint16 = decoder.KindUint16 KindUint32 = decoder.KindUint32 KindMap = decoder.KindMap KindInt32 = decoder.KindInt32 KindUint64 = decoder.KindUint64 KindUint128 = decoder.KindUint128 KindSlice = decoder.KindSlice KindContainer = decoder.KindContainer KindEndMarker = decoder.KindEndMarker KindBool = decoder.KindBool KindFloat32 = decoder.KindFloat32 )
Kind constants for MMDB data.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Decoder ¶
Decoder provides methods for decoding MMDB data.
func NewDecoder ¶
func NewDecoder(buffer []byte, offset uint, options ...DecoderOption) *Decoder
NewDecoder creates a new Decoder with the given buffer, offset, and options. Error messages automatically include contextual information like offset and path (e.g., "/city/names/en") with zero impact on successful operations.
type Unmarshaler ¶
Unmarshaler is implemented by types that can unmarshal MaxMind DB data. This follows the same pattern as json.Unmarshaler and other Go standard library interfaces.