File: deserializer_test.go

package info (click to toggle)
golang-github-oschwald-maxminddb-golang 1.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 248 kB
  • sloc: makefile: 3
file content (120 lines) | stat: -rw-r--r-- 2,298 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package maxminddb

import (
	"math/big"
	"net"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestDecodingToDeserializer(t *testing.T) {
	reader, err := Open(testFile("MaxMind-DB-test-decoder.mmdb"))
	require.NoError(t, err, "unexpected error while opening database: %v", err)

	dser := testDeserializer{}
	err = reader.Lookup(net.ParseIP("::1.1.1.0"), &dser)
	require.NoError(t, err, "unexpected error while doing lookup: %v", err)

	checkDecodingToInterface(t, dser.rv)
}

type stackValue struct {
	value  any
	curNum int
}

type testDeserializer struct {
	stack []*stackValue
	rv    any
	key   *string
}

func (*testDeserializer) ShouldSkip(_ uintptr) (bool, error) {
	return false, nil
}

func (d *testDeserializer) StartSlice(size uint) error {
	return d.add(make([]any, size))
}

func (d *testDeserializer) StartMap(_ uint) error {
	return d.add(map[string]any{})
}

//nolint:unparam // This is to meet the requirements of the interface.
func (d *testDeserializer) End() error {
	d.stack = d.stack[:len(d.stack)-1]
	return nil
}

func (d *testDeserializer) String(v string) error {
	return d.add(v)
}

func (d *testDeserializer) Float64(v float64) error {
	return d.add(v)
}

func (d *testDeserializer) Bytes(v []byte) error {
	return d.add(v)
}

func (d *testDeserializer) Uint16(v uint16) error {
	return d.add(uint64(v))
}

func (d *testDeserializer) Uint32(v uint32) error {
	return d.add(uint64(v))
}

func (d *testDeserializer) Int32(v int32) error {
	return d.add(int(v))
}

func (d *testDeserializer) Uint64(v uint64) error {
	return d.add(v)
}

func (d *testDeserializer) Uint128(v *big.Int) error {
	return d.add(v)
}

func (d *testDeserializer) Bool(v bool) error {
	return d.add(v)
}

func (d *testDeserializer) Float32(v float32) error {
	return d.add(v)
}

func (d *testDeserializer) add(v any) error {
	if len(d.stack) == 0 {
		d.rv = v
	} else {
		top := d.stack[len(d.stack)-1]
		switch parent := top.value.(type) {
		case map[string]any:
			if d.key == nil {
				key := v.(string)
				d.key = &key
			} else {
				parent[*d.key] = v
				d.key = nil
			}

		case []any:
			parent[top.curNum] = v
			top.curNum++
		default:
		}
	}

	switch v := v.(type) {
	case map[string]any, []any:
		d.stack = append(d.stack, &stackValue{value: v})
	default:
	}

	return nil
}