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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
package decoder
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Inner type with UnmarshalMaxMindDB.
type testInnerNested struct {
Value string
custom bool // track if custom unmarshaler was called
}
func (i *testInnerNested) UnmarshalMaxMindDB(d *Decoder) error {
i.custom = true
str, err := d.ReadString()
if err != nil {
return err
}
i.Value = "custom:" + str
return nil
}
// TestNestedUnmarshaler tests that UnmarshalMaxMindDB is called for nested struct fields.
func TestNestedUnmarshaler(t *testing.T) {
// Outer type without UnmarshalMaxMindDB
type Outer struct {
Field testInnerNested
Name string
}
// Create test data: a map with "Field" -> "test" and "Name" -> "example"
data := []byte{
// Map with 2 items
0xe2,
// Key "Field"
0x45, 'F', 'i', 'e', 'l', 'd',
// Value "test" (string)
0x44, 't', 'e', 's', 't',
// Key "Name"
0x44, 'N', 'a', 'm', 'e',
// Value "example" (string)
0x47, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
}
t.Run("nested field with UnmarshalMaxMindDB", func(t *testing.T) {
d := New(data)
var result Outer
err := d.Decode(0, &result)
require.NoError(t, err)
// Check that custom unmarshaler WAS called for nested field
require.True(
t,
result.Field.custom,
"Custom unmarshaler should be called for nested fields",
)
require.Equal(t, "custom:test", result.Field.Value)
require.Equal(t, "example", result.Name)
})
}
// testInnerPointer with UnmarshalMaxMindDB for pointer test.
type testInnerPointer struct {
Value string
custom bool
}
func (i *testInnerPointer) UnmarshalMaxMindDB(d *Decoder) error {
i.custom = true
str, err := d.ReadString()
if err != nil {
return err
}
i.Value = "ptr:" + str
return nil
}
// TestNestedUnmarshalerPointer tests UnmarshalMaxMindDB with pointer fields.
func TestNestedUnmarshalerPointer(t *testing.T) {
type Outer struct {
Field *testInnerPointer
Name string
}
// Test data
data := []byte{
// Map with 2 items
0xe2,
// Key "Field"
0x45, 'F', 'i', 'e', 'l', 'd',
// Value "test"
0x44, 't', 'e', 's', 't',
// Key "Name"
0x44, 'N', 'a', 'm', 'e',
// Value "example"
0x47, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
}
t.Run("pointer field with UnmarshalMaxMindDB", func(t *testing.T) {
d := New(data)
var result Outer
err := d.Decode(0, &result)
require.NoError(t, err)
// The pointer should be created and unmarshaled with custom unmarshaler
require.NotNil(t, result.Field)
require.True(
t,
result.Field.custom,
"Custom unmarshaler should be called for pointer fields",
)
require.Equal(t, "ptr:test", result.Field.Value)
require.Equal(t, "example", result.Name)
})
}
// testItem with UnmarshalMaxMindDB for slice test.
type testItem struct {
ID int
custom bool
}
func (item *testItem) UnmarshalMaxMindDB(d *Decoder) error {
item.custom = true
id, err := d.ReadUint32()
if err != nil {
return err
}
item.ID = int(id) * 2
return nil
}
// TestNestedUnmarshalerInSlice tests UnmarshalMaxMindDB for slice elements.
func TestNestedUnmarshalerInSlice(t *testing.T) {
type Container struct {
Items []testItem
}
// Test data: a map with "Items" -> [1, 2, 3]
data := []byte{
// Map with 1 item (KindMap=7 << 5 | size=1)
0xe1,
// Key "Items" (KindString=2 << 5 | size=5)
0x45, 'I', 't', 'e', 'm', 's',
// Slice with 3 items - KindSlice=11, which is > 7, so we need extended type
// Extended type: ctrl_byte = (KindExtended << 5) | size = (0 << 5) | 3 = 0x03
// Next byte: KindSlice - 7 = 11 - 7 = 4
0x03, 0x04,
// Value 1 (KindUint32=6 << 5 | size=1)
0xc1, 0x01,
// Value 2 (KindUint32=6 << 5 | size=1)
0xc1, 0x02,
// Value 3 (KindUint32=6 << 5 | size=1)
0xc1, 0x03,
}
t.Run("slice elements with UnmarshalMaxMindDB", func(t *testing.T) {
d := New(data)
var result Container
err := d.Decode(0, &result)
require.NoError(t, err)
require.Len(t, result.Items, 3)
// With custom unmarshaler, values should be doubled
require.True(
t,
result.Items[0].custom,
"Custom unmarshaler should be called for slice elements",
)
require.Equal(t, 2, result.Items[0].ID) // 1 * 2
require.Equal(t, 4, result.Items[1].ID) // 2 * 2
require.Equal(t, 6, result.Items[2].ID) // 3 * 2
})
}
// testValue with UnmarshalMaxMindDB for map test.
type testValue struct {
Data string
custom bool
}
func (v *testValue) UnmarshalMaxMindDB(d *Decoder) error {
v.custom = true
str, err := d.ReadString()
if err != nil {
return err
}
v.Data = "map:" + str
return nil
}
// TestNestedUnmarshalerInMap tests UnmarshalMaxMindDB for map values.
func TestNestedUnmarshalerInMap(t *testing.T) {
// Test data: {"key1": "value1", "key2": "value2"}
data := []byte{
// Map with 2 items
0xe2,
// Key "key1"
0x44, 'k', 'e', 'y', '1',
// Value "value1"
0x46, 'v', 'a', 'l', 'u', 'e', '1',
// Key "key2"
0x44, 'k', 'e', 'y', '2',
// Value "value2"
0x46, 'v', 'a', 'l', 'u', 'e', '2',
}
t.Run("map values with UnmarshalMaxMindDB", func(t *testing.T) {
d := New(data)
var result map[string]testValue
err := d.Decode(0, &result)
require.NoError(t, err)
require.Len(t, result, 2)
require.True(t, result["key1"].custom, "Custom unmarshaler should be called for map values")
require.Equal(t, "map:value1", result["key1"].Data)
require.Equal(t, "map:value2", result["key2"].Data)
})
}
// testMapIterator uses ReadMap() iterator to simulate mmdbtype.Map behavior.
type testMapIterator struct {
Values map[string]string
custom bool
}
func (m *testMapIterator) UnmarshalMaxMindDB(d *Decoder) error {
m.custom = true
iter, size, err := d.ReadMap()
if err != nil {
return err
}
m.Values = make(map[string]string, size)
for key, iterErr := range iter {
if iterErr != nil {
return iterErr
}
// Read the value as a string
value, err := d.ReadString()
if err != nil {
return err
}
m.Values[string(key)] = value
}
return nil
}
// TestCustomUnmarshalerWithIterator tests that custom unmarshalers using iterators
// work correctly in struct fields. This reproduces the original "no next offset available"
// issue that occurred when mmdbtype.Map was used in structs.
func TestCustomUnmarshalerWithIterator(t *testing.T) {
type Record struct {
Name string
Location testMapIterator // This field uses ReadMap() iterator
Country string
}
data := []byte{
// Map with 3 items
0xe3,
// Key "Name"
0x44, 'N', 'a', 'm', 'e',
// Value "Test" (string)
0x44, 'T', 'e', 's', 't',
// Key "Location"
0x48, 'L', 'o', 'c', 'a', 't', 'i', 'o', 'n',
// Value: Map with 2 items (latitude and longitude)
0xe2,
// Key "lat"
0x43, 'l', 'a', 't',
// Value "40.7"
0x44, '4', '0', '.', '7',
// Key "lng"
0x43, 'l', 'n', 'g',
// Value "-74.0"
0x45, '-', '7', '4', '.', '0',
// Key "Country"
0x47, 'C', 'o', 'u', 'n', 't', 'r', 'y',
// Value "US"
0x42, 'U', 'S',
}
d := New(data)
var result Record
err := d.Decode(0, &result)
require.NoError(t, err)
require.Equal(t, "Test", result.Name)
assert.True(t, result.Location.custom, "Custom unmarshaler should be called")
assert.Len(t, result.Location.Values, 2)
assert.Equal(t, "40.7", result.Location.Values["lat"])
assert.Equal(t, "-74.0", result.Location.Values["lng"])
assert.Equal(t, "US", result.Country)
}
|