File: Serializer.pm

package info (click to toggle)
libmaxmind-db-writer-perl 0.300003-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,336 kB
  • sloc: ansic: 3,059; perl: 2,895; makefile: 5; sh: 4
file content (91 lines) | stat: -rw-r--r-- 2,279 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package Test::MaxMind::DB::Writer::Serializer;

use strict;
use warnings;

use List::AllUtils qw( all natatime );
use MaxMind::DB::Writer::Serializer;
use Scalar::Util qw( blessed );
use Test::Bits;
use Test::More;

use Exporter qw( import );

our @EXPORT_OK = qw(
    test_encoding_of_type
);

sub test_encoding_of_type {
    my $type  = shift;
    my $tests = shift;

    my $iter = natatime 2, @{$tests};
    while ( my ( $input, $expect ) = $iter->() ) {
        my $desc = "packed $type - ";

        if ( ref $input && !blessed $input ) {
            $desc .=
                ref $input eq 'HASH'
                ? 'hash with ' . ( scalar keys %{$input} ) . ' keys'
                : 'array with ' . ( scalar @{$input} ) . ' keys';
        }
        else {
            $desc .=
                length($input) > 50
                ? substr( $input, 0, 50 ) . '...(' . length($input) . ')'
                : $input;
        }

        my $serializer = MaxMind::DB::Writer::Serializer->new(
            _deduplicate_data     => 0,
            map_key_type_callback => \&_map_key_type,
        );

        $serializer->store_data(
            $type,
            $input,
            ( $type eq 'array' ? 'utf8_string' : () ),
        );

        bits_is(
            ${ $serializer->buffer() },
            $expect,
            $desc
        );
    }
}

my %geoip_keys = (
    area_code   => 'utf8_string',
    description => 'map',
    geonames_id => 'uint32',
    latitude    => 'double',
    location_id => 'uint32',
    longitude   => 'double',
    metro_code  => 'uint16',
    name        => 'map',
    postal_code => 'utf8_string',
);

my %metadata_keys = (
    binary_format_major_version => 'uint16',
    binary_format_minor_version => 'uint16',
    build_epoch                 => 'uint64',
    database_type               => 'utf8_string',
    description                 => 'map',
    ip_version                  => 'uint16',
    languages                   => [ 'array', 'utf8_string' ],
    node_count                  => 'uint32',
    record_size                 => 'uint32',
);

sub _map_key_type {
    my $key = shift;

    # locale id
    return 'utf8_string' if $key =~ /^[a-z]{2,3}(?:-[A-Z]{2})?$/;

    return $geoip_keys{$key} || $metadata_keys{$key};
}

1;