Skip to content

Commit c14c7fb

Browse files
committed
Allow pointers to pointers in result struct
This variantion seems to perform somewhat better than the simpler recursive version.
1 parent 5a39e6c commit c14c7fb

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

decoder.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ func (d *decoder) sizeFromCtrlByte(ctrlByte byte, offset uint, typeNum dataType)
8484
}
8585

8686
func (d *decoder) decodeFromType(dtype dataType, size uint, offset uint, result reflect.Value) (uint, error) {
87-
if result.Kind() == reflect.Ptr {
87+
for {
88+
if result.Kind() != reflect.Ptr {
89+
break
90+
}
8891
if result.IsNil() {
8992
result.Set(reflect.New(result.Type().Elem()))
9093
}

reader_test.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,18 +167,20 @@ type PointerMap struct {
167167
}
168168

169169
type TestPointerType struct {
170-
Array *[]uint `maxminddb:"array"`
171-
Boolean *bool `maxminddb:"boolean"`
172-
Bytes *[]byte `maxminddb:"bytes"`
173-
Double *float64 `maxminddb:"double"`
174-
Float *float32 `maxminddb:"float"`
175-
Int32 *int32 `maxminddb:"int32"`
176-
Map *PointerMap `maxminddb:"map"`
177-
Uint16 *uint16 `maxminddb:"uint16"`
178-
Uint32 *uint32 `maxminddb:"uint32"`
179-
Uint64 *uint64 `maxminddb:"uint64"`
180-
Uint128 *big.Int `maxminddb:"uint128"`
181-
Utf8String *string `maxminddb:"utf8_string"`
170+
Array *[]uint `maxminddb:"array"`
171+
Boolean *bool `maxminddb:"boolean"`
172+
Bytes *[]byte `maxminddb:"bytes"`
173+
Double *float64 `maxminddb:"double"`
174+
Float *float32 `maxminddb:"float"`
175+
Int32 *int32 `maxminddb:"int32"`
176+
Map *PointerMap `maxminddb:"map"`
177+
Uint16 *uint16 `maxminddb:"uint16"`
178+
Uint32 *uint32 `maxminddb:"uint32"`
179+
180+
// Test for pointer to pointer
181+
Uint64 **uint64 `maxminddb:"uint64"`
182+
Uint128 *big.Int `maxminddb:"uint128"`
183+
Utf8String *string `maxminddb:"utf8_string"`
182184
}
183185

184186
func (s *MySuite) TestStructWithPointer(c *C) {
@@ -206,7 +208,7 @@ func (s *MySuite) TestStructWithPointer(c *C) {
206208

207209
c.Assert(*result.Uint16, Equals, uint16(100))
208210
c.Assert(*result.Uint32, Equals, uint32(268435456))
209-
c.Assert(*result.Uint64, Equals, uint64(1152921504606846976))
211+
c.Assert(**result.Uint64, Equals, uint64(1152921504606846976))
210212
c.Assert(*result.Utf8String, Equals, "unicode! ☯ - ♫")
211213
bigInt := new(big.Int)
212214
bigInt.SetString("1329227995784915872903807060280344576", 10)

0 commit comments

Comments
 (0)