Skip to content

[Perl] more flexible perl module naming (Allow::This even without WWW::) #1812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ samples/client/petstore/php/SwaggerClient-php/vendor/
samples/client/petstore/silex/SwaggerServer/composer.lock
samples/client/petstore/silex/SwaggerServer/venodr/

samples/client/petstore/perl/deep_module_test/

samples/client/petstore/python/.projectile
samples/client/petstore/python/.venv/

Expand Down
3 changes: 3 additions & 0 deletions bin/perl-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
# complex module name used for testing
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l perl -o samples/client/petstore/perl"

java $JAVA_OPTS -jar $executable $ags

java $JAVA_OPTS -jar $executable $ags --additional-properties moduleName=Something::Deep -o samples/client/petstore/perl/deep_module_test
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig {
public static final String MODULE_NAME = "moduleName";
public static final String MODULE_VERSION = "moduleVersion";
protected String moduleName = "SwaggerClient";
protected String moduleName = "WWW::SwaggerClient";
protected String modulePathPart = moduleName.replaceAll("::",String.valueOf(File.separatorChar));
protected String moduleVersion = "1.0.0";

public PerlClientCodegen() {
Expand Down Expand Up @@ -72,7 +73,7 @@ public PerlClientCodegen() {
typeMapping.put("object", "object");

cliOptions.clear();
cliOptions.add(new CliOption(MODULE_NAME, "Perl module name (convention: CamelCase).").defaultValue("SwaggerClient"));
cliOptions.add(new CliOption(MODULE_NAME, "Perl module name (convention: CamelCase or Long::Module).").defaultValue("SwaggerClient"));
cliOptions.add(new CliOption(MODULE_VERSION, "Perl module version.").defaultValue("1.0.0"));
cliOptions.add(CliOption.newBoolean(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG,
CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG_DESC).defaultValue(Boolean.TRUE.toString()));
Expand All @@ -94,15 +95,16 @@ public void processOpts() {

if (additionalProperties.containsKey(MODULE_NAME)) {
setModuleName((String) additionalProperties.get(MODULE_NAME));
setModulePathPart(moduleName.replaceAll("::",String.valueOf(File.separatorChar)));
} else {
additionalProperties.put(MODULE_NAME, moduleName);
}

supportingFiles.add(new SupportingFile("ApiClient.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "ApiClient.pm"));
supportingFiles.add(new SupportingFile("Configuration.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "Configuration.pm"));
supportingFiles.add(new SupportingFile("ApiFactory.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "ApiFactory.pm"));
supportingFiles.add(new SupportingFile("Role.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "Role.pm"));
supportingFiles.add(new SupportingFile("AutoDoc.mustache", ("lib/WWW/" + moduleName + "/Role").replace('/', File.separatorChar), "AutoDoc.pm"));
supportingFiles.add(new SupportingFile("ApiClient.mustache", ("lib/" + modulePathPart).replace('/', File.separatorChar), "ApiClient.pm"));
supportingFiles.add(new SupportingFile("Configuration.mustache", ("lib/" + modulePathPart).replace('/', File.separatorChar), "Configuration.pm"));
supportingFiles.add(new SupportingFile("ApiFactory.mustache", ("lib/" + modulePathPart).replace('/', File.separatorChar), "ApiFactory.pm"));
supportingFiles.add(new SupportingFile("Role.mustache", ("lib/" + modulePathPart).replace('/', File.separatorChar), "Role.pm"));
supportingFiles.add(new SupportingFile("AutoDoc.mustache", ("lib/" + modulePathPart + "/Role").replace('/', File.separatorChar), "AutoDoc.pm"));
supportingFiles.add(new SupportingFile("autodoc.script.mustache", "bin", "autodoc"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
}
Expand All @@ -129,12 +131,12 @@ public String escapeReservedWord(String name) {

@Override
public String apiFileFolder() {
return (outputFolder + "/lib/WWW/" + moduleName + apiPackage()).replace('/', File.separatorChar);
return (outputFolder + "/lib/" + modulePathPart + apiPackage()).replace('/', File.separatorChar);
}

@Override
public String modelFileFolder() {
return (outputFolder + "/lib/WWW/" + moduleName + modelPackage()).replace('/', File.separatorChar);
return (outputFolder + "/lib/" + modulePathPart + modelPackage()).replace('/', File.separatorChar);
}

@Override
Expand Down Expand Up @@ -251,6 +253,10 @@ public void setModuleName(String moduleName) {
this.moduleName = moduleName;
}

public void setModulePathPart(String modulePathPart) {
this.modulePathPart = modulePathPart;
}

public void setModuleVersion(String moduleVersion) {
this.moduleVersion = moduleVersion;
}
Expand Down
24 changes: 12 additions & 12 deletions modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package WWW::{{moduleName}}::ApiClient;
package {{moduleName}}::ApiClient;

use strict;
use warnings;
Expand All @@ -18,7 +18,7 @@ use Log::Any qw($log);
use Carp;
use Module::Runtime qw(use_module);

use WWW::{{moduleName}}::Configuration;
use {{moduleName}}::Configuration;

use base 'Class::Singleton';

Expand All @@ -34,7 +34,7 @@ sub _new_instance
return bless \%args, $class;
}

sub _cfg {'WWW::{{moduleName}}::Configuration'}
sub _cfg {'{{moduleName}}::Configuration'}

# Set the user agent of the API client
#
Expand Down Expand Up @@ -119,8 +119,8 @@ sub call_api {
else {
}

$self->{ua}->timeout($self->{http_timeout} || $WWW::{{moduleName}}::Configuration::http_timeout);
$self->{ua}->agent($self->{http_user_agent} || $WWW::{{moduleName}}::Configuration::http_user_agent);
$self->{ua}->timeout($self->{http_timeout} || ${{moduleName}}::Configuration::http_timeout);
$self->{ua}->agent($self->{http_user_agent} || ${{moduleName}}::Configuration::http_user_agent);

$log->debugf("REQUEST: %s", $_request->as_string);
my $_response = $self->{ua}->request($_request);
Expand Down Expand Up @@ -243,7 +243,7 @@ sub deserialize
} elsif (grep /^$class$/, ('string', 'int', 'float', 'bool', 'object')) {
return $data;
} else { # model
my $_instance = use_module("WWW::{{moduleName}}::Object::$class")->new;
my $_instance = use_module("{{moduleName}}::Object::$class")->new;
if (ref $data eq "HASH") {
return $_instance->from_hash($data);
} else { # string, need to json decode first
Expand Down Expand Up @@ -294,11 +294,11 @@ sub get_api_key_with_prefix
{
my ($self, $key_name) = @_;

my $api_key = $WWW::{{moduleName}}::Configuration::api_key->{$key_name};
my $api_key = ${{moduleName}}::Configuration::api_key->{$key_name};

return unless $api_key;

my $prefix = $WWW::{{moduleName}}::Configuration::api_key_prefix->{$key_name};
my $prefix = ${{moduleName}}::Configuration::api_key_prefix->{$key_name};
return $prefix ? "$prefix $api_key" : $api_key;
}

Expand Down Expand Up @@ -329,11 +329,11 @@ sub update_params_for_auth {
if ($api_key) {
$query_params->{'{{keyParamName}}'} = $api_key;
}{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}
if ($WWW::{{moduleName}}::Configuration::username || $WWW::{{moduleName}}::Configuration::password) {
$header_params->{'Authorization'} = 'Basic ' . encode_base64($WWW::{{moduleName}}::Configuration::username . ":" . $WWW::{{moduleName}}::Configuration::password);
if (${{moduleName}}::Configuration::username || ${{moduleName}}::Configuration::password) {
$header_params->{'Authorization'} = 'Basic ' . encode_base64(${{moduleName}}::Configuration::username . ":" . ${{moduleName}}::Configuration::password);
}{{/isBasic}}{{#isOAuth}}
if ($WWW::{{moduleName}}::Configuration::access_token) {
$header_params->{'Authorization'} = 'Bearer ' . $WWW::{{moduleName}}::Configuration::access_token;
if (${{moduleName}}::Configuration::access_token) {
$header_params->{'Authorization'} = 'Bearer ' . ${{moduleName}}::Configuration::access_token;
}{{/isOAuth}}
}
{{/authMethods}}
Expand Down
22 changes: 11 additions & 11 deletions modules/swagger-codegen/src/main/resources/perl/ApiFactory.mustache
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package WWW::{{moduleName}}::ApiFactory;
package {{moduleName}}::ApiFactory;

use strict;
use warnings;
Expand All @@ -7,26 +7,26 @@ use utf8;
use Carp;
use Module::Find;

usesub WWW::{{moduleName}}::Object;
usesub {{moduleName}}::Object;

use WWW::{{moduleName}}::ApiClient;
use {{moduleName}}::ApiClient;

=head1 Name

WWW::{{moduleName}}::ApiFactory - constructs APIs to retrieve {{moduleName}} objects
{{moduleName}}::ApiFactory - constructs APIs to retrieve {{moduleName}} objects

=head1 Synopsis

package My::Petstore::App;

use WWW::{{moduleName}}::ApiFactory;
use {{moduleName}}::ApiFactory;

my $api_factory = WWW::{{moduleName}}::ApiFactory->new( ... ); # any args for ApiClient constructor
my $api_factory = {{moduleName}}::ApiFactory->new( ... ); # any args for ApiClient constructor

# later...
my $pet_api = $api_factory->get_api('Pet');

# $pet_api isa WWW::{{moduleName}}::PetApi
# $pet_api isa {{moduleName}}::PetApi

my $pet = $pet_api->get_pet_by_id(pet_id => $pet_id);

Expand All @@ -39,9 +39,9 @@ use WWW::{{moduleName}}::ApiClient;
=cut

# Load all the API classes and construct a lookup table at startup time
my %_apis = map { $_ =~ /^WWW::{{moduleName}}::(.*)$/; $1 => $_ }
my %_apis = map { $_ =~ /^{{moduleName}}::(.*)$/; $1 => $_ }
grep {$_ =~ /Api$/}
usesub 'WWW::{{moduleName}}';
usesub '{{moduleName}}';

=head1 new()

Expand All @@ -54,7 +54,7 @@ my %_apis = map { $_ =~ /^WWW::{{moduleName}}::(.*)$/; $1 => $_ }

sub new {
my ($class, %p) = (shift, @_);
$p{api_client} = WWW::{{moduleName}}::ApiClient->instance(%p);
$p{api_client} = {{moduleName}}::ApiClient->instance(%p);
return bless \%p, $class;
}

Expand All @@ -64,7 +64,7 @@ sub new {

$which is a nickname for the class:

WWW::FooBarClient::BazApi has nickname 'Baz'
FooBarClient::BazApi has nickname 'Baz'

=cut

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package WWW::{{moduleName}}::Role::AutoDoc;
package {{moduleName}}::Role::AutoDoc;
use List::MoreUtils qw(uniq);

use Moose::Role;
Expand Down Expand Up @@ -40,7 +40,7 @@ sub _printisa {

foreach my $role (@roles) {
$rolepkg = $role->{package} || next; # some are anonymous, or something
next if $rolepkg eq 'WWW::{{moduleName}}::Role::AutoDoc';
next if $rolepkg eq '{{moduleName}}::Role::AutoDoc';
$role_reqs = join ', ', keys %{$role->{required_methods}};
$role_reqs ||= '';
$~ = $how eq 'pod' ? 'ROLES_POD' : 'ROLES';
Expand Down Expand Up @@ -424,4 +424,4 @@ $attrname



1;
1;
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ sub _deserialize {
} elsif ( grep( /^$type$/, ('int', 'double', 'string', 'boolean'))) {
return $data;
} else { # hash(model)
my $_instance = eval "WWW::{{moduleName}}::Object::$type->new()";
my $_instance = eval "{{moduleName}}::Object::$type->new()";
return $_instance->from_hash($data);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package WWW::{{moduleName}}::Configuration;
package {{moduleName}}::Configuration;

use strict;
use warnings;
Expand Down
26 changes: 13 additions & 13 deletions modules/swagger-codegen/src/main/resources/perl/README.mustache
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NAME

WWW::{{moduleName}}::Role - a Moose role for the {{appName}}
{{moduleName}}::Role - a Moose role for the {{appName}}

## {{appName}} version: {{appVersion}}

Expand All @@ -15,7 +15,7 @@ Automatically generated by the Perl Swagger Codegen project:
## A note on Moose

This role is the only component of the library that uses Moose. See
WWW::{{moduleName}}::ApiFactory for non-Moosey usage.
{{moduleName}}::ApiFactory for non-Moosey usage.

# SYNOPSIS

Expand All @@ -29,7 +29,7 @@ role.

package MyApp;
use Moose;
with 'WWW::{{moduleName}}::Role';
with '{{moduleName}}::Role';

package main;

Expand Down Expand Up @@ -88,37 +88,37 @@ you are accessing. Usually `prefix` and `in` will be determined by the code gene
the spec and you will not need to set them at run time. If not, `in` will
default to 'head' and `prefix` to the empty string.

The tokens will be placed in the `WWW::{{moduleName}}::Configuration` namespace
The tokens will be placed in the `{{moduleName}}::Configuration` namespace
as follows, but you don't need to know about this.

- `$WWW::{{moduleName}}::Configuration::username`
- `${{moduleName}}::Configuration::username`

String. The username for basic auth.

- `$WWW::{{moduleName}}::Configuration::password`
- `${{moduleName}}::Configuration::password`

String. The password for basic auth.

- `$WWW::{{moduleName}}::Configuration::api_key`
- `${{moduleName}}::Configuration::api_key`

Hashref. Keyed on the name of each key (there can be multiple tokens).

$WWW::{{moduleName}}::Configuration::api_key = {
${{moduleName}}::Configuration::api_key = {
secretKey => 'aaaabbbbccccdddd',
anotherKey => '1111222233334444',
};

- `$WWW::{{moduleName}}::Configuration::api_key_prefix`
- `${{moduleName}}::Configuration::api_key_prefix`

Hashref. Keyed on the name of each key (there can be multiple tokens). Note not
all api keys require a prefix.

$WWW::{{moduleName}}::Configuration::api_key_prefix = {
${{moduleName}}::Configuration::api_key_prefix = {
secretKey => 'string',
anotherKey => 'same or some other string',
};

- `$WWW::{{moduleName}}::Configuration::access_token`
- `${{moduleName}}::Configuration::access_token`

String. The OAuth access token.

Expand All @@ -134,7 +134,7 @@ created yet) the current value of `base_url`.

Returns an API factory object. You probably won't need to call this directly.

$self->api_factory('Pet'); # returns a WWW::{{moduleName}}::PetApi instance
$self->api_factory('Pet'); # returns a {{moduleName}}::PetApi instance

$self->pet_api; # the same

Expand Down Expand Up @@ -167,7 +167,7 @@ maven 3.0.3 or better already installed.

The config file should specify the project name for the generated library:

{"moduleName":"MyProjectName"}
{"moduleName":"WWW::MyProjectName"}

Your library files will be built under `WWW::MyProjectName`.

Expand Down
Loading