diff --git a/deserializer.go deserializer_test.go b/deserializer.go deserializer_test.go new file mode 100644 index 0000000..8b15fee --- /dev/null +++ b/deserializer.go deserializer_test.go @@ -0,0 +1,119 @@ +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 interface{} + curNum int +} + +type testDeserializer struct { + stack []*stackValue + rv interface{} + key *string +} + +func (d *testDeserializer) ShouldSkip(offset uintptr) (bool, error) { + return false, nil +} + +func (d *testDeserializer) StartSlice(size uint) error { + return d.add(make([]interface{}, size)) +} + +func (d *testDeserializer) StartMap(size uint) error { + return d.add(map[string]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 interface{}) 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]interface{}: + if d.key == nil { + key := v.(string) + d.key = &key + } else { + parent[*d.key] = v + d.key = nil + } + + case []interface{}: + parent[top.curNum] = v + top.curNum++ + default: + } + } + + switch v := v.(type) { + case map[string]interface{}, []interface{}: + d.stack = append(d.stack, &stackValue{value: v}) + default: + } + + return nil +} diff --git a/go.sum b/go.sum index 45bf757..ae4f4e7 100644 --- a/go.sum +++ b/go.sum @@ -4,19 +4,11 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.5.0 h1:DMOzIV76tmoDNE9pX6RSN0aDtCYeCg5VueieJaAo1uw= -github.com/stretchr/testify v1.5.0/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= -github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/sys v0.0.0-20191224085550-c709ea063b76 h1:Dho5nD6R3PcW2SH1or8vS0dszDaXRxIw55lBX7XiE5g= golang.org/x/sys v0.0.0-20191224085550-c709ea063b76/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/reader_test.go b/reader_test.go index 863462d..19031c2 100644 --- a/reader_test.go +++ b/reader_test.go @@ -203,6 +203,10 @@ func TestDecodingToInterface(t *testing.T) { err = reader.Lookup(net.ParseIP("::1.1.1.0"), &recordInterface) require.NoError(t, err, "unexpected error while doing lookup: %v", err) + checkDecodingToInterface(t, recordInterface) +} + +func checkDecodingToInterface(t *testing.T, recordInterface interface{}) { record := recordInterface.(map[string]interface{}) assert.Equal(t, []interface{}{uint64(1), uint64(2), uint64(3)}, record["array"]) assert.Equal(t, true, record["boolean"])