From f521680c0f1f3d796468b69fe8b0fe3aada0af7f Mon Sep 17 00:00:00 2001 From: Dave Baird Date: Wed, 4 Nov 2015 21:42:27 +0100 Subject: [PATCH] Moved object classes' constructor into base class --- .../main/resources/perl/BaseObject.mustache | 26 ++++++++--- .../src/main/resources/perl/object.mustache | 32 +++---------- .../WWW/SwaggerClient/Object/ApiResponse.pm | 35 +++------------ .../WWW/SwaggerClient/Object/BaseObject.pm | 26 ++++++++--- .../lib/WWW/SwaggerClient/Object/Category.pm | 33 +++----------- .../lib/WWW/SwaggerClient/Object/Order.pm | 41 +++-------------- .../perl/lib/WWW/SwaggerClient/Object/Pet.pm | 41 +++-------------- .../perl/lib/WWW/SwaggerClient/Object/Tag.pm | 33 +++----------- .../perl/lib/WWW/SwaggerClient/Object/User.pm | 45 +++---------------- 9 files changed, 77 insertions(+), 235 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache b/modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache index 58c047f49a4..f2a58efdb68 100644 --- a/modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache @@ -11,7 +11,7 @@ use Log::Any qw($log); use Date::Parse; use DateTime; -use base "Class::Accessor"; +use base ("Class::Accessor", "Class::Data::Inheritable"); # @@ -20,6 +20,22 @@ use base "Class::Accessor"; #NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. # +__PACKAGE__->mk_classdata('attribute_map'); +__PACKAGE__->mk_classdata('swagger_types'); + +# new object +sub new { + my ($class, %args) = @_; + + my $self = bless {}, $class; + + foreach my $attribute (keys %{$class->attribute_map}) { + my $args_key = $class->attribute_map->{$attribute}; + $self->$attribute( $args{ $args_key } ); + } + + return $self; +} # return perl hash sub to_hash { @@ -30,9 +46,9 @@ sub to_hash { sub TO_JSON { my $self = shift; my $_data = {}; - foreach my $_key (keys %{$self->get_attribute_map}) { + foreach my $_key (keys %{$self->attribute_map}) { if (defined $self->{$_key}) { - $_data->{$self->get_attribute_map->{$_key}} = $self->{$_key}; + $_data->{$self->attribute_map->{$_key}} = $self->{$_key}; } } return $_data; @@ -43,8 +59,8 @@ sub from_hash { my ($self, $hash) = @_; # loop through attributes and use swagger_types to deserialize the data - while ( my ($_key, $_type) = each %{$self->get_swagger_types} ) { - my $_json_attribute = $self->get_attribute_map->{$_key}; + while ( my ($_key, $_type) = each %{$self->swagger_types} ) { + my $_json_attribute = $self->attribute_map->{$_key}; if ($_type =~ /^array\[/i) { # array my $_subclass = substr($_type, 6, -1); my @_array = (); diff --git a/modules/swagger-codegen/src/main/resources/perl/object.mustache b/modules/swagger-codegen/src/main/resources/perl/object.mustache index fc418e79ce4..bbddd3c7f32 100644 --- a/modules/swagger-codegen/src/main/resources/perl/object.mustache +++ b/modules/swagger-codegen/src/main/resources/perl/object.mustache @@ -21,39 +21,17 @@ use base "WWW::{{moduleName}}::Object::BaseObject"; #NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. # -my $swagger_types = { +__PACKAGE__->swagger_types( { {{#vars}}'{{name}}' => '{{{datatype}}}'{{#hasMore}}, {{/hasMore}}{{/vars}} -}; +} ); -my $attribute_map = { +__PACKAGE__->attribute_map( { {{#vars}}'{{name}}' => '{{baseName}}'{{#hasMore}}, {{/hasMore}}{{/vars}} -}; +} ); -__PACKAGE__->mk_accessors(keys %$attribute_map); - -# new object -sub new { - my ($class, %args) = @_; - my $self = { - {{#vars}}#{{#description}}{{{description}}}{{/description}} - '{{name}}' => $args{'{{baseName}}'}{{#hasMore}}, - {{/hasMore}}{{/vars}} - }; - - return bless $self, $class; -} - -# get swagger type of the attribute -sub get_swagger_types { - return $swagger_types; -} - -# get attribute mappping -sub get_attribute_map { - return $attribute_map; -} +__PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map}); 1; {{/model}} diff --git a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/ApiResponse.pm b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/ApiResponse.pm index 029aad32b9b..9d3de7648aa 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/ApiResponse.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/ApiResponse.pm @@ -19,43 +19,18 @@ use base "WWW::SwaggerClient::Object::BaseObject"; #NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. # -my $swagger_types = { +__PACKAGE__->swagger_types( { 'code' => 'int', 'type' => 'string', 'message' => 'string' -}; +} ); -my $attribute_map = { +__PACKAGE__->attribute_map( { 'code' => 'code', 'type' => 'type', 'message' => 'message' -}; +} ); -__PACKAGE__->mk_accessors(keys %$attribute_map); - -# new object -sub new { - my ($class, %args) = @_; - my $self = { - # - 'code' => $args{'code'}, - # - 'type' => $args{'type'}, - # - 'message' => $args{'message'} - }; - - return bless $self, $class; -} - -# get swagger type of the attribute -sub get_swagger_types { - return $swagger_types; -} - -# get attribute mappping -sub get_attribute_map { - return $attribute_map; -} +__PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map}); 1; 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 fcdbe3fc14e..fee2b06fb0e 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/BaseObject.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/BaseObject.pm @@ -11,7 +11,7 @@ use Log::Any qw($log); use Date::Parse; use DateTime; -use base "Class::Accessor"; +use base ("Class::Accessor", "Class::Data::Inheritable"); # @@ -20,6 +20,22 @@ use base "Class::Accessor"; #NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. # +__PACKAGE__->mk_classdata('attribute_map'); +__PACKAGE__->mk_classdata('swagger_types'); + +# new object +sub new { + my ($class, %args) = @_; + + my $self = bless {}, $class; + + foreach my $attribute (keys %{$class->attribute_map}) { + my $args_key = $class->attribute_map->{$attribute}; + $self->$attribute( $args{ $args_key } ); + } + + return $self; +} # return perl hash sub to_hash { @@ -30,9 +46,9 @@ sub to_hash { sub TO_JSON { my $self = shift; my $_data = {}; - foreach my $_key (keys %{$self->get_attribute_map}) { + foreach my $_key (keys %{$self->attribute_map}) { if (defined $self->{$_key}) { - $_data->{$self->get_attribute_map->{$_key}} = $self->{$_key}; + $_data->{$self->attribute_map->{$_key}} = $self->{$_key}; } } return $_data; @@ -43,8 +59,8 @@ sub from_hash { my ($self, $hash) = @_; # loop through attributes and use swagger_types to deserialize the data - while ( my ($_key, $_type) = each %{$self->get_swagger_types} ) { - my $_json_attribute = $self->get_attribute_map->{$_key}; + while ( my ($_key, $_type) = each %{$self->swagger_types} ) { + my $_json_attribute = $self->attribute_map->{$_key}; if ($_type =~ /^array\[/i) { # array my $_subclass = substr($_type, 6, -1); my @_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 7aa9b642824..1e062925037 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Category.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Category.pm @@ -19,39 +19,16 @@ use base "WWW::SwaggerClient::Object::BaseObject"; #NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. # -my $swagger_types = { +__PACKAGE__->swagger_types( { 'id' => 'int', 'name' => 'string' -}; +} ); -my $attribute_map = { +__PACKAGE__->attribute_map( { 'id' => 'id', 'name' => 'name' -}; +} ); -__PACKAGE__->mk_accessors(keys %$attribute_map); - -# new object -sub new { - my ($class, %args) = @_; - my $self = { - # - 'id' => $args{'id'}, - # - 'name' => $args{'name'} - }; - - return bless $self, $class; -} - -# get swagger type of the attribute -sub get_swagger_types { - return $swagger_types; -} - -# get attribute mappping -sub get_attribute_map { - return $attribute_map; -} +__PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map}); 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 f7016179e3a..70b3db2cfc0 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Order.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Order.pm @@ -19,55 +19,24 @@ use base "WWW::SwaggerClient::Object::BaseObject"; #NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. # -my $swagger_types = { +__PACKAGE__->swagger_types( { 'id' => 'int', 'pet_id' => 'int', 'quantity' => 'int', 'ship_date' => 'DateTime', 'status' => 'string', 'complete' => 'boolean' -}; +} ); -my $attribute_map = { +__PACKAGE__->attribute_map( { 'id' => 'id', 'pet_id' => 'petId', 'quantity' => 'quantity', 'ship_date' => 'shipDate', 'status' => 'status', 'complete' => 'complete' -}; +} ); -__PACKAGE__->mk_accessors(keys %$attribute_map); - -# new object -sub new { - my ($class, %args) = @_; - my $self = { - # - 'id' => $args{'id'}, - # - 'pet_id' => $args{'petId'}, - # - 'quantity' => $args{'quantity'}, - # - 'ship_date' => $args{'shipDate'}, - #Order Status - 'status' => $args{'status'}, - # - 'complete' => $args{'complete'} - }; - - return bless $self, $class; -} - -# get swagger type of the attribute -sub get_swagger_types { - return $swagger_types; -} - -# get attribute mappping -sub get_attribute_map { - return $attribute_map; -} +__PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map}); 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 20b9262e49f..3ff3f50b399 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Pet.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Pet.pm @@ -19,55 +19,24 @@ use base "WWW::SwaggerClient::Object::BaseObject"; #NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. # -my $swagger_types = { +__PACKAGE__->swagger_types( { 'id' => 'int', 'category' => 'Category', 'name' => 'string', 'photo_urls' => 'ARRAY[string]', 'tags' => 'ARRAY[Tag]', 'status' => 'string' -}; +} ); -my $attribute_map = { +__PACKAGE__->attribute_map( { 'id' => 'id', 'category' => 'category', 'name' => 'name', 'photo_urls' => 'photoUrls', 'tags' => 'tags', 'status' => 'status' -}; +} ); -__PACKAGE__->mk_accessors(keys %$attribute_map); - -# new object -sub new { - my ($class, %args) = @_; - my $self = { - # - 'id' => $args{'id'}, - # - 'category' => $args{'category'}, - # - 'name' => $args{'name'}, - # - 'photo_urls' => $args{'photoUrls'}, - # - 'tags' => $args{'tags'}, - #pet status in the store - 'status' => $args{'status'} - }; - - return bless $self, $class; -} - -# get swagger type of the attribute -sub get_swagger_types { - return $swagger_types; -} - -# get attribute mappping -sub get_attribute_map { - return $attribute_map; -} +__PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map}); 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 2c585b1ec80..bf97f210a37 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Tag.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/Tag.pm @@ -19,39 +19,16 @@ use base "WWW::SwaggerClient::Object::BaseObject"; #NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. # -my $swagger_types = { +__PACKAGE__->swagger_types( { 'id' => 'int', 'name' => 'string' -}; +} ); -my $attribute_map = { +__PACKAGE__->attribute_map( { 'id' => 'id', 'name' => 'name' -}; +} ); -__PACKAGE__->mk_accessors(keys %$attribute_map); - -# new object -sub new { - my ($class, %args) = @_; - my $self = { - # - 'id' => $args{'id'}, - # - 'name' => $args{'name'} - }; - - return bless $self, $class; -} - -# get swagger type of the attribute -sub get_swagger_types { - return $swagger_types; -} - -# get attribute mappping -sub get_attribute_map { - return $attribute_map; -} +__PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map}); 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 271f5e91ef7..36fc7baf6ee 100644 --- a/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/User.pm +++ b/samples/client/petstore/perl/lib/WWW/SwaggerClient/Object/User.pm @@ -19,7 +19,7 @@ use base "WWW::SwaggerClient::Object::BaseObject"; #NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. # -my $swagger_types = { +__PACKAGE__->swagger_types( { 'id' => 'int', 'username' => 'string', 'first_name' => 'string', @@ -28,9 +28,9 @@ my $swagger_types = { 'password' => 'string', 'phone' => 'string', 'user_status' => 'int' -}; +} ); -my $attribute_map = { +__PACKAGE__->attribute_map( { 'id' => 'id', 'username' => 'username', 'first_name' => 'firstName', @@ -39,43 +39,8 @@ my $attribute_map = { 'password' => 'password', 'phone' => 'phone', 'user_status' => 'userStatus' -}; +} ); -__PACKAGE__->mk_accessors(keys %$attribute_map); - -# new object -sub new { - my ($class, %args) = @_; - my $self = { - # - 'id' => $args{'id'}, - # - 'username' => $args{'username'}, - # - 'first_name' => $args{'firstName'}, - # - 'last_name' => $args{'lastName'}, - # - 'email' => $args{'email'}, - # - 'password' => $args{'password'}, - # - 'phone' => $args{'phone'}, - #User Status - 'user_status' => $args{'userStatus'} - }; - - return bless $self, $class; -} - -# get swagger type of the attribute -sub get_swagger_types { - return $swagger_types; -} - -# get attribute mappping -sub get_attribute_map { - return $attribute_map; -} +__PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map}); 1;