decoder
packageThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
Overview¶
Package decoder provides low-level decoding utilities for MaxMind DB data.
Index¶
- type DataDecoder
- type Decoder
- func (d *Decoder) Offset() uint
- func (d *Decoder) PeekKind() (Kind, error)
- func (d *Decoder) ReadBool() (bool, error)
- func (d *Decoder) ReadBytes() ([]byte, error)
- func (d *Decoder) ReadFloat32() (float32, error)
- func (d *Decoder) ReadFloat64() (float64, error)
- func (d *Decoder) ReadInt32() (int32, error)
- func (d *Decoder) ReadMap() (iter.Seq2[[]byte, error], uint, error)
- func (d *Decoder) ReadSlice() (iter.Seq[error], uint, error)
- func (d *Decoder) ReadString() (string, error)
- func (d *Decoder) ReadUint128() (hi, lo uint64, err error)
- func (d *Decoder) ReadUint16() (uint16, error)
- func (d *Decoder) ReadUint32() (uint32, error)
- func (d *Decoder) ReadUint64() (uint64, error)
- func (d *Decoder) SkipValue() error
- type DecoderOption
- type Kind
- type ReflectionDecoder
- type Unmarshaler
Examples¶
Constants¶
This section is empty.
Variables¶
This section is empty.
Functions¶
This section is empty.
Types¶
typeDataDecoder¶
type DataDecoder struct {// contains filtered or unexported fields}DataDecoder is a decoder for the MMDB data section.This is exported so mmdbdata package can use it, but still internal.
funcNewDataDecoder¶
func NewDataDecoder(buffer []byte)DataDecoder
NewDataDecoder creates aDataDecoder.
typeDecoder¶
type Decoder struct {// contains filtered or unexported fields}Decoder allows decoding of a single value stored at a specific offsetin the database.
funcNewDecoder¶
func NewDecoder(dDataDecoder, offsetuint, options ...DecoderOption) *Decoder
NewDecoder creates a new Decoder with the given DataDecoder, offset, and options.
func (*Decoder)Offset¶
Offset returns the current offset position in the database.This can be used by custom unmarshalers for caching purposes.
func (*Decoder)PeekKind¶
PeekKind returns the kind of the current value without consuming it.This allows for look-ahead parsing similar to jsontext.Decoder.PeekKind().
Example¶
ExampleDecoder_PeekKind demonstrates how to use PeekKind forlook-ahead parsing without consuming values.
// Create test data with different typestestCases := [][]byte{{0x44, 't', 'e', 's', 't'}, // String{0xE0}, // Empty map{0x00, 0x04}, // Empty slice (extended type){0x01, 0x07}, // Bool true (extended type)}typeNames := []string{"String", "Map", "Slice", "Bool"}for i, buffer := range testCases {decoder := NewDecoder(NewDataDecoder(buffer), 0)// Peek at the kind without consuming ittyp, err := decoder.PeekKind()if err != nil {panic(err)}fmt.Printf("Type %d: %s (value: %d)\n", i+1, typeNames[i], typ)// PeekKind doesn't consume, so we can peek againtyp2, err := decoder.PeekKind()if err != nil {panic(err)}if typ != typ2 {fmt.Println("ERROR: PeekKind consumed the value!")}}Output:Type 1: String (value: 2)Type 2: Map (value: 7)Type 3: Slice (value: 11)Type 4: Bool (value: 14)
func (*Decoder)ReadBool¶
ReadBool reads the value pointed by the decoder as a bool.
Returns an error if the database is malformed or if the pointed value is not a bool.
func (*Decoder)ReadBytes¶
ReadBytes reads the value pointed by the decoder as bytes.
Returns an error if the database is malformed or if the pointed value is not bytes.
func (*Decoder)ReadFloat32¶
ReadFloat32 reads the value pointed by the decoder as a float32.
Returns an error if the database is malformed or if the pointed value is not a float.
func (*Decoder)ReadFloat64¶
ReadFloat64 reads the value pointed by the decoder as a float64.
Returns an error if the database is malformed or if the pointed value is not a double.
func (*Decoder)ReadInt32¶
ReadInt32 reads the value pointed by the decoder as a int32.
Returns an error if the database is malformed or if the pointed value is not an int32.
func (*Decoder)ReadMap¶
ReadMap returns an iterator to read the map along with the map size. Thesize can be used to pre-allocate a map with the correct capacity for betterperformance. The first value from the iterator is the key. Please note thatthis byte slice is only valid during the iteration. This is done to avoidan unnecessary allocation. You must make a copy of it if you are storing itfor later use. The second value is an error indicating that the database ismalformed or that the pointed value is not a map.
func (*Decoder)ReadSlice¶
ReadSlice returns an iterator over the values of the slice along with theslice size. The size can be used to pre-allocate a slice with the correctcapacity for better performance. The iterator returns an error if thedatabase is malformed or if the pointed value is not a slice.
func (*Decoder)ReadString¶
ReadString reads the value pointed by the decoder as a string.
Returns an error if the database is malformed or if the pointed value is not a string.
func (*Decoder)ReadUint128¶
ReadUint128 reads the value pointed by the decoder as a uint128.
Returns an error if the database is malformed or if the pointed value is not an uint128.
func (*Decoder)ReadUint16¶
ReadUint16 reads the value pointed by the decoder as a uint16.
Returns an error if the database is malformed or if the pointed value is not an uint16.
func (*Decoder)ReadUint32¶
ReadUint32 reads the value pointed by the decoder as a uint32.
Returns an error if the database is malformed or if the pointed value is not an uint32.
func (*Decoder)ReadUint64¶
ReadUint64 reads the value pointed by the decoder as a uint64.
Returns an error if the database is malformed or if the pointed value is not an uint64.
typeKind¶
type Kindint
Kind constants for the different MMDB data kinds.
const (// KindExtended indicates an extended kind.KindExtendedKind =iota// KindPointer is a pointer to another location in the data section.KindPointer// KindString is a UTF-8 string.KindString// KindFloat64 is a 64-bit floating point number.KindFloat64// KindBytes is a byte slice.KindBytes// KindUint16 is a 16-bit unsigned integer.KindUint16// KindUint32 is a 32-bit unsigned integer.KindUint32// KindMap is a map from strings to other data types.KindMap// KindInt32 is a 32-bit signed integer.KindInt32// KindUint64 is a 64-bit unsigned integer.KindUint64// KindUint128 is a 128-bit unsigned integer.KindUint128// KindSlice is an array of values.KindSlice// KindContainer is a data cache container.KindContainer// KindEndMarker marks the end of the data section.KindEndMarker// KindBool is a boolean value.KindBool// KindFloat32 is a 32-bit floating point number.KindFloat32)
MMDB data kind constants.
func (Kind)IsContainer¶
IsContainer returns true if the Kind represents a container type (Map or Slice).
Example¶
ExampleKind_IsContainer demonstrates container type detection.
kinds := []Kind{KindString, KindMap, KindSlice, KindUint32}for _, k := range kinds {if k.IsContainer() {fmt.Printf("%s is a container type\n", k.String())} else {fmt.Printf("%s is not a container type\n", k.String())}}Output:String is not a container typeMap is a container typeSlice is a container typeUint32 is not a container type
func (Kind)IsScalar¶
IsScalar returns true if the Kind represents a scalar value type.
Example¶
ExampleKind_IsScalar demonstrates scalar type detection.
kinds := []Kind{KindString, KindMap, KindUint32, KindPointer}for _, k := range kinds {if k.IsScalar() {fmt.Printf("%s is a scalar value\n", k.String())} else {fmt.Printf("%s is not a scalar value\n", k.String())}}Output:String is a scalar valueMap is not a scalar valueUint32 is a scalar valuePointer is not a scalar value
typeReflectionDecoder¶
type ReflectionDecoder struct {DataDecoder}ReflectionDecoder is a decoder for the MMDB data section.
func (*ReflectionDecoder)Decode¶
func (d *ReflectionDecoder) Decode(offsetuint, vany)error
Decode decodes the data value at offset and stores it in the valuepointed at by v.
func (*ReflectionDecoder)DecodePath¶
func (d *ReflectionDecoder) DecodePath(offsetuint,path []any,vany,)error
DecodePath decodes the data value at offset and stores the value associatedwith the path in the value pointed at by v.
func (*ReflectionDecoder)IsEmptyValueAt¶
func (d *ReflectionDecoder) IsEmptyValueAt(offsetuint) (bool,error)
IsEmptyValueAt checks if the value at the given offset is an empty map or array.Returns true if the value is a map or array with size 0.
func (*ReflectionDecoder)VerifyDataSection¶
func (d *ReflectionDecoder) VerifyDataSection(offsets map[uint]bool)error
VerifyDataSection verifies the data section against the providedoffsets from the tree.
typeUnmarshaler¶
Unmarshaler is implemented by types that can unmarshal MaxMind DB data.This is used internally for reflection-based decoding.