Movatterモバイル変換


[0]ホーム

URL:


decoder

package
v2.0.0Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2025 License:ISCImports:10Imported by:0

Details

Repository

github.com/oschwald/maxminddb-golang

Links

Documentation

Overview

Package decoder provides low-level decoding utilities for MaxMind DB data.

Index

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

func (d *Decoder) Offset()uint

Offset returns the current offset position in the database.This can be used by custom unmarshalers for caching purposes.

func (*Decoder)PeekKind

func (d *Decoder) PeekKind() (Kind,error)

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

func (d *Decoder) ReadBool() (bool,error)

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

func (d *Decoder) ReadBytes() ([]byte,error)

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

func (d *Decoder) ReadFloat32() (float32,error)

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

func (d *Decoder) ReadFloat64() (float64,error)

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

func (d *Decoder) ReadInt32() (int32,error)

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

func (d *Decoder) ReadMap() (iter.Seq2[[]byte,error],uint,error)

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

func (d *Decoder) ReadSlice() (iter.Seq[error],uint,error)

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

func (d *Decoder) ReadString() (string,error)

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

func (d *Decoder) ReadUint128() (hi, louint64, errerror)

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

func (d *Decoder) ReadUint16() (uint16,error)

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

func (d *Decoder) ReadUint32() (uint32,error)

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

func (d *Decoder) ReadUint64() (uint64,error)

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.

func (*Decoder)SkipValue

func (d *Decoder) SkipValue()error

SkipValue skips over the current value without decoding it.This is useful in custom decoders when encountering unknown fields.The decoder will be positioned after the skipped value.

typeDecoderOption

type DecoderOption func(*decoderOptions)

DecoderOption configures a Decoder.

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

func (kKind) IsContainer()bool

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

func (kKind) IsScalar()bool

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

func (Kind)String

func (kKind) String()string

String returns a human-readable name for the Kind.

Example

ExampleKind_String demonstrates human-readable Kind names.

kinds := []Kind{KindString, KindMap, KindSlice, KindUint32, KindBool}for _, k := range kinds {fmt.Printf("%s\n", k.String())}
Output:StringMapSliceUint32Bool

typeReflectionDecoder

type ReflectionDecoder struct {DataDecoder}

ReflectionDecoder is a decoder for the MMDB data section.

funcNew

func New(buffer []byte)ReflectionDecoder

New creates aReflectionDecoder.

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

type Unmarshaler interface {UnmarshalMaxMindDB(d *Decoder)error}

Unmarshaler is implemented by types that can unmarshal MaxMind DB data.This is used internally for reflection-based decoding.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp