Skip to content

Commit fc218d0

Browse files
committed
Merge pull request #1812 from captin411/perl-flexible-module-name
[Perl] more flexible perl module naming (Allow::This even without WWW::)
2 parents 3a2ad9e + 99842f6 commit fc218d0

File tree

19 files changed

+200
-93
lines changed

19 files changed

+200
-93
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ samples/client/petstore/php/SwaggerClient-php/vendor/
5555
samples/client/petstore/silex/SwaggerServer/composer.lock
5656
samples/client/petstore/silex/SwaggerServer/venodr/
5757

58+
samples/client/petstore/perl/deep_module_test/
59+
5860
samples/client/petstore/python/.projectile
5961
samples/client/petstore/python/.venv/
6062

bin/perl-petstore.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ fi
2626

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

3132
java $JAVA_OPTS -jar $executable $ags
33+
34+
java $JAVA_OPTS -jar $executable $ags --additional-properties moduleName=Something::Deep -o samples/client/petstore/perl/deep_module_test

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PerlClientCodegen.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig {
2121
public static final String MODULE_NAME = "moduleName";
2222
public static final String MODULE_VERSION = "moduleVersion";
23-
protected String moduleName = "SwaggerClient";
23+
protected String moduleName = "WWW::SwaggerClient";
24+
protected String modulePathPart = moduleName.replaceAll("::",String.valueOf(File.separatorChar));
2425
protected String moduleVersion = "1.0.0";
2526

2627
public PerlClientCodegen() {
@@ -72,7 +73,7 @@ public PerlClientCodegen() {
7273
typeMapping.put("object", "object");
7374

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

9596
if (additionalProperties.containsKey(MODULE_NAME)) {
9697
setModuleName((String) additionalProperties.get(MODULE_NAME));
98+
setModulePathPart(moduleName.replaceAll("::",String.valueOf(File.separatorChar)));
9799
} else {
98100
additionalProperties.put(MODULE_NAME, moduleName);
99101
}
100102

101-
supportingFiles.add(new SupportingFile("ApiClient.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "ApiClient.pm"));
102-
supportingFiles.add(new SupportingFile("Configuration.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "Configuration.pm"));
103-
supportingFiles.add(new SupportingFile("ApiFactory.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "ApiFactory.pm"));
104-
supportingFiles.add(new SupportingFile("Role.mustache", ("lib/WWW/" + moduleName).replace('/', File.separatorChar), "Role.pm"));
105-
supportingFiles.add(new SupportingFile("AutoDoc.mustache", ("lib/WWW/" + moduleName + "/Role").replace('/', File.separatorChar), "AutoDoc.pm"));
103+
supportingFiles.add(new SupportingFile("ApiClient.mustache", ("lib/" + modulePathPart).replace('/', File.separatorChar), "ApiClient.pm"));
104+
supportingFiles.add(new SupportingFile("Configuration.mustache", ("lib/" + modulePathPart).replace('/', File.separatorChar), "Configuration.pm"));
105+
supportingFiles.add(new SupportingFile("ApiFactory.mustache", ("lib/" + modulePathPart).replace('/', File.separatorChar), "ApiFactory.pm"));
106+
supportingFiles.add(new SupportingFile("Role.mustache", ("lib/" + modulePathPart).replace('/', File.separatorChar), "Role.pm"));
107+
supportingFiles.add(new SupportingFile("AutoDoc.mustache", ("lib/" + modulePathPart + "/Role").replace('/', File.separatorChar), "AutoDoc.pm"));
106108
supportingFiles.add(new SupportingFile("autodoc.script.mustache", "bin", "autodoc"));
107109
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
108110
}
@@ -129,12 +131,12 @@ public String escapeReservedWord(String name) {
129131

130132
@Override
131133
public String apiFileFolder() {
132-
return (outputFolder + "/lib/WWW/" + moduleName + apiPackage()).replace('/', File.separatorChar);
134+
return (outputFolder + "/lib/" + modulePathPart + apiPackage()).replace('/', File.separatorChar);
133135
}
134136

135137
@Override
136138
public String modelFileFolder() {
137-
return (outputFolder + "/lib/WWW/" + moduleName + modelPackage()).replace('/', File.separatorChar);
139+
return (outputFolder + "/lib/" + modulePathPart + modelPackage()).replace('/', File.separatorChar);
138140
}
139141

140142
@Override
@@ -251,6 +253,10 @@ public void setModuleName(String moduleName) {
251253
this.moduleName = moduleName;
252254
}
253255

256+
public void setModulePathPart(String modulePathPart) {
257+
this.modulePathPart = modulePathPart;
258+
}
259+
254260
public void setModuleVersion(String moduleVersion) {
255261
this.moduleVersion = moduleVersion;
256262
}

modules/swagger-codegen/src/main/resources/perl/ApiClient.mustache

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package WWW::{{moduleName}}::ApiClient;
1+
package {{moduleName}}::ApiClient;
22

33
use strict;
44
use warnings;
@@ -18,7 +18,7 @@ use Log::Any qw($log);
1818
use Carp;
1919
use Module::Runtime qw(use_module);
2020

21-
use WWW::{{moduleName}}::Configuration;
21+
use {{moduleName}}::Configuration;
2222

2323
use base 'Class::Singleton';
2424

@@ -34,7 +34,7 @@ sub _new_instance
3434
return bless \%args, $class;
3535
}
3636

37-
sub _cfg {'WWW::{{moduleName}}::Configuration'}
37+
sub _cfg {'{{moduleName}}::Configuration'}
3838

3939
# Set the user agent of the API client
4040
#
@@ -119,8 +119,8 @@ sub call_api {
119119
else {
120120
}
121121

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

125125
$log->debugf("REQUEST: %s", $_request->as_string);
126126
my $_response = $self->{ua}->request($_request);
@@ -243,7 +243,7 @@ sub deserialize
243243
} elsif (grep /^$class$/, ('string', 'int', 'float', 'bool', 'object')) {
244244
return $data;
245245
} else { # model
246-
my $_instance = use_module("WWW::{{moduleName}}::Object::$class")->new;
246+
my $_instance = use_module("{{moduleName}}::Object::$class")->new;
247247
if (ref $data eq "HASH") {
248248
return $_instance->from_hash($data);
249249
} else { # string, need to json decode first
@@ -294,11 +294,11 @@ sub get_api_key_with_prefix
294294
{
295295
my ($self, $key_name) = @_;
296296
297-
my $api_key = $WWW::{{moduleName}}::Configuration::api_key->{$key_name};
297+
my $api_key = ${{moduleName}}::Configuration::api_key->{$key_name};
298298

299299
return unless $api_key;
300300

301-
my $prefix = $WWW::{{moduleName}}::Configuration::api_key_prefix->{$key_name};
301+
my $prefix = ${{moduleName}}::Configuration::api_key_prefix->{$key_name};
302302
return $prefix ? "$prefix $api_key" : $api_key;
303303
}
304304

@@ -329,11 +329,11 @@ sub update_params_for_auth {
329329
if ($api_key) {
330330
$query_params->{'{{keyParamName}}'} = $api_key;
331331
}{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}
332-
if ($WWW::{{moduleName}}::Configuration::username || $WWW::{{moduleName}}::Configuration::password) {
333-
$header_params->{'Authorization'} = 'Basic ' . encode_base64($WWW::{{moduleName}}::Configuration::username . ":" . $WWW::{{moduleName}}::Configuration::password);
332+
if (${{moduleName}}::Configuration::username || ${{moduleName}}::Configuration::password) {
333+
$header_params->{'Authorization'} = 'Basic ' . encode_base64(${{moduleName}}::Configuration::username . ":" . ${{moduleName}}::Configuration::password);
334334
}{{/isBasic}}{{#isOAuth}}
335-
if ($WWW::{{moduleName}}::Configuration::access_token) {
336-
$header_params->{'Authorization'} = 'Bearer ' . $WWW::{{moduleName}}::Configuration::access_token;
335+
if (${{moduleName}}::Configuration::access_token) {
336+
$header_params->{'Authorization'} = 'Bearer ' . ${{moduleName}}::Configuration::access_token;
337337
}{{/isOAuth}}
338338
}
339339
{{/authMethods}}

modules/swagger-codegen/src/main/resources/perl/ApiFactory.mustache

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package WWW::{{moduleName}}::ApiFactory;
1+
package {{moduleName}}::ApiFactory;
22

33
use strict;
44
use warnings;
@@ -7,26 +7,26 @@ use utf8;
77
use Carp;
88
use Module::Find;
99

10-
usesub WWW::{{moduleName}}::Object;
10+
usesub {{moduleName}}::Object;
1111

12-
use WWW::{{moduleName}}::ApiClient;
12+
use {{moduleName}}::ApiClient;
1313

1414
=head1 Name
1515

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

1818
=head1 Synopsis
1919

2020
package My::Petstore::App;
2121

22-
use WWW::{{moduleName}}::ApiFactory;
22+
use {{moduleName}}::ApiFactory;
2323

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

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

29-
# $pet_api isa WWW::{{moduleName}}::PetApi
29+
# $pet_api isa {{moduleName}}::PetApi
3030

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

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

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

4646
=head1 new()
4747

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

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

@@ -64,7 +64,7 @@ sub new {
6464

6565
$which is a nickname for the class:
6666

67-
WWW::FooBarClient::BazApi has nickname 'Baz'
67+
FooBarClient::BazApi has nickname 'Baz'
6868

6969
=cut
7070

modules/swagger-codegen/src/main/resources/perl/AutoDoc.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package WWW::{{moduleName}}::Role::AutoDoc;
1+
package {{moduleName}}::Role::AutoDoc;
22
use List::MoreUtils qw(uniq);
33

44
use Moose::Role;
@@ -40,7 +40,7 @@ sub _printisa {
4040

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

425425

426426

427-
1;
427+
1;

modules/swagger-codegen/src/main/resources/perl/BaseObject.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ sub _deserialize {
6868
} elsif ( grep( /^$type$/, ('int', 'double', 'string', 'boolean'))) {
6969
return $data;
7070
} else { # hash(model)
71-
my $_instance = eval "WWW::{{moduleName}}::Object::$type->new()";
71+
my $_instance = eval "{{moduleName}}::Object::$type->new()";
7272
return $_instance->from_hash($data);
7373
}
7474
}

modules/swagger-codegen/src/main/resources/perl/Configuration.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package WWW::{{moduleName}}::Configuration;
1+
package {{moduleName}}::Configuration;
22

33
use strict;
44
use warnings;

modules/swagger-codegen/src/main/resources/perl/README.mustache

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# NAME
22

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

55
## {{appName}} version: {{appVersion}}
66

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

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

2020
# SYNOPSIS
2121

@@ -29,7 +29,7 @@ role.
2929

3030
package MyApp;
3131
use Moose;
32-
with 'WWW::{{moduleName}}::Role';
32+
with '{{moduleName}}::Role';
3333

3434
package main;
3535

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

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

94-
- `$WWW::{{moduleName}}::Configuration::username`
94+
- `${{moduleName}}::Configuration::username`
9595

9696
String. The username for basic auth.
9797

98-
- `$WWW::{{moduleName}}::Configuration::password`
98+
- `${{moduleName}}::Configuration::password`
9999

100100
String. The password for basic auth.
101101

102-
- `$WWW::{{moduleName}}::Configuration::api_key`
102+
- `${{moduleName}}::Configuration::api_key`
103103

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

106-
$WWW::{{moduleName}}::Configuration::api_key = {
106+
${{moduleName}}::Configuration::api_key = {
107107
secretKey => 'aaaabbbbccccdddd',
108108
anotherKey => '1111222233334444',
109109
};
110110

111-
- `$WWW::{{moduleName}}::Configuration::api_key_prefix`
111+
- `${{moduleName}}::Configuration::api_key_prefix`
112112

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

116-
$WWW::{{moduleName}}::Configuration::api_key_prefix = {
116+
${{moduleName}}::Configuration::api_key_prefix = {
117117
secretKey => 'string',
118118
anotherKey => 'same or some other string',
119119
};
120120

121-
- `$WWW::{{moduleName}}::Configuration::access_token`
121+
- `${{moduleName}}::Configuration::access_token`
122122

123123
String. The OAuth access token.
124124

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

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

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

139139
$self->pet_api; # the same
140140

@@ -167,7 +167,7 @@ maven 3.0.3 or better already installed.
167167

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

170-
{"moduleName":"MyProjectName"}
170+
{"moduleName":"WWW::MyProjectName"}
171171

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

0 commit comments

Comments
 (0)