-
Notifications
You must be signed in to change notification settings - Fork 105
Closed
Description
Hello!
With a relatively recent maxmind country-level DB (let me know if you want my exact copy), we have found at least one IP address that decodes to a different country code based on which de-serialization type is used. This is a bit concerning (we expect it has something to do with registered vs represented countries but we haven't nailed it down).
Here's the difference:
var data map[string]map[string]any
reader.Lookup("95.140.157.134", &data)
fmt.Println(data["country"]["iso_code"])
This says Russia.
var data struct {
Country struct {
IsoCode string `maxminddb:"iso_code"`
} `maxminddb:"country"`
}
reader.Lookup("95.140.157.134", &data)
fmt.Println(data.Country.IsoCode)
This says Poland.
What's going on here?
Activity
jtolio commentedon Jul 31, 2023
Additional information:
If I do the first style (map-based decoding), both "country" and "registered_country" say Russia.
However, if I do the second style (struct-based decoding), "country" is Poland, and "registered_country" is Russia.
So I think map-style is broken.
oschwald commentedon Jul 31, 2023
I can't reproduce this:
Output:
I am guessing you are ignoring the error from
Lookup
, which might be related. I had to change the type to something that it could decode to.jtolio commentedon Jul 31, 2023
I only removed the error handling for the purposes of this issue submission. Here is a full test for me:
This outputs:
Notably, your formulation (
map[string]any
instead ofmap[string]map[string]any
) works as expected.oschwald commentedon Jul 31, 2023
As suggested above, your version fails for me. I receive:
This is because the subdivisions are an array, not map.
If I ignore the error, I see the behavior you describe. However, I wouldn't expect there to be valid data set by a method that returns an error and I wouldn't consider that a bug.
jtolio commentedon Aug 1, 2023
This fails, even with the latest tag, v1.11.0? I didn't think to try the latest dev branch, but I will do that tomorrow. Perhaps there is some difference between your branch and the tagged release I am using. All of the returned errors are nil in my code--none of the panics in my example code above are triggered. Could this difference be in the format of the mmdb file I have?
oschwald commentedon Aug 1, 2023
I have found an issue that I think is the cause of what you are seeing. I'll do a release shortly.
oschwald commentedon Aug 1, 2023
1.12.0 has been released.
That said, I wouldn't recommend decoding to a
map[string]map[string]any
. It is much slower than decoding to a struct that contains just the data you want and it is more fragile than decoding tomap[string]any
as it makes possibly unfounded assumptions about what is in the database.jtolio commentedon Aug 1, 2023
Great, thank you! 1.12.0 is fixed. We do use the struct approach, but upon discovering this discrepancy we were worried which method was correct.