From f69fb40c71bd5c432a359b7dd59083ade779ecba Mon Sep 17 00:00:00 2001 From: William Cheng Date: Thu, 7 May 2015 14:41:58 +0800 Subject: [PATCH] add methods for static variable --- .../main/resources/perl/BaseObject.mustache | 10 +-- .../src/main/resources/perl/object.mustache | 61 +++---------------- .../WWW/SwaggerClient/Object/BaseObject.pm | 10 +-- .../lib/WWW/SwaggerClient/Object/Category.pm | 57 ++--------------- .../lib/WWW/SwaggerClient/Object/Order.pm | 57 ++--------------- .../perl/lib/WWW/SwaggerClient/Object/Pet.pm | 57 ++--------------- .../perl/lib/WWW/SwaggerClient/Object/Tag.pm | 57 ++--------------- .../perl/lib/WWW/SwaggerClient/Object/User.pm | 57 ++--------------- samples/client/petstore/perl/test.pl | 22 ++++++- 9 files changed, 67 insertions(+), 321 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache b/modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache index be8937a16f0d..4a0be0aaea05 100644 --- a/modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache @@ -28,9 +28,9 @@ sub to_hash { sub TO_JSON { my $self = shift; my $_data = {}; - foreach my $_key (keys $self->{attribute_map}) { - if (defined $self->{$self->{attribute_map}->{$_key}}) { - $_data->{$self->{attribute_map}->{$_key}} = $self->{$_key}; + foreach my $_key (keys $self->get_attribute_map) { + if (defined $self->{$self->get_attribute_map->{$_key}}) { + $_data->{$self->get_attribute_map->{$_key}} = $self->{$_key}; } } return $_data; @@ -40,11 +40,11 @@ sub TO_JSON { sub from_hash { my ($self, $hash) = @_; # loop through attributes and use swagger_types to deserialize the data - while ( my ($_key, $_type) = each $self->{swagger_types}) { + while ( my ($_key, $_type) = each $self->get_swagger_types ) { if ($_type =~ /^array\[/i) { # array my $_subclass = substr($_type, 6, -1); my @_array = (); - foreach my $_element (@{$hash->{$self->{attribute_map}->{$_key}}}) { + foreach my $_element (@{$hash->{$self->get_attribute_map->{$_key}}}) { push @_array, $self->_deserialize($_subclass, $_element); } $self->{$_key} = \@_array; diff --git a/modules/swagger-codegen/src/main/resources/perl/object.mustache b/modules/swagger-codegen/src/main/resources/perl/object.mustache index 73b2e749ea33..34e504819a0d 100644 --- a/modules/swagger-codegen/src/main/resources/perl/object.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/object.mustache @@ -43,61 +43,16 @@ sub new { return bless $self, $class; } -# return json string -sub to_hash { - return decode_json(JSON->new->convert_blessed->encode( shift )); +# get swagger type of the attribute +sub get_swagger_types { + return $swagger_types; } -# used by JSON for serialization -sub TO_JSON { - my $self = shift; - my $_data = {}; - foreach my $_key (keys $attribute_map) { - if (defined $self->{$attribute_map->{$_key}}) { - $_data->{$attribute_map->{$_key}} = $self->{$_key}; - } - } - return $_data; +# get attribute mappping +sub get_attribute_map { + return $attribute_map; } -# from json string -sub from_hash { - my ($self, $hash) = @_; - # loop through attributes and use swagger_types to deserialize the data - while ( my ($_key, $_type) = each $swagger_types) { - if ($_type =~ /^array\[/i) { # array - my $_subclass = substr($_type, 6, -1); - my @_array = (); - foreach my $_element (@{$hash->{$attribute_map->{$_key}}}) { - push @_array, $self->_deserialize($_subclass, $_element); - } - $self->{$_key} = \@_array; - } elsif (defined $hash->{$_key}) { #hash(model), primitive, datetime - $self->{$_key} = $self->_deserialize($_type, $hash->{$_key}); - } else { - $log->debugf("warning: %s not defined\n", $_key); - } - } - - return $self; -} - -# deserialize non-array data -sub _deserialize { - my ($self, $type, $data) = @_; - $log->debugf("deserializing %s with %s",Dumper($data), $type); - - if ($type eq 'DateTime') { - return DateTime->from_epoch(epoch => str2time($data)); - } elsif ( grep( /^$type$/, ('string', 'int', 'float', 'bool')) ) { - return $data; - } else { # hash(model) - my $_instance = eval "WWW::{{invokerPackage}}::Object::$type->new()"; - return $_instance->from_hash($data); - } -} - -{{/model}} -{{/models}} - 1; +{{/model}} +{{/models}} diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/BaseObject.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/BaseObject.pm index 276b3f46f0f2..584710238adf 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/BaseObject.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/BaseObject.pm @@ -28,9 +28,9 @@ sub to_hash { sub TO_JSON { my $self = shift; my $_data = {}; - foreach my $_key (keys $self->{attribute_map}) { - if (defined $self->{$self->{attribute_map}->{$_key}}) { - $_data->{$self->{attribute_map}->{$_key}} = $self->{$_key}; + foreach my $_key (keys $self->get_attribute_map) { + if (defined $self->{$self->get_attribute_map->{$_key}}) { + $_data->{$self->get_attribute_map->{$_key}} = $self->{$_key}; } } return $_data; @@ -40,11 +40,11 @@ sub TO_JSON { sub from_hash { my ($self, $hash) = @_; # loop through attributes and use swagger_types to deserialize the data - while ( my ($_key, $_type) = each $self->{swagger_types}) { + while ( my ($_key, $_type) = each $self->get_swagger_types ) { if ($_type =~ /^array\[/i) { # array my $_subclass = substr($_type, 6, -1); my @_array = (); - foreach my $_element (@{$hash->{$self->{attribute_map}->{$_key}}}) { + foreach my $_element (@{$hash->{$self->get_attribute_map->{$_key}}}) { push @_array, $self->_deserialize($_subclass, $_element); } $self->{$_key} = \@_array; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Category.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Category.pm index b82bd5f82a2f..2ae61e3a7faa 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Category.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Category.pm @@ -42,59 +42,14 @@ sub new { return bless $self, $class; } -# return json string -sub to_hash { - return decode_json(JSON->new->convert_blessed->encode( shift )); +# get swagger type of the attribute +sub get_swagger_types { + return $swagger_types; } -# used by JSON for serialization -sub TO_JSON { - my $self = shift; - my $_data = {}; - foreach my $_key (keys $attribute_map) { - if (defined $self->{$attribute_map->{$_key}}) { - $_data->{$attribute_map->{$_key}} = $self->{$_key}; - } - } - return $_data; +# get attribute mappping +sub get_attribute_map { + return $attribute_map; } -# from json string -sub from_hash { - my ($self, $hash) = @_; - # loop through attributes and use swagger_types to deserialize the data - while ( my ($_key, $_type) = each $swagger_types) { - if ($_type =~ /^array\[/i) { # array - my $_subclass = substr($_type, 6, -1); - my @_array = (); - foreach my $_element (@{$hash->{$attribute_map->{$_key}}}) { - push @_array, $self->_deserialize($_subclass, $_element); - } - $self->{$_key} = \@_array; - } elsif (defined $hash->{$_key}) { #hash(model), primitive, datetime - $self->{$_key} = $self->_deserialize($_type, $hash->{$_key}); - } else { - $log->debugf("warning: %s not defined\n", $_key); - } - } - - return $self; -} - -# deserialize non-array data -sub _deserialize { - my ($self, $type, $data) = @_; - $log->debugf("deserializing %s with %s",Dumper($data), $type); - - if ($type eq 'DateTime') { - return DateTime->from_epoch(epoch => str2time($data)); - } elsif ( grep( /^$type$/, ('string', 'int', 'float', 'bool')) ) { - return $data; - } else { # hash(model) - my $_instance = eval "WWW::SwaggerClient::Object::$type->new()"; - return $_instance->from_hash($data); - } -} - - 1; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Order.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Order.pm index 35a6e96fac81..5693ecd2177c 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Order.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Order.pm @@ -58,59 +58,14 @@ sub new { return bless $self, $class; } -# return json string -sub to_hash { - return decode_json(JSON->new->convert_blessed->encode( shift )); +# get swagger type of the attribute +sub get_swagger_types { + return $swagger_types; } -# used by JSON for serialization -sub TO_JSON { - my $self = shift; - my $_data = {}; - foreach my $_key (keys $attribute_map) { - if (defined $self->{$attribute_map->{$_key}}) { - $_data->{$attribute_map->{$_key}} = $self->{$_key}; - } - } - return $_data; +# get attribute mappping +sub get_attribute_map { + return $attribute_map; } -# from json string -sub from_hash { - my ($self, $hash) = @_; - # loop through attributes and use swagger_types to deserialize the data - while ( my ($_key, $_type) = each $swagger_types) { - if ($_type =~ /^array\[/i) { # array - my $_subclass = substr($_type, 6, -1); - my @_array = (); - foreach my $_element (@{$hash->{$attribute_map->{$_key}}}) { - push @_array, $self->_deserialize($_subclass, $_element); - } - $self->{$_key} = \@_array; - } elsif (defined $hash->{$_key}) { #hash(model), primitive, datetime - $self->{$_key} = $self->_deserialize($_type, $hash->{$_key}); - } else { - $log->debugf("warning: %s not defined\n", $_key); - } - } - - return $self; -} - -# deserialize non-array data -sub _deserialize { - my ($self, $type, $data) = @_; - $log->debugf("deserializing %s with %s",Dumper($data), $type); - - if ($type eq 'DateTime') { - return DateTime->from_epoch(epoch => str2time($data)); - } elsif ( grep( /^$type$/, ('string', 'int', 'float', 'bool')) ) { - return $data; - } else { # hash(model) - my $_instance = eval "WWW::SwaggerClient::Object::$type->new()"; - return $_instance->from_hash($data); - } -} - - 1; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Pet.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Pet.pm index 1e692d7bb51e..6c31afb5b913 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Pet.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Pet.pm @@ -58,59 +58,14 @@ sub new { return bless $self, $class; } -# return json string -sub to_hash { - return decode_json(JSON->new->convert_blessed->encode( shift )); +# get swagger type of the attribute +sub get_swagger_types { + return $swagger_types; } -# used by JSON for serialization -sub TO_JSON { - my $self = shift; - my $_data = {}; - foreach my $_key (keys $attribute_map) { - if (defined $self->{$attribute_map->{$_key}}) { - $_data->{$attribute_map->{$_key}} = $self->{$_key}; - } - } - return $_data; +# get attribute mappping +sub get_attribute_map { + return $attribute_map; } -# from json string -sub from_hash { - my ($self, $hash) = @_; - # loop through attributes and use swagger_types to deserialize the data - while ( my ($_key, $_type) = each $swagger_types) { - if ($_type =~ /^array\[/i) { # array - my $_subclass = substr($_type, 6, -1); - my @_array = (); - foreach my $_element (@{$hash->{$attribute_map->{$_key}}}) { - push @_array, $self->_deserialize($_subclass, $_element); - } - $self->{$_key} = \@_array; - } elsif (defined $hash->{$_key}) { #hash(model), primitive, datetime - $self->{$_key} = $self->_deserialize($_type, $hash->{$_key}); - } else { - $log->debugf("warning: %s not defined\n", $_key); - } - } - - return $self; -} - -# deserialize non-array data -sub _deserialize { - my ($self, $type, $data) = @_; - $log->debugf("deserializing %s with %s",Dumper($data), $type); - - if ($type eq 'DateTime') { - return DateTime->from_epoch(epoch => str2time($data)); - } elsif ( grep( /^$type$/, ('string', 'int', 'float', 'bool')) ) { - return $data; - } else { # hash(model) - my $_instance = eval "WWW::SwaggerClient::Object::$type->new()"; - return $_instance->from_hash($data); - } -} - - 1; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Tag.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Tag.pm index b46b44927f80..45c570a75069 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Tag.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Tag.pm @@ -42,59 +42,14 @@ sub new { return bless $self, $class; } -# return json string -sub to_hash { - return decode_json(JSON->new->convert_blessed->encode( shift )); +# get swagger type of the attribute +sub get_swagger_types { + return $swagger_types; } -# used by JSON for serialization -sub TO_JSON { - my $self = shift; - my $_data = {}; - foreach my $_key (keys $attribute_map) { - if (defined $self->{$attribute_map->{$_key}}) { - $_data->{$attribute_map->{$_key}} = $self->{$_key}; - } - } - return $_data; +# get attribute mappping +sub get_attribute_map { + return $attribute_map; } -# from json string -sub from_hash { - my ($self, $hash) = @_; - # loop through attributes and use swagger_types to deserialize the data - while ( my ($_key, $_type) = each $swagger_types) { - if ($_type =~ /^array\[/i) { # array - my $_subclass = substr($_type, 6, -1); - my @_array = (); - foreach my $_element (@{$hash->{$attribute_map->{$_key}}}) { - push @_array, $self->_deserialize($_subclass, $_element); - } - $self->{$_key} = \@_array; - } elsif (defined $hash->{$_key}) { #hash(model), primitive, datetime - $self->{$_key} = $self->_deserialize($_type, $hash->{$_key}); - } else { - $log->debugf("warning: %s not defined\n", $_key); - } - } - - return $self; -} - -# deserialize non-array data -sub _deserialize { - my ($self, $type, $data) = @_; - $log->debugf("deserializing %s with %s",Dumper($data), $type); - - if ($type eq 'DateTime') { - return DateTime->from_epoch(epoch => str2time($data)); - } elsif ( grep( /^$type$/, ('string', 'int', 'float', 'bool')) ) { - return $data; - } else { # hash(model) - my $_instance = eval "WWW::SwaggerClient::Object::$type->new()"; - return $_instance->from_hash($data); - } -} - - 1; diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/User.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/User.pm index 08d9ff6b0801..f5d7c1dbce2b 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/User.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/User.pm @@ -66,59 +66,14 @@ sub new { return bless $self, $class; } -# return json string -sub to_hash { - return decode_json(JSON->new->convert_blessed->encode( shift )); +# get swagger type of the attribute +sub get_swagger_types { + return $swagger_types; } -# used by JSON for serialization -sub TO_JSON { - my $self = shift; - my $_data = {}; - foreach my $_key (keys $attribute_map) { - if (defined $self->{$attribute_map->{$_key}}) { - $_data->{$attribute_map->{$_key}} = $self->{$_key}; - } - } - return $_data; +# get attribute mappping +sub get_attribute_map { + return $attribute_map; } -# from json string -sub from_hash { - my ($self, $hash) = @_; - # loop through attributes and use swagger_types to deserialize the data - while ( my ($_key, $_type) = each $swagger_types) { - if ($_type =~ /^array\[/i) { # array - my $_subclass = substr($_type, 6, -1); - my @_array = (); - foreach my $_element (@{$hash->{$attribute_map->{$_key}}}) { - push @_array, $self->_deserialize($_subclass, $_element); - } - $self->{$_key} = \@_array; - } elsif (defined $hash->{$_key}) { #hash(model), primitive, datetime - $self->{$_key} = $self->_deserialize($_type, $hash->{$_key}); - } else { - $log->debugf("warning: %s not defined\n", $_key); - } - } - - return $self; -} - -# deserialize non-array data -sub _deserialize { - my ($self, $type, $data) = @_; - $log->debugf("deserializing %s with %s",Dumper($data), $type); - - if ($type eq 'DateTime') { - return DateTime->from_epoch(epoch => str2time($data)); - } elsif ( grep( /^$type$/, ('string', 'int', 'float', 'bool')) ) { - return $data; - } else { # hash(model) - my $_instance = eval "WWW::SwaggerClient::Object::$type->new()"; - return $_instance->from_hash($data); - } -} - - 1; diff --git a/samples/client/petstore/perl/test.pl b/samples/client/petstore/perl/test.pl index 25ac786166cf..ed7dff0b33c2 100644 --- a/samples/client/petstore/perl/test.pl +++ b/samples/client/petstore/perl/test.pl @@ -11,20 +11,35 @@ use WWW::SwaggerClient::Object::Tag; use WWW::SwaggerClient::Object::Category; use JSON; use Data::Dumper; +use DateTime; my $api = WWW::SwaggerClient::PetApi->new(); #print WWW::SwaggerClient::APIClient::to_form_value('testing 123'); my $pet_id = 5; +my $dt = DateTime->new( + year => 1972, + month => 12, + day => 31, + hour => 23, + minute => 59, + second => 30, + time_zone => 'UTC' +); + +print "dt = $dt\n"; +print "dt = ".$dt->datetime()."\n"; + my $tag = WWW::SwaggerClient::Object::Tag->new({'id' => 'tag1', 'name' => 'just kidding', "photoUrls" => ['123', 'oop']}); my $pet = WWW::SwaggerClient::Object::Pet->new({'id' => 5, 'name' => 'haha', - "photoUrls" => ['123', 'oop'], 'tags' => [$tag], 'created' => '2003-04-05 02:58:00'}); + "photoUrls" => ['123', 'oop'], 'tags' => [$tag], 'created' => $dt}); ##print Dumper $pet; - +print "pet ref=".ref($pet)."\n"; +print "tag ref=".ref($tag)."\n"; my $json = JSON->new->convert_blessed; #print $json->convert_blessed->encode($pet); @@ -34,7 +49,8 @@ print Dumper($pet->to_hash); my $pet2 = WWW::SwaggerClient::Object::Pet->new(); $pet2 = $pet2->from_hash($pet->to_hash); #$pet2->from_json($pet->to_hash); -##print Dumper($pet2->to_hash); +print Dumper($pet2->to_hash); +#exit; print "============================\n"; print Dumper $api->get_pet_by_id({pet_id => $pet_id}); print Dumper $api->update_pet_with_form({pet_id => $pet_id, name => 'test_name', status => 'test status'});