File: CodeGen.pm

package info (click to toggle)
libgoogle-protocolbuffers-perl 0.12-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 388 kB
  • sloc: perl: 1,960; makefile: 2
file content (151 lines) | stat: -rwxr-xr-x 4,357 bytes parent folder | download
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package Google::ProtocolBuffers::CodeGen;
use strict;
use warnings;

use Google::ProtocolBuffers::Constants qw/:types :labels :complex_types/;

my %primitive_types = reverse (
    TYPE_DOUBLE => TYPE_DOUBLE,
    TYPE_FLOAT  => TYPE_FLOAT,
    TYPE_INT64  => TYPE_INT64,
    TYPE_UINT64 => TYPE_UINT64,
    TYPE_INT32  => TYPE_INT32,
    TYPE_FIXED64=> TYPE_FIXED64,
    TYPE_FIXED32=> TYPE_FIXED32,
    TYPE_BOOL   => TYPE_BOOL,
    TYPE_STRING => TYPE_STRING,
    TYPE_GROUP  => TYPE_GROUP,      ## 
    TYPE_MESSAGE=> TYPE_MESSAGE,    ## should never appear, because 'message' is a 'complex type'
    TYPE_BYTES  => TYPE_BYTES,
    TYPE_UINT32 => TYPE_UINT32,
    TYPE_ENUM   => TYPE_ENUM,       ## 
    TYPE_SFIXED32=>TYPE_SFIXED32,
    TYPE_SFIXED64=>TYPE_SFIXED64,
    TYPE_SINT32 => TYPE_SINT32,
    TYPE_SINT64 => TYPE_SINT64,
);

my %labels = reverse (
    LABEL_OPTIONAL  => LABEL_OPTIONAL,
    LABEL_REQUIRED  => LABEL_REQUIRED,
    LABEL_REPEATED  => LABEL_REPEATED,
);

sub _get_perl_literal {
    my $v = shift;
    my $opts = shift;
   
    if ($v =~ /^-?\d+$/) {
        ## integer literal
        if ($v>0x7fff_ffff || $v<-0x8000_0000) {
            return "Math::BigInt->new('$v')";
        } else {
            return "$v";
        }
     } elsif ($v =~ /[-+]?\d*\.\d+([Ee][\+-]?\d+)?|[-+]?\d+[Ee][\+-]?\d+/i) {        
        ## floating point literal
        return "$v";
    } else {
        ## string literal
        $v =~ s/([\x00-\x1f'"\\$@%\x80-\xff])/ '\\x{' . sprintf("%02x", ord($1)) . '}' /ge;
        return qq["$v"];
    } 
}

sub generate_code_of_enum {
    my $self = shift;
    my $opts = shift;
    
    my $class_name = ref($self) || $self;
    my $fields_text;
    foreach my $f (@{ $self->_pb_fields_list }) {
        my ($name, $value) = @$f;
        $value = _get_perl_literal($value, $opts); 
        $fields_text .= "               ['$name', $value],\n";
    }
    
    return <<"CODE";
    unless ($class_name->can('_pb_fields_list')) {
        Google::ProtocolBuffers->create_enum(
            '$class_name',
            [
$fields_text
            ]
        );
    }
    
CODE
}


sub generate_code_of_message_or_group {
    my $self = shift;
    my $opts = shift;

    my $create_what = 
        ($self->_pb_complex_type_kind==MESSAGE) ? 'create_message' :
        ($self->_pb_complex_type_kind==GROUP)   ? 'create_group'   : die;
                        
    my $class_name = ref($self) || $self;
    
    my $fields_text = ''; # may be empty, as empty messages are allowed
    foreach my $f (@{ $self->_pb_fields_list }) {
        my ($label, $type, $name, $field_number, $default_value) = @$f;
        
        die unless $labels{$label};
        $label = "Google::ProtocolBuffers::Constants::$labels{$label}()";

        if ($primitive_types{$type}) {
            $type = "Google::ProtocolBuffers::Constants::$primitive_types{$type}()";
        } else {
            $type = "'$type'";
        }
        
        $default_value = (defined $default_value) ? 
            _get_perl_literal($default_value, $opts) : 'undef'; 
        $fields_text .= <<"FIELD";             
                [
                    $label, 
                    $type, 
                    '$name', $field_number, $default_value
                ],
FIELD
    }

    my $oneofs_text = "            undef,\n";
    if ($self->can('_pb_oneofs')) {
        $oneofs_text = "            {\n";
        while (my ($name, $fields) = each %{$self->_pb_oneofs}) {
            $oneofs_text .= "                '$name' => [\n";
            foreach my $f (@$fields) {
                $oneofs_text .= "                    '$f',\n";
            }
            $oneofs_text .= "                ],\n";
        }
        $oneofs_text .= "            },\n";
    }

    my $options = '';
    foreach my $opt_name (qw/create_accessors follow_best_practice/) {
        if ($opts->{$opt_name}) {
            $options .= "'$opt_name' => 1, "    
        }
    }
    
    return <<"CODE";
    unless ($class_name->can('_pb_fields_list')) {
        Google::ProtocolBuffers->$create_what(
            '$class_name',
            [
$fields_text
            ],
$oneofs_text
            { $options }
        );
    }

CODE

}

1;