|
| 1 | +package decoder |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | +) |
| 8 | + |
| 9 | +// Inner type with UnmarshalMaxMindDB. |
| 10 | +type testInnerNested struct { |
| 11 | + Value string |
| 12 | + custom bool // track if custom unmarshaler was called |
| 13 | +} |
| 14 | + |
| 15 | +func (i *testInnerNested) UnmarshalMaxMindDB(d *Decoder) error { |
| 16 | + i.custom = true |
| 17 | + str, err := d.DecodeString() |
| 18 | + if err != nil { |
| 19 | + return err |
| 20 | + } |
| 21 | + i.Value = "custom:" + str |
| 22 | + return nil |
| 23 | +} |
| 24 | + |
| 25 | +// TestNestedUnmarshaler tests that UnmarshalMaxMindDB is called for nested struct fields. |
| 26 | +func TestNestedUnmarshaler(t *testing.T) { |
| 27 | + // Outer type without UnmarshalMaxMindDB |
| 28 | + type Outer struct { |
| 29 | + Field testInnerNested |
| 30 | + Name string |
| 31 | + } |
| 32 | + |
| 33 | + // Create test data: a map with "Field" -> "test" and "Name" -> "example" |
| 34 | + data := []byte{ |
| 35 | + // Map with 2 items |
| 36 | + 0xe2, |
| 37 | + // Key "Field" |
| 38 | + 0x45, 'F', 'i', 'e', 'l', 'd', |
| 39 | + // Value "test" (string) |
| 40 | + 0x44, 't', 'e', 's', 't', |
| 41 | + // Key "Name" |
| 42 | + 0x44, 'N', 'a', 'm', 'e', |
| 43 | + // Value "example" (string) |
| 44 | + 0x47, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 45 | + } |
| 46 | + |
| 47 | + t.Run("nested field with UnmarshalMaxMindDB", func(t *testing.T) { |
| 48 | + d := New(data) |
| 49 | + var result Outer |
| 50 | + |
| 51 | + err := d.Decode(0, &result) |
| 52 | + require.NoError(t, err) |
| 53 | + |
| 54 | + // Check that custom unmarshaler WAS called for nested field |
| 55 | + require.True( |
| 56 | + t, |
| 57 | + result.Field.custom, |
| 58 | + "Custom unmarshaler should be called for nested fields", |
| 59 | + ) |
| 60 | + require.Equal(t, "custom:test", result.Field.Value) |
| 61 | + require.Equal(t, "example", result.Name) |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +// testInnerPointer with UnmarshalMaxMindDB for pointer test. |
| 66 | +type testInnerPointer struct { |
| 67 | + Value string |
| 68 | + custom bool |
| 69 | +} |
| 70 | + |
| 71 | +func (i *testInnerPointer) UnmarshalMaxMindDB(d *Decoder) error { |
| 72 | + i.custom = true |
| 73 | + str, err := d.DecodeString() |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + i.Value = "ptr:" + str |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +// TestNestedUnmarshalerPointer tests UnmarshalMaxMindDB with pointer fields. |
| 82 | +func TestNestedUnmarshalerPointer(t *testing.T) { |
| 83 | + type Outer struct { |
| 84 | + Field *testInnerPointer |
| 85 | + Name string |
| 86 | + } |
| 87 | + |
| 88 | + // Test data |
| 89 | + data := []byte{ |
| 90 | + // Map with 2 items |
| 91 | + 0xe2, |
| 92 | + // Key "Field" |
| 93 | + 0x45, 'F', 'i', 'e', 'l', 'd', |
| 94 | + // Value "test" |
| 95 | + 0x44, 't', 'e', 's', 't', |
| 96 | + // Key "Name" |
| 97 | + 0x44, 'N', 'a', 'm', 'e', |
| 98 | + // Value "example" |
| 99 | + 0x47, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 100 | + } |
| 101 | + |
| 102 | + t.Run("pointer field with UnmarshalMaxMindDB", func(t *testing.T) { |
| 103 | + d := New(data) |
| 104 | + var result Outer |
| 105 | + err := d.Decode(0, &result) |
| 106 | + require.NoError(t, err) |
| 107 | + |
| 108 | + // The pointer should be created and unmarshaled with custom unmarshaler |
| 109 | + require.NotNil(t, result.Field) |
| 110 | + require.True( |
| 111 | + t, |
| 112 | + result.Field.custom, |
| 113 | + "Custom unmarshaler should be called for pointer fields", |
| 114 | + ) |
| 115 | + require.Equal(t, "ptr:test", result.Field.Value) |
| 116 | + require.Equal(t, "example", result.Name) |
| 117 | + }) |
| 118 | +} |
| 119 | + |
| 120 | +// testItem with UnmarshalMaxMindDB for slice test. |
| 121 | +type testItem struct { |
| 122 | + ID int |
| 123 | + custom bool |
| 124 | +} |
| 125 | + |
| 126 | +func (item *testItem) UnmarshalMaxMindDB(d *Decoder) error { |
| 127 | + item.custom = true |
| 128 | + id, err := d.DecodeUInt32() |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + item.ID = int(id) * 2 |
| 133 | + return nil |
| 134 | +} |
| 135 | + |
| 136 | +// TestNestedUnmarshalerInSlice tests UnmarshalMaxMindDB for slice elements. |
| 137 | +func TestNestedUnmarshalerInSlice(t *testing.T) { |
| 138 | + type Container struct { |
| 139 | + Items []testItem |
| 140 | + } |
| 141 | + |
| 142 | + // Test data: a map with "Items" -> [1, 2, 3] |
| 143 | + data := []byte{ |
| 144 | + // Map with 1 item (TypeMap=7 << 5 | size=1) |
| 145 | + 0xe1, |
| 146 | + // Key "Items" (TypeString=2 << 5 | size=5) |
| 147 | + 0x45, 'I', 't', 'e', 'm', 's', |
| 148 | + // Slice with 3 items - TypeSlice=11, which is > 7, so we need extended type |
| 149 | + // Extended type: ctrl_byte = (TypeExtended << 5) | size = (0 << 5) | 3 = 0x03 |
| 150 | + // Next byte: TypeSlice - 7 = 11 - 7 = 4 |
| 151 | + 0x03, 0x04, |
| 152 | + // Value 1 (TypeUint32=6 << 5 | size=1) |
| 153 | + 0xc1, 0x01, |
| 154 | + // Value 2 (TypeUint32=6 << 5 | size=1) |
| 155 | + 0xc1, 0x02, |
| 156 | + // Value 3 (TypeUint32=6 << 5 | size=1) |
| 157 | + 0xc1, 0x03, |
| 158 | + } |
| 159 | + |
| 160 | + t.Run("slice elements with UnmarshalMaxMindDB", func(t *testing.T) { |
| 161 | + d := New(data) |
| 162 | + var result Container |
| 163 | + err := d.Decode(0, &result) |
| 164 | + require.NoError(t, err) |
| 165 | + |
| 166 | + require.Len(t, result.Items, 3) |
| 167 | + // With custom unmarshaler, values should be doubled |
| 168 | + require.True( |
| 169 | + t, |
| 170 | + result.Items[0].custom, |
| 171 | + "Custom unmarshaler should be called for slice elements", |
| 172 | + ) |
| 173 | + require.Equal(t, 2, result.Items[0].ID) // 1 * 2 |
| 174 | + require.Equal(t, 4, result.Items[1].ID) // 2 * 2 |
| 175 | + require.Equal(t, 6, result.Items[2].ID) // 3 * 2 |
| 176 | + }) |
| 177 | +} |
| 178 | + |
| 179 | +// testValue with UnmarshalMaxMindDB for map test. |
| 180 | +type testValue struct { |
| 181 | + Data string |
| 182 | + custom bool |
| 183 | +} |
| 184 | + |
| 185 | +func (v *testValue) UnmarshalMaxMindDB(d *Decoder) error { |
| 186 | + v.custom = true |
| 187 | + str, err := d.DecodeString() |
| 188 | + if err != nil { |
| 189 | + return err |
| 190 | + } |
| 191 | + v.Data = "map:" + str |
| 192 | + return nil |
| 193 | +} |
| 194 | + |
| 195 | +// TestNestedUnmarshalerInMap tests UnmarshalMaxMindDB for map values. |
| 196 | +func TestNestedUnmarshalerInMap(t *testing.T) { |
| 197 | + // Test data: {"key1": "value1", "key2": "value2"} |
| 198 | + data := []byte{ |
| 199 | + // Map with 2 items |
| 200 | + 0xe2, |
| 201 | + // Key "key1" |
| 202 | + 0x44, 'k', 'e', 'y', '1', |
| 203 | + // Value "value1" |
| 204 | + 0x46, 'v', 'a', 'l', 'u', 'e', '1', |
| 205 | + // Key "key2" |
| 206 | + 0x44, 'k', 'e', 'y', '2', |
| 207 | + // Value "value2" |
| 208 | + 0x46, 'v', 'a', 'l', 'u', 'e', '2', |
| 209 | + } |
| 210 | + |
| 211 | + t.Run("map values with UnmarshalMaxMindDB", func(t *testing.T) { |
| 212 | + d := New(data) |
| 213 | + var result map[string]testValue |
| 214 | + err := d.Decode(0, &result) |
| 215 | + require.NoError(t, err) |
| 216 | + |
| 217 | + require.Len(t, result, 2) |
| 218 | + require.True(t, result["key1"].custom, "Custom unmarshaler should be called for map values") |
| 219 | + require.Equal(t, "map:value1", result["key1"].Data) |
| 220 | + require.Equal(t, "map:value2", result["key2"].Data) |
| 221 | + }) |
| 222 | +} |
0 commit comments