blob: 9fbb92f5b6953a75348faa02c41e4c9b62240aad [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Mark Sleee9ce01c2007-05-16 02:29:53 +000019
David Reissfb635332008-03-27 21:42:30 +000020#include <string>
21#include <fstream>
22#include <iostream>
23#include <vector>
24#include <list>
25
Mark Slee2c44d202007-05-16 02:18:07 +000026#include <stdlib.h>
27#include <sys/stat.h>
28#include <sstream>
David Reissfb635332008-03-27 21:42:30 +000029#include "t_oop_generator.h"
David Reiss204420f2008-01-11 20:59:03 +000030#include "platform.h"
Mark Slee2c44d202007-05-16 02:18:07 +000031using namespace std;
32
David Reissfb635332008-03-27 21:42:30 +000033
34/**
35 * PERL code generator.
36 *
David Reissfb635332008-03-27 21:42:30 +000037 */
38class t_perl_generator : public t_oop_generator {
39 public:
40 t_perl_generator(
41 t_program* program,
42 const std::map<std::string, std::string>& parsed_options,
43 const std::string& option_string)
44 : t_oop_generator(program)
45 {
46 out_dir_base_ = "gen-perl";
David Reiss82e6fc02009-03-26 23:32:36 +000047 escape_['$'] = "\\$";
48 escape_['@'] = "\\@";
David Reissfb635332008-03-27 21:42:30 +000049 }
50
51 /**
52 * Init and close methods
53 */
54
55 void init_generator();
56 void close_generator();
57
58 /**
59 * Program-level generation functions
60 */
61
62 void generate_typedef (t_typedef* ttypedef);
63 void generate_enum (t_enum* tenum);
64 void generate_const (t_const* tconst);
65 void generate_struct (t_struct* tstruct);
66 void generate_xception (t_struct* txception);
67 void generate_service (t_service* tservice);
68
69 std::string render_const_value(t_type* type, t_const_value* value);
70
71 /**
72 * Structs!
73 */
74
75 void generate_perl_struct(t_struct* tstruct, bool is_exception);
76 void generate_perl_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false);
77 void generate_perl_struct_reader(std::ofstream& out, t_struct* tstruct);
78 void generate_perl_struct_writer(std::ofstream& out, t_struct* tstruct);
79 void generate_perl_function_helpers(t_function* tfunction);
80
81 /**
82 * Service-level generation functions
83 */
84
85 void generate_service_helpers (t_service* tservice);
86 void generate_service_interface (t_service* tservice);
87 void generate_service_rest (t_service* tservice);
88 void generate_service_client (t_service* tservice);
89 void generate_service_processor (t_service* tservice);
90 void generate_process_function (t_service* tservice, t_function* tfunction);
91
92 /**
93 * Serialization constructs
94 */
95
96 void generate_deserialize_field (std::ofstream &out,
97 t_field* tfield,
98 std::string prefix="",
99 bool inclass=false);
100
101 void generate_deserialize_struct (std::ofstream &out,
102 t_struct* tstruct,
103 std::string prefix="");
104
105 void generate_deserialize_container (std::ofstream &out,
106 t_type* ttype,
107 std::string prefix="");
108
109 void generate_deserialize_set_element (std::ofstream &out,
110 t_set* tset,
111 std::string prefix="");
112
113 void generate_deserialize_map_element (std::ofstream &out,
114 t_map* tmap,
115 std::string prefix="");
116
117 void generate_deserialize_list_element (std::ofstream &out,
118 t_list* tlist,
119 std::string prefix="");
120
121 void generate_serialize_field (std::ofstream &out,
122 t_field* tfield,
123 std::string prefix="");
124
125 void generate_serialize_struct (std::ofstream &out,
126 t_struct* tstruct,
127 std::string prefix="");
128
129 void generate_serialize_container (std::ofstream &out,
130 t_type* ttype,
131 std::string prefix="");
132
133 void generate_serialize_map_element (std::ofstream &out,
134 t_map* tmap,
135 std::string kiter,
136 std::string viter);
137
138 void generate_serialize_set_element (std::ofstream &out,
139 t_set* tmap,
140 std::string iter);
141
142 void generate_serialize_list_element (std::ofstream &out,
143 t_list* tlist,
144 std::string iter);
145
146 /**
147 * Helper rendering functions
148 */
149
150 std::string perl_includes();
151 std::string declare_field(t_field* tfield, bool init=false, bool obj=false);
152 std::string function_signature(t_function* tfunction, std::string prefix="");
153 std::string argument_list(t_struct* tstruct);
154 std::string type_to_enum(t_type* ttype);
155
156 std::string autogen_comment() {
157 return
158 std::string("#\n") +
159 "# Autogenerated by Thrift\n" +
160 "#\n" +
161 "# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" +
162 "#\n";
163 }
164
165 void perl_namespace_dirs(t_program* p, std::list<std::string>& dirs) {
David Reiss07ef3a92008-03-27 21:42:39 +0000166 std::string ns = p->get_namespace("perl");
David Reissfb635332008-03-27 21:42:30 +0000167 std::string::size_type loc;
168
169 if (ns.size() > 0) {
170 while ((loc = ns.find(".")) != std::string::npos) {
171 dirs.push_back(ns.substr(0, loc));
172 ns = ns.substr(loc+1);
173 }
174 }
175
176 if (ns.size() > 0) {
177 dirs.push_back(ns);
178 }
179 }
180
181 std::string perl_namespace(t_program* p) {
David Reiss07ef3a92008-03-27 21:42:39 +0000182 std::string ns = p->get_namespace("perl");
David Reissfb635332008-03-27 21:42:30 +0000183 std::string result = "";
184 std::string::size_type loc;
185
186 if (ns.size() > 0) {
187 while ((loc = ns.find(".")) != std::string::npos) {
188 result += ns.substr(0, loc);
189 result += "::";
190 ns = ns.substr(loc+1);
191 }
192
193 if (ns.size() > 0) {
194 result += ns + "::";
195 }
196 }
197
198 return result;
199 }
200
T Jake Luciani41687fc2008-12-23 03:45:43 +0000201 std::string get_namespace_out_dir() {
202 std::string outdir = get_out_dir();
203 std::list<std::string> dirs;
204 perl_namespace_dirs(program_, dirs);
205 std::list<std::string>::iterator it;
206 for (it = dirs.begin(); it != dirs.end(); it++) {
207 outdir += *it + "/";
208 }
209 return outdir;
210 }
211
David Reissfb635332008-03-27 21:42:30 +0000212 private:
213
214 /**
215 * File streams
216 */
217 std::ofstream f_types_;
218 std::ofstream f_consts_;
219 std::ofstream f_helpers_;
220 std::ofstream f_service_;
221
222};
223
224
Mark Slee2c44d202007-05-16 02:18:07 +0000225/**
226 * Prepares for file generation by opening up the necessary file output
227 * streams.
228 *
229 * @param tprogram The program to generate
230 */
231void t_perl_generator::init_generator() {
232 // Make output directory
David Reiss204420f2008-01-11 20:59:03 +0000233 MKDIR(get_out_dir().c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000234
dweatherford65b70752007-10-31 02:18:14 +0000235 string outdir = get_out_dir();
David Reiss4b83d6d2008-03-27 19:45:19 +0000236 std::list<std::string> dirs;
237 perl_namespace_dirs(program_, dirs);
238 std::list<std::string>::iterator it;
239 for (it = dirs.begin(); it != dirs.end(); it++) {
240 outdir += *it + "/";
241 MKDIR(outdir.c_str());
Mark Slee27ed6ec2007-08-16 01:26:31 +0000242 }
243
Mark Slee2c44d202007-05-16 02:18:07 +0000244 // Make output file
dweatherford65b70752007-10-31 02:18:14 +0000245 string f_types_name = outdir+"Types.pm";
Mark Slee2c44d202007-05-16 02:18:07 +0000246 f_types_.open(f_types_name.c_str());
dweatherford65b70752007-10-31 02:18:14 +0000247 string f_consts_name = outdir+"Constants.pm";
Mark Slee2c44d202007-05-16 02:18:07 +0000248 f_consts_.open(f_consts_name.c_str());
249
250 // Print header
251 f_types_ <<
252 autogen_comment() <<
253 perl_includes();
254
255 // Print header
256 f_consts_ <<
David Reissc5c54252008-04-03 23:16:46 +0000257 autogen_comment() <<
258 "package "<< perl_namespace(program_) <<"Constants;"<<endl<<
259 perl_includes() <<
260 endl;
Mark Slee2c44d202007-05-16 02:18:07 +0000261}
262
263/**
264 * Prints standard java imports
265 */
266string t_perl_generator::perl_includes() {
267 string inc;
268
269 inc = "require 5.6.0;\n";
270 inc += "use strict;\n";
271 inc += "use warnings;\n";
272 inc += "use Thrift;\n\n";
Mark Slee27ed6ec2007-08-16 01:26:31 +0000273
Mark Slee2c44d202007-05-16 02:18:07 +0000274 return inc;
275}
276
277/**
278 * Close up (or down) some filez.
279 */
280void t_perl_generator::close_generator() {
281 // Close types file
282 f_types_ << "1;" << endl;
283 f_types_.close();
284
285 f_consts_ << "1;" << endl;
286 f_consts_.close();
287}
288
289/**
290 * Generates a typedef. This is not done in PERL, types are all implicit.
291 *
292 * @param ttypedef The type definition
293 */
294void t_perl_generator::generate_typedef(t_typedef* ttypedef) {}
295
296/**
297 * Generates code for an enumerated type. Since define is expensive to lookup
298 * in PERL, we use a global array for this.
299 *
300 * @param tenum The enumeration
301 */
302void t_perl_generator::generate_enum(t_enum* tenum) {
T Jake Luciani41687fc2008-12-23 03:45:43 +0000303 f_types_ << "package " << perl_namespace(program_) <<tenum->get_name()<<";"<<endl;
Mark Slee2c44d202007-05-16 02:18:07 +0000304
305 vector<t_enum_value*> constants = tenum->get_constants();
306 vector<t_enum_value*>::iterator c_iter;
307 int value = -1;
308 for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
309 if ((*c_iter)->has_value()) {
310 value = (*c_iter)->get_value();
311 } else {
312 ++value;
313 }
Mark Slee27ed6ec2007-08-16 01:26:31 +0000314
Mark Slee2c44d202007-05-16 02:18:07 +0000315 f_types_ << "use constant "<<(*c_iter)->get_name() << " => " << value << ";" << endl;
316 }
317}
318
319/**
320 * Generate a constant value
321 */
322void t_perl_generator::generate_const(t_const* tconst) {
323 t_type* type = tconst->get_type();
324 string name = tconst->get_name();
325 t_const_value* value = tconst->get_value();
326
327 f_consts_ << "use constant " << name << " => ";
328 f_consts_ << render_const_value(type, value);
329 f_consts_ << ";" << endl << endl;
330}
331
332/**
333 * Prints the value of a constant with the given type. Note that type checking
334 * is NOT performed in this function as it is always run beforehand using the
335 * validate_types method in main.cc
336 */
337string t_perl_generator::render_const_value(t_type* type, t_const_value* value) {
338 std::ostringstream out;
339
David Reisse087a302007-08-23 21:43:25 +0000340 type = get_true_type(type);
Mark Slee2c44d202007-05-16 02:18:07 +0000341
342 if (type->is_base_type()) {
343 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
344 switch (tbase) {
345 case t_base_type::TYPE_STRING:
David Reiss82e6fc02009-03-26 23:32:36 +0000346 out << '"' << get_escaped_string(value) << '"';
Mark Slee2c44d202007-05-16 02:18:07 +0000347 break;
348 case t_base_type::TYPE_BOOL:
349 out << (value->get_integer() > 0 ? "1" : "0");
350 break;
351 case t_base_type::TYPE_BYTE:
352 case t_base_type::TYPE_I16:
353 case t_base_type::TYPE_I32:
354 case t_base_type::TYPE_I64:
355 out << value->get_integer();
356 break;
357 case t_base_type::TYPE_DOUBLE:
358 if (value->get_type() == t_const_value::CV_INTEGER) {
359 out << value->get_integer();
360 } else {
361 out << value->get_double();
362 }
363 break;
364 default:
David Reissdd7796f2007-08-28 21:09:06 +0000365 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
Mark Slee2c44d202007-05-16 02:18:07 +0000366 }
367 } else if (type->is_enum()) {
368 out << value->get_integer();
369 } else if (type->is_struct() || type->is_xception()) {
370 out << "new " << perl_namespace(type->get_program()) << type->get_name() << "({" << endl;
371 indent_up();
372 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
373 vector<t_field*>::const_iterator f_iter;
374 const map<t_const_value*, t_const_value*>& val = value->get_map();
375 map<t_const_value*, t_const_value*>::const_iterator v_iter;
376 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
377 t_type* field_type = NULL;
378 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
379 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
380 field_type = (*f_iter)->get_type();
381 }
382 }
383 if (field_type == NULL) {
384 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
385 }
386 out << render_const_value(g_type_string, v_iter->first);
387 out << " => ";
388 out << render_const_value(field_type, v_iter->second);
389 out << endl;
390 }
391
392 out << "})";
393 } else if (type->is_map()) {
394 t_type* ktype = ((t_map*)type)->get_key_type();
395 t_type* vtype = ((t_map*)type)->get_val_type();
396 out << "{" << endl;
397
398 const map<t_const_value*, t_const_value*>& val = value->get_map();
399 map<t_const_value*, t_const_value*>::const_iterator v_iter;
400 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
401 out << render_const_value(ktype, v_iter->first);
402 out << " => ";
403 out << render_const_value(vtype, v_iter->second);
404 out << "," << endl;
405 }
406
David Reiss0c703cc2008-03-25 18:38:56 +0000407 out << "}";
Mark Slee2c44d202007-05-16 02:18:07 +0000408 } else if (type->is_list() || type->is_set()) {
409 t_type* etype;
410 if (type->is_list()) {
411 etype = ((t_list*)type)->get_elem_type();
412 } else {
413 etype = ((t_set*)type)->get_elem_type();
414 }
415 out << "[" << endl;
416 const vector<t_const_value*>& val = value->get_list();
417 vector<t_const_value*>::const_iterator v_iter;
418 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
419
420 out << render_const_value(etype, *v_iter);
421 if (type->is_set()) {
422 out << " => 1";
423 }
424 out << "," << endl;
425 }
426 out << "]";
427 }
428 return out.str();
429}
430
431/**
432 * Make a struct
433 */
434void t_perl_generator::generate_struct(t_struct* tstruct) {
435 generate_perl_struct(tstruct, false);
436}
437
438/**
439 * Generates a struct definition for a thrift exception. Basically the same
440 * as a struct but extends the Exception class.
441 *
442 * @param txception The struct definition
443 */
444void t_perl_generator::generate_xception(t_struct* txception) {
445 generate_perl_struct(txception, true);
446}
447
448/**
449 * Structs can be normal or exceptions.
450 */
451void t_perl_generator::generate_perl_struct(t_struct* tstruct,
452 bool is_exception) {
453 generate_perl_struct_definition(f_types_, tstruct, is_exception);
454}
455
456/**
457 * Generates a struct definition for a thrift data type. This is nothing in PERL
458 * where the objects are all just associative arrays (unless of course we
459 * decide to start using objects for them...)
460 *
461 * @param tstruct The struct definition
462 */
463void t_perl_generator::generate_perl_struct_definition(ofstream& out,
464 t_struct* tstruct,
465 bool is_exception) {
466 const vector<t_field*>& members = tstruct->get_members();
467 vector<t_field*>::const_iterator m_iter;
468
469 out <<
470 "package " << perl_namespace(tstruct->get_program()) << tstruct->get_name() <<";\n";
471 if (is_exception) {
472 out << "use base('Thrift::TException');\n";
473 }
474
Mark Slee82664432007-09-19 06:49:30 +0000475 //Create simple acessor methods
T Jake Luciani41687fc2008-12-23 03:45:43 +0000476 out << "use Class::Accessor;\n";
Mark Slee82664432007-09-19 06:49:30 +0000477 out << "use base('Class::Accessor');\n";
478
479 if (members.size() > 0) {
480 out << perl_namespace(tstruct->get_program()) << tstruct->get_name() <<"->mk_accessors( qw( ";
481 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
482 t_type* t = get_true_type((*m_iter)->get_type());
483 if (!t->is_xception()) {
484 out << (*m_iter)->get_name() << " ";
485 }
486 }
487
488 out << ") );\n";
489 }
490
491
492 // new()
Mark Slee2c44d202007-05-16 02:18:07 +0000493 out << "sub new {\n";
494 indent_up();
495 out << "my $classname = shift;\n";
496 out << "my $self = {};\n";
497 out << "my $vals = shift || {};\n";
498
499 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
500 string dval = "undef";
David Reisse087a302007-08-23 21:43:25 +0000501 t_type* t = get_true_type((*m_iter)->get_type());
Mark Slee2c44d202007-05-16 02:18:07 +0000502 if ((*m_iter)->get_value() != NULL && !(t->is_struct() || t->is_xception())) {
503 dval = render_const_value((*m_iter)->get_type(), (*m_iter)->get_value());
504 }
505 out <<
506 "$self->{" << (*m_iter)->get_name() << "} = " << dval << ";" << endl;
507 }
508
509 // Generate constructor from array
510 if (members.size() > 0) {
511
512 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
David Reisse087a302007-08-23 21:43:25 +0000513 t_type* t = get_true_type((*m_iter)->get_type());
Mark Slee2c44d202007-05-16 02:18:07 +0000514 if ((*m_iter)->get_value() != NULL && (t->is_struct() || t->is_xception())) {
515 indent(out) << "$self->{" << (*m_iter)->get_name() << "} = " << render_const_value(t, (*m_iter)->get_value()) << ";" << endl;
516 }
517 }
518
519 out << indent() << "if (UNIVERSAL::isa($vals,'HASH')) {" << endl;
520 indent_up();
521 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
522 out <<
523 indent() << "if (defined $vals->{" << (*m_iter)->get_name() << "}) {" << endl <<
524 indent() << " $self->{" << (*m_iter)->get_name() << "} = $vals->{" << (*m_iter)->get_name() << "};" << endl <<
525 indent() << "}" << endl;
526 }
527 indent_down();
528 out <<
529 indent() << "}" << endl;
530
531 }
532
533 out << "return bless($self,$classname);\n";
534 indent_down();
535 out << "}\n\n";
536
537 out <<
538 "sub getName {" << endl <<
539 indent() << " return '" << tstruct->get_name() << "';" << endl <<
540 indent() << "}" << endl <<
541 endl;
542
543 generate_perl_struct_reader(out, tstruct);
544 generate_perl_struct_writer(out, tstruct);
545
546}
547
548/**
549 * Generates the read() method for a struct
550 */
551void t_perl_generator::generate_perl_struct_reader(ofstream& out,
552 t_struct* tstruct) {
553 const vector<t_field*>& fields = tstruct->get_members();
554 vector<t_field*>::const_iterator f_iter;
555
556 out << "sub read {" <<endl;
557
558 indent_up();
559
560 out <<
561 indent() << "my $self = shift;" <<endl <<
562 indent() << "my $input = shift;" <<endl <<
563 indent() << "my $xfer = 0;" << endl <<
564 indent() << "my $fname;" << endl <<
565 indent() << "my $ftype = 0;" << endl <<
566 indent() << "my $fid = 0;" << endl;
567
568 indent(out) << "$xfer += $input->readStructBegin(\\$fname);" << endl;
569
570
571 // Loop over reading in fields
572 indent(out) << "while (1) " << endl;
573
574 scope_up(out);
575
576 indent(out) << "$xfer += $input->readFieldBegin(\\$fname, \\$ftype, \\$fid);" << endl;
577
578 // Check for field STOP marker and break
579 indent(out) << "if ($ftype == TType::STOP) {" << endl;
580 indent_up();
581 indent(out) << "last;" << endl;
582 indent_down();
583 indent(out) << "}" << endl;
584
585 // Switch statement on the field we are reading
586 indent(out) << "SWITCH: for($fid)" << endl;
587
588 scope_up(out);
589
590 // Generate deserialization code for known cases
591 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
592
593 indent(out) << "/^" << (*f_iter)->get_key() << "$/ && do{";
594 indent(out) << "if ($ftype == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000595
596 indent_up();
Mark Slee2c44d202007-05-16 02:18:07 +0000597 generate_deserialize_field(out, *f_iter, "self->");
598 indent_down();
Mark Slee27ed6ec2007-08-16 01:26:31 +0000599
Mark Slee2c44d202007-05-16 02:18:07 +0000600 indent(out) << "} else {" << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000601
Mark Slee2c44d202007-05-16 02:18:07 +0000602 indent(out) << " $xfer += $input->skip($ftype);" << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000603
Mark Slee2c44d202007-05-16 02:18:07 +0000604 out <<
605 indent() << "}" << endl <<
606 indent() << "last; };" << endl;
607
608 }
609 // In the default case we skip the field
610
611 indent(out) << " $xfer += $input->skip($ftype);" << endl;
612
613 scope_down(out);
614
615 indent(out) << "$xfer += $input->readFieldEnd();" << endl;
616
617 scope_down(out);
618
619 indent(out) << "$xfer += $input->readStructEnd();" << endl;
620
621 indent(out) << "return $xfer;" << endl;
622
623 indent_down();
624 out << indent() << "}" << endl << endl;
625}
626
627/**
628 * Generates the write() method for a struct
629 */
630void t_perl_generator::generate_perl_struct_writer(ofstream& out,
631 t_struct* tstruct) {
632 string name = tstruct->get_name();
633 const vector<t_field*>& fields = tstruct->get_members();
634 vector<t_field*>::const_iterator f_iter;
635
636 out << "sub write {" << endl;
637
638 indent_up();
639 indent(out) << "my $self = shift;"<<endl;
640 indent(out) << "my $output = shift;"<<endl;
641 indent(out) << "my $xfer = 0;" << endl;
642
643 indent(out) << "$xfer += $output->writeStructBegin('" << name << "');" << endl;
644
645 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
646 out << indent() << "if (defined $self->{" << (*f_iter)->get_name() << "}) {" << endl;
647 indent_up();
648
649 indent(out) <<
650 "$xfer += $output->writeFieldBegin(" <<
651 "'" << (*f_iter)->get_name() << "', " <<
652 type_to_enum((*f_iter)->get_type()) << ", " <<
653 (*f_iter)->get_key() << ");" << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000654
Mark Slee2c44d202007-05-16 02:18:07 +0000655
656 // Write field contents
657 generate_serialize_field(out, *f_iter, "self->");
658
659 indent(out) <<
660 "$xfer += $output->writeFieldEnd();" << endl;
661
662 indent_down();
663 indent(out) << "}" << endl;
664 }
665
666
667 out <<
668 indent() << "$xfer += $output->writeFieldStop();" << endl <<
669 indent() << "$xfer += $output->writeStructEnd();" << endl;
670
671 out <<indent() << "return $xfer;" << endl;
672
673 indent_down();
674 out <<
675 indent() << "}" << endl <<
676 endl;
677}
678
679/**
680 * Generates a thrift service.
681 *
682 * @param tservice The service definition
683 */
684void t_perl_generator::generate_service(t_service* tservice) {
T Jake Luciani41687fc2008-12-23 03:45:43 +0000685 string f_service_name = get_namespace_out_dir()+service_name_+".pm";
Mark Slee2c44d202007-05-16 02:18:07 +0000686 f_service_.open(f_service_name.c_str());
687
688 f_service_ <<
689 /// "package "<<service_name_<<";"<<endl<<
690 autogen_comment() <<
691 perl_includes();
Mark Slee27ed6ec2007-08-16 01:26:31 +0000692
Mark Slee2c44d202007-05-16 02:18:07 +0000693 f_service_ <<
Mark Slee27ed6ec2007-08-16 01:26:31 +0000694 "use " << perl_namespace(tservice->get_program()) << "Types;" << endl;
Mark Slee2c44d202007-05-16 02:18:07 +0000695
T Jake Luciani41687fc2008-12-23 03:45:43 +0000696 t_service* extends_s = tservice->get_extends();
697 if (extends_s != NULL) {
Mark Slee2c44d202007-05-16 02:18:07 +0000698 f_service_ <<
T Jake Luciani41687fc2008-12-23 03:45:43 +0000699 "use " << perl_namespace(extends_s->get_program()) << extends_s->get_name() << ";" << endl;
Mark Slee2c44d202007-05-16 02:18:07 +0000700 }
701
702 f_service_ <<
703 endl;
704
705 // Generate the three main parts of the service (well, two for now in PERL)
706 generate_service_helpers(tservice);
707 generate_service_interface(tservice);
708 generate_service_rest(tservice);
709 generate_service_client(tservice);
710 generate_service_processor(tservice);
711
712 // Close service file
713 f_service_ << "1;" << endl;
714 f_service_.close();
715}
716
717/**
718 * Generates a service server definition.
719 *
720 * @param tservice The service to generate a server for.
721 */
722void t_perl_generator::generate_service_processor(t_service* tservice) {
723 // Generate the dispatch methods
724 vector<t_function*> functions = tservice->get_functions();
725 vector<t_function*>::iterator f_iter;
726
727 string extends = "";
728 string extends_processor = "";
T Jake Luciani41687fc2008-12-23 03:45:43 +0000729 t_service* extends_s = tservice->get_extends();
730 if (extends_s != NULL) {
731 extends = perl_namespace(extends_s->get_program()) + extends_s->get_name();
Mark Slee2c44d202007-05-16 02:18:07 +0000732 extends_processor = "use base('" + extends + "Processor');";
733 }
734
735 indent_up();
736
737 // Generate the header portion
738 f_service_ <<
T Jake Luciani41687fc2008-12-23 03:45:43 +0000739 "package " << perl_namespace(program_) << service_name_ << "Processor;" << endl << extends_processor << endl;
Mark Slee2c44d202007-05-16 02:18:07 +0000740
741
742 if (extends.empty()) {
743 f_service_ << "sub new {" << endl;
744
745 indent_up();
746
747 f_service_ <<
748 indent() << "my $classname = shift;"<< endl <<
749 indent() << "my $handler = shift;"<< endl <<
750 indent() << "my $self = {};" << endl;
751
752 f_service_ <<
753 indent() << "$self->{handler} = $handler;" << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000754
Mark Slee2c44d202007-05-16 02:18:07 +0000755 f_service_ <<
756 indent() << "return bless($self,$classname);"<<endl;
757
758 indent_down();
759
760 f_service_ <<"}" << endl << endl;
761 }
762
763 // Generate the server implementation
764 f_service_ << "sub process {" << endl;
765 indent_up();
766
767 f_service_ <<
768 indent() << "my $self = shift;"<<endl <<
769 indent() << "my $input = shift;"<<endl <<
770 indent() << "my $output = shift;"<<endl;
771
772 f_service_ <<
773 indent() << "my $rseqid = 0;" << endl <<
774 indent() << "my $fname = undef;" << endl <<
775 indent() << "my $mtype = 0;" << endl << endl;
776
777 f_service_ <<
778 indent() << "$input->readMessageBegin(\\$fname, \\$mtype, \\$rseqid);" << endl;
779
780 // HOT: check for method implementation
781 f_service_ <<
782 indent() << "my $methodname = 'process_'.$fname;" << endl <<
783 indent() << "if (!method_exists($self, $methodname)) {" << endl;
784
785 f_service_ <<
786 indent() << " $input->skip(TType::STRUCT);" << endl <<
787 indent() << " $input->readMessageEnd();" << endl <<
788 indent() << " my $x = new TApplicationException('Function '.$fname.' not implemented.', TApplicationException::UNKNOWN_METHOD);" << endl <<
789 indent() << " $output->writeMessageBegin($fname, TMessageType::EXCEPTION, $rseqid);" << endl <<
790 indent() << " $x->write($output);" << endl <<
791 indent() << " $output->writeMessageEnd();" << endl <<
792 indent() << " $output->getTransport()->flush();" << endl <<
793 indent() << " return;" << endl;
794
795 f_service_ <<
796 indent() << "}" << endl <<
797 indent() << "$self->$methodname($rseqid, $input, $output);" << endl <<
798 indent() << "return 1;" << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000799
Mark Slee2c44d202007-05-16 02:18:07 +0000800 indent_down();
801
802 f_service_ <<
803 indent() << "}" << endl <<endl;
804
805 // Generate the process subfunctions
806 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
807 generate_process_function(tservice, *f_iter);
808 }
809}
810
811/**
812 * Generates a process function definition.
813 *
814 * @param tfunction The function to write a dispatcher for
815 */
816void t_perl_generator::generate_process_function(t_service* tservice,
817 t_function* tfunction) {
818 // Open function
819 f_service_ <<
820 "sub process_" << tfunction->get_name() << "{"<<endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000821
Mark Slee2c44d202007-05-16 02:18:07 +0000822 indent_up();
Mark Slee27ed6ec2007-08-16 01:26:31 +0000823
Mark Slee2c44d202007-05-16 02:18:07 +0000824 f_service_ <<
825 indent() << "my $self = shift;"<<endl<<
826 indent() << "my ($seqid, $input, $output); " << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000827
Mark Slee2c44d202007-05-16 02:18:07 +0000828 string argsname = perl_namespace(tservice->get_program()) + service_name_ + "_" + tfunction->get_name() + "_args";
829 string resultname = perl_namespace(tservice->get_program()) + service_name_ + "_" + tfunction->get_name() + "_result";
Mark Slee27ed6ec2007-08-16 01:26:31 +0000830
Mark Slee2c44d202007-05-16 02:18:07 +0000831 f_service_ <<
832 indent() << "my $args = new " << argsname << "();" << endl <<
833 indent() << "$args->read($input);" << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000834
Mark Slee2c44d202007-05-16 02:18:07 +0000835 f_service_ <<
836 indent() << "$input->readMessageEnd();" << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000837
Mark Slee2c44d202007-05-16 02:18:07 +0000838 t_struct* xs = tfunction->get_xceptions();
839 const std::vector<t_field*>& xceptions = xs->get_members();
840 vector<t_field*>::const_iterator x_iter;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000841
David Reissc51986f2009-03-24 20:01:25 +0000842 // Declare result for non oneway function
David Reiss47329252009-03-24 20:01:02 +0000843 if (!tfunction->is_oneway()) {
Mark Slee2c44d202007-05-16 02:18:07 +0000844 f_service_ <<
845 indent() << "my $result = new " << resultname << "();" << endl;
846 }
Mark Slee27ed6ec2007-08-16 01:26:31 +0000847
Mark Slee2c44d202007-05-16 02:18:07 +0000848 // Try block for a function with exceptions
849 if (xceptions.size() > 0) {
850 f_service_ <<
851 indent() << "eval {" << endl;
852 indent_up();
853 }
Mark Slee27ed6ec2007-08-16 01:26:31 +0000854
Mark Slee2c44d202007-05-16 02:18:07 +0000855 // Generate the function call
856 t_struct* arg_struct = tfunction->get_arglist();
857 const std::vector<t_field*>& fields = arg_struct->get_members();
858 vector<t_field*>::const_iterator f_iter;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000859
Mark Slee2c44d202007-05-16 02:18:07 +0000860 f_service_ << indent();
David Reiss47329252009-03-24 20:01:02 +0000861 if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) {
Mark Slee2c44d202007-05-16 02:18:07 +0000862 f_service_ << "$result->{success} = ";
863 }
864 f_service_ <<
865 "$self->{handler}->" << tfunction->get_name() << "(";
866 bool first = true;
867 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
868 if (first) {
869 first = false;
870 } else {
871 f_service_ << ", ";
872 }
873 f_service_ << "$args->" << (*f_iter)->get_name();
874 }
875 f_service_ << ");" << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000876
David Reiss47329252009-03-24 20:01:02 +0000877 if (!tfunction->is_oneway() && xceptions.size() > 0) {
Mark Slee2c44d202007-05-16 02:18:07 +0000878 indent_down();
879 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
880 f_service_ <<
881 indent() << "}; if( UNIVERSAL::isa($@,'"<<(*x_iter)->get_type()->get_name()<<"') ){ "<<endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000882
David Reiss47329252009-03-24 20:01:02 +0000883 if (!tfunction->is_oneway()) {
Mark Slee2c44d202007-05-16 02:18:07 +0000884 indent_up();
885 f_service_ <<
886 indent() << "$result->{" << (*x_iter)->get_name() << "} = $@;" << endl;
887 indent_down();
888 f_service_ << indent();
889 }
890 }
891 indent_down();
892 f_service_ << "}" << endl;
893 }
894
David Reissc51986f2009-03-24 20:01:25 +0000895 // Shortcut out here for oneway functions
David Reiss47329252009-03-24 20:01:02 +0000896 if (tfunction->is_oneway()) {
Mark Slee2c44d202007-05-16 02:18:07 +0000897 f_service_ <<
898 indent() << "return;" << endl;
899 indent_down();
900 f_service_ <<
901 indent() << "}" << endl;
902 return;
903 }
904 indent_up();
905 // Serialize the request header
906 f_service_ <<
907 indent() << "$output->writeMessageBegin('" << tfunction->get_name() << "', TMessageType::REPLY, $seqid);" << endl <<
908 indent() << "$result->write($output);" << endl <<
909 indent() << "$output->getTransport()->flush();" << endl;
910 indent_down();
Mark Slee27ed6ec2007-08-16 01:26:31 +0000911
Mark Slee2c44d202007-05-16 02:18:07 +0000912 // Close function
913 indent_down();
914 f_service_ <<
915 indent() << "}" << endl;
916}
917
918/**
919 * Generates helper functions for a service.
920 *
921 * @param tservice The service to generate a header definition for
922 */
923void t_perl_generator::generate_service_helpers(t_service* tservice) {
924 vector<t_function*> functions = tservice->get_functions();
925 vector<t_function*>::iterator f_iter;
926
927 f_service_ <<
928 "# HELPER FUNCTIONS AND STRUCTURES" << endl << endl;
929
930 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
931 t_struct* ts = (*f_iter)->get_arglist();
932 string name = ts->get_name();
933 ts->set_name(service_name_ + "_" + name);
934 generate_perl_struct_definition(f_service_, ts, false);
935 generate_perl_function_helpers(*f_iter);
936 ts->set_name(name);
937 }
938}
939
940/**
941 * Generates a struct and helpers for a function.
942 *
943 * @param tfunction The function
944 */
945void t_perl_generator::generate_perl_function_helpers(t_function* tfunction) {
946 t_struct result(program_, service_name_ + "_" + tfunction->get_name() + "_result");
947 t_field success(tfunction->get_returntype(), "success", 0);
948 if (!tfunction->get_returntype()->is_void()) {
949 result.append(&success);
950 }
951
952 t_struct* xs = tfunction->get_xceptions();
953 const vector<t_field*>& fields = xs->get_members();
954 vector<t_field*>::const_iterator f_iter;
955 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
956 result.append(*f_iter);
957 }
958
959 generate_perl_struct_definition(f_service_, &result, false);
960}
961
962/**
963 * Generates a service interface definition.
964 *
965 * @param tservice The service to generate a header definition for
966 */
967void t_perl_generator::generate_service_interface(t_service* tservice) {
Mark Slee2c44d202007-05-16 02:18:07 +0000968 string extends_if = "";
T Jake Luciani41687fc2008-12-23 03:45:43 +0000969 t_service* extends_s = tservice->get_extends();
970 if (extends_s != NULL) {
971 extends_if = "use base('" + perl_namespace(extends_s->get_program()) + extends_s->get_name() + "If');";
Mark Slee2c44d202007-05-16 02:18:07 +0000972 }
973
974 f_service_ <<
T Jake Luciani41687fc2008-12-23 03:45:43 +0000975 "package " << perl_namespace(program_) << service_name_ << "If;"<<endl<<
Mark Slee2c44d202007-05-16 02:18:07 +0000976 extends_if<<endl;
977
978
979 indent_up();
980 vector<t_function*> functions = tservice->get_functions();
981 vector<t_function*>::iterator f_iter;
982 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
983 f_service_ <<
984 "sub " << function_signature(*f_iter) <<endl<< " die 'implement interface';\n}" << endl;
985 }
986 indent_down();
987
988}
989
990/**
991 * Generates a REST interface
992 */
993void t_perl_generator::generate_service_rest(t_service* tservice) {
994 string extends = "";
995 string extends_if = "";
T Jake Luciani41687fc2008-12-23 03:45:43 +0000996 t_service* extends_s = tservice->get_extends();
997 if (extends_s != NULL) {
998 extends = extends_s->get_name();
999 extends_if = "use base('" + perl_namespace(extends_s->get_program()) + extends_s->get_name() + "Rest');";
Mark Slee2c44d202007-05-16 02:18:07 +00001000 }
1001 f_service_ <<
T Jake Luciani41687fc2008-12-23 03:45:43 +00001002 "package " << perl_namespace(program_) << service_name_ << "Rest;"<<endl<<
Mark Slee2c44d202007-05-16 02:18:07 +00001003 extends_if << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +00001004
1005
Mark Slee2c44d202007-05-16 02:18:07 +00001006 if (extends.empty()) {
1007 f_service_ << "sub new {" << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +00001008
Mark Slee2c44d202007-05-16 02:18:07 +00001009 indent_up();
Mark Slee27ed6ec2007-08-16 01:26:31 +00001010
Mark Slee2c44d202007-05-16 02:18:07 +00001011 f_service_ <<
1012 indent() << "my $classname=shift;"<<endl <<
1013 indent() << "my $impl =shift;"<<endl <<
1014 indent() << "my $self ={ impl => $impl };"<<endl << endl<<
1015 indent() << "return bless($self,$classname);" << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +00001016
1017
Mark Slee2c44d202007-05-16 02:18:07 +00001018 indent_down();
1019
1020 f_service_ <<
1021 indent() << "}" << endl << endl;
1022 }
Mark Slee27ed6ec2007-08-16 01:26:31 +00001023
Mark Slee2c44d202007-05-16 02:18:07 +00001024 vector<t_function*> functions = tservice->get_functions();
1025 vector<t_function*>::iterator f_iter;
1026 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
1027 f_service_ <<
1028 "sub " << (*f_iter)->get_name() <<
1029 "{" <<endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +00001030
Mark Slee2c44d202007-05-16 02:18:07 +00001031 indent_up();
Mark Slee27ed6ec2007-08-16 01:26:31 +00001032
Mark Slee2c44d202007-05-16 02:18:07 +00001033 f_service_ <<
1034 indent() << "my $self = shift;"<< endl <<
1035 indent() << "my $request = shift;" << endl << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +00001036
1037
Mark Slee2c44d202007-05-16 02:18:07 +00001038 const vector<t_field*>& args = (*f_iter)->get_arglist()->get_members();
1039 vector<t_field*>::const_iterator a_iter;
1040 for (a_iter = args.begin(); a_iter != args.end(); ++a_iter) {
David Reisse087a302007-08-23 21:43:25 +00001041 t_type* atype = get_true_type((*a_iter)->get_type());
Mark Slee2c44d202007-05-16 02:18:07 +00001042 string req = "$request->{'" + (*a_iter)->get_name() + "'}";
1043 f_service_ <<
1044 indent() << "my $" << (*a_iter)->get_name() << " = (" << req << ") ? " << req << " : undef;" << endl;
1045 if (atype->is_string() &&
1046 ((t_base_type*)atype)->is_string_list()) {
1047 f_service_ <<
1048 indent() << "my @" << (*a_iter)->get_name() << " = split(/,/, $" << (*a_iter)->get_name() << ");" << endl <<
1049 indent() << "$"<<(*a_iter)->get_name() <<" = \\@"<<(*a_iter)->get_name()<<endl;
1050 }
1051 }
1052 f_service_ <<
1053 indent() << "return $self->{impl}->" << (*f_iter)->get_name() << "(" << argument_list((*f_iter)->get_arglist()) << ");" << endl;
1054 indent_down();
1055 indent(f_service_) << "}" << endl <<endl;
1056 }
Mark Slee27ed6ec2007-08-16 01:26:31 +00001057
Mark Slee2c44d202007-05-16 02:18:07 +00001058}
1059
1060/**
1061 * Generates a service client definition.
1062 *
1063 * @param tservice The service to generate a server for.
1064 */
1065void t_perl_generator::generate_service_client(t_service* tservice) {
1066 string extends = "";
1067 string extends_client = "";
T Jake Luciani41687fc2008-12-23 03:45:43 +00001068 t_service* extends_s = tservice->get_extends();
1069 if (extends_s != NULL) {
1070 extends = perl_namespace(extends_s->get_program()) + extends_s->get_name();
Mark Slee2c44d202007-05-16 02:18:07 +00001071 extends_client = "use base('" + extends + "Client');";
1072 }
1073
1074 f_service_ <<
T Jake Luciani41687fc2008-12-23 03:45:43 +00001075 "package " << perl_namespace(program_) << service_name_ << "Client;"<<endl;
Mark Slee2c44d202007-05-16 02:18:07 +00001076
1077 f_service_ <<
1078 extends_client << endl <<
T Jake Luciani41687fc2008-12-23 03:45:43 +00001079 "use base('" << perl_namespace(program_) << service_name_ << "If');" << endl;
Mark Slee2c44d202007-05-16 02:18:07 +00001080
1081 // Constructor function
1082 f_service_ << "sub new {"<<endl;
1083
1084 indent_up();
1085
1086 f_service_ <<
1087 indent() << "my $classname = shift;"<<endl<<
1088 indent() << "my $input = shift;"<<endl<<
1089 indent() << "my $output = shift;"<<endl<<
1090 indent() << "my $self = {};" <<endl;
1091
1092 if (!extends.empty()) {
1093 f_service_ <<
1094 indent() << " $self = $classname->SUPER::new($input, $output);" << endl;
1095 } else {
1096 f_service_ <<
1097 indent() << " $self->{input} = $input;" << endl <<
1098 indent() << " $self->{output} = defined $output ? $output : $input;" << endl <<
1099 indent() << " $self->{seqid} = 0;" << endl;
1100 }
1101
1102 f_service_ <<
1103 indent() << "return bless($self,$classname);"<<endl;
1104
1105 indent_down();
1106
1107 f_service_ <<
1108 indent() << "}" << endl << endl;
1109
1110 // Generate client method implementations
1111 vector<t_function*> functions = tservice->get_functions();
1112 vector<t_function*>::const_iterator f_iter;
1113 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
1114 t_struct* arg_struct = (*f_iter)->get_arglist();
1115 const vector<t_field*>& fields = arg_struct->get_members();
1116 vector<t_field*>::const_iterator fld_iter;
1117 string funname = (*f_iter)->get_name();
Mark Slee27ed6ec2007-08-16 01:26:31 +00001118
Mark Slee2c44d202007-05-16 02:18:07 +00001119 // Open function
1120 f_service_ << "sub " << function_signature(*f_iter) << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +00001121
Mark Slee2c44d202007-05-16 02:18:07 +00001122 indent_up();
Mark Slee27ed6ec2007-08-16 01:26:31 +00001123
Mark Slee2c44d202007-05-16 02:18:07 +00001124 indent(f_service_) << indent() <<
1125 "$self->send_" << funname << "(";
Mark Slee27ed6ec2007-08-16 01:26:31 +00001126
Mark Slee2c44d202007-05-16 02:18:07 +00001127 bool first = true;
1128 for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
1129 if (first) {
1130 first = false;
1131 } else {
1132 f_service_ << ", ";
1133 }
1134 f_service_ << "$" << (*fld_iter)->get_name();
1135 }
1136 f_service_ << ");" << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +00001137
David Reiss47329252009-03-24 20:01:02 +00001138 if (!(*f_iter)->is_oneway()) {
Mark Slee2c44d202007-05-16 02:18:07 +00001139 f_service_ << indent();
1140 if (!(*f_iter)->get_returntype()->is_void()) {
1141 f_service_ << "return ";
1142 }
1143 f_service_ <<
1144 "$self->recv_" << funname << "();" << endl;
1145 }
Mark Slee27ed6ec2007-08-16 01:26:31 +00001146
Mark Slee2c44d202007-05-16 02:18:07 +00001147 indent_down();
Mark Slee27ed6ec2007-08-16 01:26:31 +00001148
1149 f_service_ << "}" << endl << endl;
1150
Mark Slee2c44d202007-05-16 02:18:07 +00001151 f_service_ <<
1152 "sub send_" << function_signature(*f_iter) << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +00001153
Mark Slee2c44d202007-05-16 02:18:07 +00001154 indent_up();
Mark Slee27ed6ec2007-08-16 01:26:31 +00001155
Mark Slee2c44d202007-05-16 02:18:07 +00001156 std::string argsname = perl_namespace(tservice->get_program()) + service_name_ + "_" + (*f_iter)->get_name() + "_args";
Mark Slee27ed6ec2007-08-16 01:26:31 +00001157
Mark Slee2c44d202007-05-16 02:18:07 +00001158 // Serialize the request header
1159 f_service_ <<
1160 indent() << "$self->{output}->writeMessageBegin('" << (*f_iter)->get_name() << "', TMessageType::CALL, $self->{seqid});" << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +00001161
Mark Slee2c44d202007-05-16 02:18:07 +00001162 f_service_ <<
1163 indent() << "my $args = new " << argsname << "();" << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +00001164
Mark Slee2c44d202007-05-16 02:18:07 +00001165 for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
1166 f_service_ <<
1167 indent() << "$args->{" << (*fld_iter)->get_name() << "} = $" << (*fld_iter)->get_name() << ";" << endl;
1168 }
Mark Slee27ed6ec2007-08-16 01:26:31 +00001169
Mark Slee2c44d202007-05-16 02:18:07 +00001170 // Write to the stream
1171 f_service_ <<
1172 indent() << "$args->write($self->{output});" << endl <<
1173 indent() << "$self->{output}->writeMessageEnd();" << endl <<
1174 indent() << "$self->{output}->getTransport()->flush();" << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +00001175
1176
Mark Slee2c44d202007-05-16 02:18:07 +00001177 indent_down();
Mark Slee27ed6ec2007-08-16 01:26:31 +00001178
1179 f_service_ << "}" << endl;
1180
1181
David Reiss47329252009-03-24 20:01:02 +00001182 if (!(*f_iter)->is_oneway()) {
Mark Slee2c44d202007-05-16 02:18:07 +00001183 std::string resultname = perl_namespace(tservice->get_program()) + service_name_ + "_" + (*f_iter)->get_name() + "_result";
1184 t_struct noargs(program_);
1185
1186 t_function recv_function((*f_iter)->get_returntype(),
1187 string("recv_") + (*f_iter)->get_name(),
1188 &noargs);
1189 // Open function
1190 f_service_ <<
1191 endl <<
1192 "sub " << function_signature(&recv_function) << endl;
1193
1194 indent_up();
1195
1196 f_service_ <<
1197 indent() << "my $rseqid = 0;" << endl <<
1198 indent() << "my $fname;" << endl <<
1199 indent() << "my $mtype = 0;" << endl <<
1200 endl;
1201
1202 f_service_ <<
1203 indent() << "$self->{input}->readMessageBegin(\\$fname, \\$mtype, \\$rseqid);" << endl <<
1204 indent() << "if ($mtype == TMessageType::EXCEPTION) {" << endl <<
1205 indent() << " my $x = new TApplicationException();" << endl <<
1206 indent() << " $x->read($self->{input});" << endl <<
1207 indent() << " $self->{input}->readMessageEnd();" << endl <<
1208 indent() << " die $x;" << endl <<
1209 indent() << "}" << endl;
1210
1211
1212 f_service_ <<
1213 indent() << "my $result = new " << resultname << "();" << endl <<
1214 indent() << "$result->read($self->{input});" << endl;
1215
1216
1217 f_service_ <<
1218 indent() << "$self->{input}->readMessageEnd();" << endl <<
1219 endl;
1220
1221
1222 // Careful, only return result if not a void function
1223 if (!(*f_iter)->get_returntype()->is_void()) {
1224 f_service_ <<
1225 indent() << "if (defined $result->{success} ) {" << endl <<
1226 indent() << " return $result->{success};" << endl <<
1227 indent() << "}" << endl;
1228 }
1229
1230 t_struct* xs = (*f_iter)->get_xceptions();
1231 const std::vector<t_field*>& xceptions = xs->get_members();
1232 vector<t_field*>::const_iterator x_iter;
1233 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
1234 f_service_ <<
1235 indent() << "if (defined $result->{" << (*x_iter)->get_name() << "}) {" << endl <<
1236 indent() << " die $result->{" << (*x_iter)->get_name() << "};" << endl <<
1237 indent() << "}" << endl;
1238 }
1239
1240 // Careful, only return _result if not a void function
1241 if ((*f_iter)->get_returntype()->is_void()) {
1242 indent(f_service_) <<
1243 "return;" << endl;
1244 } else {
1245 f_service_ <<
1246 indent() << "die \"" << (*f_iter)->get_name() << " failed: unknown result\";" << endl;
1247 }
1248
1249 // Close function
1250 indent_down();
1251 f_service_ << "}"<<endl;
1252
1253 }
1254 }
1255
1256}
1257
1258/**
1259 * Deserializes a field of any type.
1260 */
1261void t_perl_generator::generate_deserialize_field(ofstream &out,
1262 t_field* tfield,
1263 string prefix,
1264 bool inclass) {
David Reisse087a302007-08-23 21:43:25 +00001265 t_type* type = get_true_type(tfield->get_type());
Mark Slee2c44d202007-05-16 02:18:07 +00001266
1267 if (type->is_void()) {
1268 throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " +
1269 prefix + tfield->get_name();
1270 }
1271
1272 string name = tfield->get_name();
1273
1274 //Hack for when prefix is defined (always a hash ref)
1275 if (!prefix.empty()) {
1276 name = prefix + "{" + tfield->get_name() + "}";
1277 }
1278
1279 if (type->is_struct() || type->is_xception()) {
1280 generate_deserialize_struct(out,
1281 (t_struct*)type,
1282 name);
1283 } else if (type->is_container()) {
1284 generate_deserialize_container(out, type, name);
1285 } else if (type->is_base_type() || type->is_enum()) {
1286 indent(out) <<
1287 "$xfer += $input->";
Mark Slee27ed6ec2007-08-16 01:26:31 +00001288
Mark Slee2c44d202007-05-16 02:18:07 +00001289 if (type->is_base_type()) {
1290 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
1291 switch (tbase) {
1292 case t_base_type::TYPE_VOID:
1293 throw "compiler error: cannot serialize void field in a struct: " +
1294 name;
1295 break;
1296 case t_base_type::TYPE_STRING:
1297 out << "readString(\\$" << name << ");";
1298 break;
1299 case t_base_type::TYPE_BOOL:
1300 out << "readBool(\\$" << name << ");";
1301 break;
1302 case t_base_type::TYPE_BYTE:
1303 out << "readByte(\\$" << name << ");";
1304 break;
1305 case t_base_type::TYPE_I16:
1306 out << "readI16(\\$" << name << ");";
1307 break;
1308 case t_base_type::TYPE_I32:
1309 out << "readI32(\\$" << name << ");";
1310 break;
1311 case t_base_type::TYPE_I64:
1312 out << "readI64(\\$" << name << ");";
1313 break;
1314 case t_base_type::TYPE_DOUBLE:
1315 out << "readDouble(\\$" << name << ");";
1316 break;
1317 default:
David Reissdd7796f2007-08-28 21:09:06 +00001318 throw "compiler error: no PERL name for base type " + t_base_type::t_base_name(tbase);
Mark Slee2c44d202007-05-16 02:18:07 +00001319 }
1320 } else if (type->is_enum()) {
1321 out << "readI32(\\$" << name << ");";
1322 }
1323 out << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +00001324
Mark Slee2c44d202007-05-16 02:18:07 +00001325 } else {
1326 printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
1327 tfield->get_name().c_str(), type->get_name().c_str());
1328 }
1329}
1330
1331/**
1332 * Generates an unserializer for a variable. This makes two key assumptions,
1333 * first that there is a const char* variable named data that points to the
1334 * buffer for deserialization, and that there is a variable protocol which
1335 * is a reference to a TProtocol serialization object.
1336 */
1337void t_perl_generator::generate_deserialize_struct(ofstream &out,
1338 t_struct* tstruct,
1339 string prefix) {
1340 out <<
1341 indent() << "$" << prefix << " = new " << perl_namespace(tstruct->get_program()) << tstruct->get_name() << "();" << endl <<
1342 indent() << "$xfer += $" << prefix << "->read($input);" << endl;
1343}
1344
1345void t_perl_generator::generate_deserialize_container(ofstream &out,
1346 t_type* ttype,
1347 string prefix) {
1348 scope_up(out);
1349
1350 string size = tmp("_size");
1351 string ktype = tmp("_ktype");
1352 string vtype = tmp("_vtype");
1353 string etype = tmp("_etype");
1354
1355 t_field fsize(g_type_i32, size);
1356 t_field fktype(g_type_byte, ktype);
1357 t_field fvtype(g_type_byte, vtype);
1358 t_field fetype(g_type_byte, etype);
Mark Slee27ed6ec2007-08-16 01:26:31 +00001359
Mark Slee2c44d202007-05-16 02:18:07 +00001360 out <<
1361 indent() << "my $" << size << " = 0;" << endl;
1362
1363 // Declare variables, read header
1364 if (ttype->is_map()) {
1365 out <<
1366 indent() << "$" << prefix << " = {};" << endl <<
1367 indent() << "my $" << ktype << " = 0;" << endl <<
1368 indent() << "my $" << vtype << " = 0;" << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +00001369
Mark Slee2c44d202007-05-16 02:18:07 +00001370 out <<
1371 indent() << "$xfer += $input->readMapBegin(" <<
1372 "\\$" << ktype << ", \\$" << vtype << ", \\$" << size << ");" << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +00001373
Mark Slee2c44d202007-05-16 02:18:07 +00001374 } else if (ttype->is_set()) {
Mark Slee27ed6ec2007-08-16 01:26:31 +00001375
Mark Slee2c44d202007-05-16 02:18:07 +00001376 out <<
1377 indent() << "$" << prefix << " = {};" << endl <<
1378 indent() << "my $" << etype << " = 0;" << endl <<
1379 indent() << "$xfer += $input->readSetBegin(" <<
1380 "\\$" << etype << ", \\$" << size << ");" << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +00001381
Mark Slee2c44d202007-05-16 02:18:07 +00001382 } else if (ttype->is_list()) {
Mark Slee27ed6ec2007-08-16 01:26:31 +00001383
Mark Slee2c44d202007-05-16 02:18:07 +00001384 out <<
1385 indent() << "$" << prefix << " = [];" << endl <<
1386 indent() << "my $" << etype << " = 0;" << endl <<
1387 indent() << "$xfer += $input->readListBegin(" <<
1388 "\\$" << etype << ", \\$" << size << ");" << endl;
Mark Slee27ed6ec2007-08-16 01:26:31 +00001389
Mark Slee2c44d202007-05-16 02:18:07 +00001390 }
1391
1392 // For loop iterates over elements
1393 string i = tmp("_i");
1394 indent(out) <<
1395 "for (my $" <<
1396 i << " = 0; $" << i << " < $" << size << "; ++$" << i << ")" << endl;
1397
1398 scope_up(out);
1399
1400 if (ttype->is_map()) {
1401 generate_deserialize_map_element(out, (t_map*)ttype, prefix);
1402 } else if (ttype->is_set()) {
1403 generate_deserialize_set_element(out, (t_set*)ttype, prefix);
1404 } else if (ttype->is_list()) {
1405 generate_deserialize_list_element(out, (t_list*)ttype, prefix);
1406 }
1407
1408 scope_down(out);
1409
1410
1411 // Read container end
1412 if (ttype->is_map()) {
1413 indent(out) << "$xfer += $input->readMapEnd();" << endl;
1414 } else if (ttype->is_set()) {
1415 indent(out) << "$xfer += $input->readSetEnd();" << endl;
1416 } else if (ttype->is_list()) {
1417 indent(out) << "$xfer += $input->readListEnd();" << endl;
1418 }
1419
1420 scope_down(out);
1421}
1422
1423
1424/**
1425 * Generates code to deserialize a map
1426 */
1427void t_perl_generator::generate_deserialize_map_element(ofstream &out,
1428 t_map* tmap,
1429 string prefix) {
1430 string key = tmp("key");
1431 string val = tmp("val");
1432 t_field fkey(tmap->get_key_type(), key);
1433 t_field fval(tmap->get_val_type(), val);
1434
1435 indent(out) <<
1436 declare_field(&fkey, true, true) << endl;
1437 indent(out) <<
1438 declare_field(&fval, true, true) << endl;
1439
1440 generate_deserialize_field(out, &fkey);
1441 generate_deserialize_field(out, &fval);
1442
1443 indent(out) <<
1444 "$" << prefix << "->{$" << key << "} = $" << val << ";" << endl;
1445}
1446
1447void t_perl_generator::generate_deserialize_set_element(ofstream &out,
1448 t_set* tset,
1449 string prefix) {
1450 string elem = tmp("elem");
1451 t_field felem(tset->get_elem_type(), elem);
1452
1453 indent(out) <<
1454 "my $" << elem << " = undef;" << endl;
1455
1456 generate_deserialize_field(out, &felem);
1457
1458 indent(out) <<
1459 "$" << prefix << "->{$" << elem << "} = 1;" << endl;
1460}
1461
1462void t_perl_generator::generate_deserialize_list_element(ofstream &out,
1463 t_list* tlist,
1464 string prefix) {
1465 string elem = tmp("elem");
1466 t_field felem(tlist->get_elem_type(), elem);
1467
1468 indent(out) <<
1469 "my $" << elem << " = undef;" << endl;
1470
1471 generate_deserialize_field(out, &felem);
1472
1473 indent(out) <<
1474 "push(@{$" << prefix << "},$" << elem << ");" << endl;
1475}
1476
1477
1478/**
1479 * Serializes a field of any type.
1480 *
1481 * @param tfield The field to serialize
1482 * @param prefix Name to prepend to field name
1483 */
1484void t_perl_generator::generate_serialize_field(ofstream &out,
1485 t_field* tfield,
1486 string prefix) {
David Reisse087a302007-08-23 21:43:25 +00001487 t_type* type = get_true_type(tfield->get_type());
Mark Slee2c44d202007-05-16 02:18:07 +00001488
1489 // Do nothing for void types
1490 if (type->is_void()) {
1491 throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " +
1492 prefix + tfield->get_name();
1493 }
1494
1495 if (type->is_struct() || type->is_xception()) {
1496 generate_serialize_struct(out,
1497 (t_struct*)type,
1498 prefix + "{"+tfield->get_name()+"}" );
1499 } else if (type->is_container()) {
1500 generate_serialize_container(out,
1501 type,
1502 prefix + "{" + tfield->get_name()+"}");
1503 } else if (type->is_base_type() || type->is_enum()) {
1504
1505 string name = tfield->get_name();
1506
1507 //Hack for when prefix is defined (always a hash ref)
1508 if(!prefix.empty())
1509 name = prefix + "{" + tfield->get_name() + "}";
1510
1511 indent(out) <<
1512 "$xfer += $output->";
1513
1514 if (type->is_base_type()) {
1515 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
1516 switch (tbase) {
1517 case t_base_type::TYPE_VOID:
1518 throw
1519 "compiler error: cannot serialize void field in a struct: " + name;
1520 break;
1521 case t_base_type::TYPE_STRING:
1522 out << "writeString($" << name << ");";
1523 break;
1524 case t_base_type::TYPE_BOOL:
1525 out << "writeBool($" << name << ");";
1526 break;
1527 case t_base_type::TYPE_BYTE:
1528 out << "writeByte($" << name << ");";
1529 break;
1530 case t_base_type::TYPE_I16:
1531 out << "writeI16($" << name << ");";
1532 break;
1533 case t_base_type::TYPE_I32:
1534 out << "writeI32($" << name << ");";
1535 break;
1536 case t_base_type::TYPE_I64:
1537 out << "writeI64($" << name << ");";
1538 break;
1539 case t_base_type::TYPE_DOUBLE:
1540 out << "writeDouble($" << name << ");";
1541 break;
1542 default:
David Reissdd7796f2007-08-28 21:09:06 +00001543 throw "compiler error: no PERL name for base type " + t_base_type::t_base_name(tbase);
Mark Slee2c44d202007-05-16 02:18:07 +00001544 }
1545 } else if (type->is_enum()) {
1546 out << "writeI32($" << name << ");";
1547 }
1548 out << endl;
1549
1550 } else {
1551 printf("DO NOT KNOW HOW TO SERIALIZE FIELD '%s%s' TYPE '%s'\n",
1552 prefix.c_str(),
1553 tfield->get_name().c_str(),
1554 type->get_name().c_str());
1555 }
1556}
1557
1558/**
1559 * Serializes all the members of a struct.
1560 *
1561 * @param tstruct The struct to serialize
1562 * @param prefix String prefix to attach to all fields
1563 */
1564void t_perl_generator::generate_serialize_struct(ofstream &out,
1565 t_struct* tstruct,
1566 string prefix) {
1567 indent(out) <<
1568 "$xfer += $" << prefix << "->write($output);" << endl;
1569}
1570
1571/**
1572 * Writes out a container
1573 */
1574void t_perl_generator::generate_serialize_container(ofstream &out,
1575 t_type* ttype,
1576 string prefix) {
1577 scope_up(out);
1578
1579 if (ttype->is_map()) {
1580 indent(out) <<
1581 "$output->writeMapBegin(" <<
1582 type_to_enum(((t_map*)ttype)->get_key_type()) << ", " <<
1583 type_to_enum(((t_map*)ttype)->get_val_type()) << ", " <<
1584 "scalar(keys %{$" << prefix << "}));" << endl;
1585 } else if (ttype->is_set()) {
1586 indent(out) <<
1587 "$output->writeSetBegin(" <<
1588 type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " <<
1589 "scalar(@{$" << prefix << "}));" << endl;
1590
1591 } else if (ttype->is_list()) {
1592
1593 indent(out) <<
1594 "$output->writeListBegin(" <<
1595 type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " <<
1596 "scalar(@{$" << prefix << "}));" << endl;
1597
1598 }
1599
1600 scope_up(out);
1601
1602 if (ttype->is_map()) {
1603 string kiter = tmp("kiter");
1604 string viter = tmp("viter");
1605 indent(out) <<
1606 "while( my ($"<<kiter<<",$"<<viter<<") = each %{$" << prefix << "}) " << endl;
1607
1608 scope_up(out);
1609 generate_serialize_map_element(out, (t_map*)ttype, kiter, viter);
1610 scope_down(out);
1611
1612 } else if (ttype->is_set()) {
1613 string iter = tmp("iter");
1614 indent(out) <<
1615 "foreach my $"<<iter<<" (@{$" << prefix << "})" << endl;
1616 scope_up(out);
1617 generate_serialize_set_element(out, (t_set*)ttype, iter);
1618 scope_down(out);
1619
1620
1621 } else if (ttype->is_list()) {
1622 string iter = tmp("iter");
1623 indent(out) <<
1624 "foreach my $"<<iter<<" (@{$" << prefix << "}) " << endl;
1625 scope_up(out);
1626 generate_serialize_list_element(out, (t_list*)ttype, iter);
1627 scope_down(out);
1628 }
1629
1630 scope_down(out);
1631
1632 if (ttype->is_map()) {
1633 indent(out) <<
1634 "$output->writeMapEnd();" << endl;
1635 } else if (ttype->is_set()) {
1636 indent(out) <<
1637 "$output->writeSetEnd();" << endl;
1638 } else if (ttype->is_list()) {
1639 indent(out) <<
1640 "$output->writeListEnd();" << endl;
1641 }
1642
1643 scope_down(out);
1644}
1645
1646/**
1647 * Serializes the members of a map.
1648 *
1649 */
1650void t_perl_generator::generate_serialize_map_element(ofstream &out,
1651 t_map* tmap,
1652 string kiter,
1653 string viter) {
1654 t_field kfield(tmap->get_key_type(), kiter);
1655 generate_serialize_field(out, &kfield);
1656
1657 t_field vfield(tmap->get_val_type(), viter);
1658 generate_serialize_field(out, &vfield);
1659}
1660
1661/**
1662 * Serializes the members of a set.
1663 */
1664void t_perl_generator::generate_serialize_set_element(ofstream &out,
1665 t_set* tset,
1666 string iter) {
1667 t_field efield(tset->get_elem_type(), iter);
1668 generate_serialize_field(out, &efield);
1669}
1670
1671/**
1672 * Serializes the members of a list.
1673 */
1674void t_perl_generator::generate_serialize_list_element(ofstream &out,
1675 t_list* tlist,
1676 string iter) {
1677 t_field efield(tlist->get_elem_type(), iter);
1678 generate_serialize_field(out, &efield);
1679}
1680
1681/**
1682 * Declares a field, which may include initialization as necessary.
1683 *
1684 * @param ttype The type
1685 */
1686string t_perl_generator::declare_field(t_field* tfield, bool init, bool obj) {
1687 string result = "my $" + tfield->get_name();
1688 if (init) {
David Reisse087a302007-08-23 21:43:25 +00001689 t_type* type = get_true_type(tfield->get_type());
Mark Slee2c44d202007-05-16 02:18:07 +00001690 if (type->is_base_type()) {
1691 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
1692 switch (tbase) {
1693 case t_base_type::TYPE_VOID:
1694 break;
1695 case t_base_type::TYPE_STRING:
1696 result += " = ''";
1697 break;
1698 case t_base_type::TYPE_BOOL:
1699 result += " = 0";
1700 break;
1701 case t_base_type::TYPE_BYTE:
1702 case t_base_type::TYPE_I16:
1703 case t_base_type::TYPE_I32:
1704 case t_base_type::TYPE_I64:
1705 result += " = 0";
1706 break;
1707 case t_base_type::TYPE_DOUBLE:
1708 result += " = 0.0";
1709 break;
1710 default:
David Reissdd7796f2007-08-28 21:09:06 +00001711 throw "compiler error: no PERL initializer for base type " + t_base_type::t_base_name(tbase);
Mark Slee2c44d202007-05-16 02:18:07 +00001712 }
1713 } else if (type->is_enum()) {
1714 result += " = 0";
1715 } else if (type->is_container()) {
1716 result += " = []";
1717 } else if (type->is_struct() || type->is_xception()) {
1718 if (obj) {
1719 result += " = new " + perl_namespace(type->get_program()) + type->get_name() + "()";
1720 } else {
1721 result += " = undef";
1722 }
1723 }
1724 }
1725 return result + ";";
1726}
1727
1728/**
1729 * Renders a function signature of the form 'type name(args)'
1730 *
1731 * @param tfunction Function definition
1732 * @return String of rendered function definition
1733 */
1734string t_perl_generator::function_signature(t_function* tfunction,
1735 string prefix) {
1736
1737 string str;
1738
1739 str = prefix + tfunction->get_name() + "{\n";
1740 str += " my $self = shift;\n";
1741
1742 //Need to create perl function arg inputs
1743 const vector<t_field*> &fields = tfunction->get_arglist()->get_members();
1744 vector<t_field*>::const_iterator f_iter;
1745
1746 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1747 str += " my $" + (*f_iter)->get_name() + " = shift;\n";
1748 }
1749
1750 return str;
1751}
1752
1753/**
1754 * Renders a field list
1755 */
1756string t_perl_generator::argument_list(t_struct* tstruct) {
1757 string result = "";
1758
1759 const vector<t_field*>& fields = tstruct->get_members();
1760 vector<t_field*>::const_iterator f_iter;
1761 bool first = true;
1762 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1763 if (first) {
1764 first = false;
1765 } else {
1766 result += ", ";
1767 }
1768 result += "$" + (*f_iter)->get_name();
1769 }
1770 return result;
1771}
1772
1773/**
1774 * Converts the parse type to a C++ enum string for the given type.
1775 */
1776string t_perl_generator ::type_to_enum(t_type* type) {
David Reisse087a302007-08-23 21:43:25 +00001777 type = get_true_type(type);
Mark Slee2c44d202007-05-16 02:18:07 +00001778
1779 if (type->is_base_type()) {
1780 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
1781 switch (tbase) {
1782 case t_base_type::TYPE_VOID:
1783 throw "NO T_VOID CONSTRUCT";
1784 case t_base_type::TYPE_STRING:
1785 return "TType::STRING";
1786 case t_base_type::TYPE_BOOL:
1787 return "TType::BOOL";
1788 case t_base_type::TYPE_BYTE:
1789 return "TType::BYTE";
1790 case t_base_type::TYPE_I16:
1791 return "TType::I16";
1792 case t_base_type::TYPE_I32:
1793 return "TType::I32";
1794 case t_base_type::TYPE_I64:
1795 return "TType::I64";
1796 case t_base_type::TYPE_DOUBLE:
1797 return "TType::DOUBLE";
1798 }
1799 } else if (type->is_enum()) {
1800 return "TType::I32";
1801 } else if (type->is_struct() || type->is_xception()) {
1802 return "TType::STRUCT";
1803 } else if (type->is_map()) {
1804 return "TType::MAP";
1805 } else if (type->is_set()) {
1806 return "TType::SET";
1807 } else if (type->is_list()) {
1808 return "TType::LIST";
1809 }
1810
1811 throw "INVALID TYPE IN type_to_enum: " + type->get_name();
1812}
David Reiss2b386c52008-03-27 21:42:23 +00001813
David Reiss2b386c52008-03-27 21:42:23 +00001814THRIFT_REGISTER_GENERATOR(perl, "Perl", "");