maxmind / MaxMind-DB-Writer-perl

Create MaxMind DB database files
https://metacpan.org/release/MaxMind-DB-Writer/
Other
77 stars 27 forks source link

Can't use string as a HASH ref in DB/Writer/Tree.pm #88

Closed thevirus20 closed 7 years ago

thevirus20 commented 7 years ago

How to deal with the following?

Can't use string ("101.215.51.0/24") as a HASH ref while "strict refs" in use at /Library/Perl/5.18/darwin-thread-multi-2level/MaxMind/DB/Writer/Tree.pm line 203.

A simple ip like "1.2.3.4/24" works well.

oschwald commented 7 years ago

Please share a short code snippet that demonstrates the issue. Based on the error, it sounds like you are passing in the IP address as the $args parameter to insert_network, not the $network parameter.

thevirus20 commented 7 years ago
use MaxMind::DB::Writer::Tree;
my %types = (
    location  => [ 'array', 'utf8_string' ],
    latitude => 'utf8_string',
    longitude => 'utf8_string',
    country => 'utf8_string',
    region_name => 'utf8_string',
    city_name => 'utf8_string',
    timezone => 'utf8_string',
);

my $tree = MaxMind::DB::Writer::Tree->new(
    ip_version            => 4,
    record_size           => 24,
    database_type         => 'My-IP-Data',
    languages             => ['en'],
    description           => { en => 'My database of IP data' },
    map_key_type_callback => sub { $types{ $_[0] } },
    remove_reserved_networks => 0,
    #merge_record_collisions => 1,
    #merge_strategy =>'toplevel',
);
$tree->insert_network(
'1.2.3.1/24',
{
    location  =>  [-73.60901129999999,45.5142856],
            latitude => '45.5142856',
            longitude => '-73.60901129999999',
            country => 'Canada',
            region_name => 'Quebec',
            city_name => 'Outremont',
            timezone => 'America/New_York',
},

    '101.215.51.0/24',

    {
        location  =>  [-73.60901129999999,45.5142856],
        latitude => '45.5142856',
        longitude => '-73.60901129999999',
        country => 'Canada',
        region_name => 'Quebec',
        city_name => 'Outremont',
        timezone => 'America/New_York',
    },

);
open my $fh, '>:raw', 'mmdbs/final-my-ip-data.mmdb';
$tree->write_tree($fh);
print "writing finished \n;"

The above code works well when only ip 1.2.3.1/24 is used in generating mmdb file. Also please how can I correctly pass the merge_strategy so that ip are not overriden but merged. Thanks.

thevirus20 commented 7 years ago

Never mind, I just separated both the ips in separate insert_network call and it worked. Changed code:

$tree->insert_network(
'1.2.3.1/24',
{
    location  =>  [-73.60901129999999,45.5142856],
            latitude => '45.5142856',
            longitude => '-73.60901129999999',
            country => 'Canada',
            region_name => 'Quebec',
            city_name => 'Outremont',
            timezone => 'America/New_York',
},
);
$tree->insert_network(
    '101.215.51.0/24',
    {
        location  =>  [-73.60901129999999,45.5142856],
        latitude => '45.5142856',
        longitude => '-73.60901129999999',
        country => 'Canada',
        region_name => 'Quebec',
        city_name => 'Outremont',
        timezone => 'America/New_York',
    },
);

I would now like to know what it the correct way of doing it. Please clarify on merge_strategy for merging ip without overriding.