-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from oschwald/greg/custom-deserializer
Support custom deserializer
- Loading branch information
Showing
6 changed files
with
294 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package maxminddb | ||
|
||
import "math/big" | ||
|
||
// deserializer is an interface for a type that deserializes an MaxMind DB | ||
// data record to some other type. This exists as an alternative to the | ||
// standard reflection API. | ||
// | ||
// This is fundamentally different than the Unmarshaler interface that | ||
// several packages provide. A Deserializer will generally create the | ||
// final struct or value rather than unmarshaling to itself. | ||
// | ||
// This interface and the associated unmarshaling code is EXPERIMENTAL! | ||
// It is not currently covered by any Semantic Versioning guarantees. | ||
// Use at your own risk. | ||
type deserializer interface { | ||
ShouldSkip(offset uintptr) (bool, error) | ||
StartSlice(size uint) error | ||
StartMap(size uint) error | ||
End() error | ||
String(string) error | ||
Float64(float64) error | ||
Bytes([]byte) error | ||
Uint16(uint16) error | ||
Uint32(uint32) error | ||
Int32(int32) error | ||
Uint64(uint64) error | ||
Uint128(*big.Int) error | ||
Bool(bool) error | ||
Float32(float32) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters