update php codegen with better naming convention

This commit is contained in:
William Cheng 2015-03-20 21:56:41 +08:00
parent 92e5574ec1
commit c36e5a96fe
13 changed files with 535 additions and 57 deletions

View File

@ -18,6 +18,9 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DefaultCodegen {
Logger LOGGER = LoggerFactory.getLogger(DefaultCodegen.class);
@ -1060,4 +1063,76 @@ public class DefaultCodegen {
opList.add(co);
co.baseName = tag;
}
/* underscore and camelize are copied from Twitter elephant bird
* https://github.com/twitter/elephant-bird/blob/master/core/src/main/java/com/twitter/elephantbird/util/Strings.java
*/
/**
* Underscore the given word.
* @param word The word
* @return The underscored version of the word
*/
public static String underscore(String word) {
String firstPattern = "([A-Z]+)([A-Z][a-z])";
String secondPattern = "([a-z\\d])([A-Z])";
String replacementPattern = "$1_$2";
// Replace package separator with slash.
word = word.replaceAll("\\.", "/");
// Replace $ with two underscores for inner classes.
word = word.replaceAll("\\$", "__");
// Replace capital letter with _ plus lowercase letter.
word = word.replaceAll(firstPattern, replacementPattern);
word = word.replaceAll(secondPattern, replacementPattern);
word = word.replace('-', '_');
word = word.toLowerCase();
return word;
}
public static String camelize(String word) {
return camelize(word, false);
}
public static String camelize(String word, boolean lowercaseFirstLetter) {
// Replace all slashes with dots (package separator)
Pattern p = Pattern.compile("\\/(.?)");
Matcher m = p.matcher(word);
while (m.find()) {
word = m.replaceFirst("." + m.group(1)/*.toUpperCase()*/);
m = p.matcher(word);
}
// Uppercase the class name.
p = Pattern.compile("(\\.?)(\\w)([^\\.]*)$");
m = p.matcher(word);
if (m.find()) {
String rep = m.group(1) + m.group(2).toUpperCase() + m.group(3);
rep = rep.replaceAll("\\$", "\\\\\\$");
word = m.replaceAll(rep);
}
// Replace two underscores with $ to support inner classes.
p = Pattern.compile("(__)(.)");
m = p.matcher(word);
while (m.find()) {
word = m.replaceFirst("\\$" + m.group(2).toUpperCase());
m = p.matcher(word);
}
// Remove all underscores
p = Pattern.compile("(_)(.)");
m = p.matcher(word);
while (m.find()) {
word = m.replaceFirst(m.group(2).toUpperCase());
m = p.matcher(word);
}
if (lowercaseFirstLetter) {
word = word.substring(0, 1).toLowerCase() + word.substring(1);
}
return word;
}
}

View File

@ -38,7 +38,7 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
reservedWords = new HashSet<String> (
Arrays.asList(
"int")
"__halt_compiler", "abstract", "and", "array", "as", "break", "callable", "case", "catch", "class", "clone", "const", "continue", "declare", "default", "die", "do", "echo", "else", "elseif", "empty", "enddeclare", "endfor", "endforeach", "endif", "endswitch", "endwhile", "eval", "exit", "extends", "final", "for", "foreach", "function", "global", "goto", "if", "implements", "include", "include_once", "instanceof", "insteadof", "interface", "isset", "list", "namespace", "new", "or", "print", "private", "protected", "public", "require", "require_once", "return", "static", "switch", "throw", "trait", "try", "unset", "use", "var", "while", "xor")
);
additionalProperties.put("invokerPackage", invokerPackage);
@ -111,4 +111,42 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
public String toDefaultValue(Property p) {
return "null";
}
@Override
public String toVarName(String name) {
// parameter name starting with number won't compile
// need to escape it by appending _ at the beginning
if (name.matches("^[0-9]")) {
name = "_" + name;
}
// return the name in underscore style
// PhoneNumber => phone_number
return underscore(name);
}
@Override
public String toParamName(String name) {
// should be the same as variable name
return toVarName(name);
}
@Override
public String toModelName(String name) {
// model name cannot use reserved keyword
if(reservedWords.contains(name))
escapeReservedWord(name); // e.g. return => _return
// camelize the model name
// phone_number => PhoneNumber
return camelize(name);
}
@Override
public String toModelFilename(String name) {
// should be the same as the model name
return toModelName(name);
}
}

View File

@ -58,7 +58,7 @@ class PhpModelTest extends FlatSpec with Matchers {
vars.get(2).baseName should be ("createdAt")
vars.get(2).complexType should be (null)
vars.get(2).datatype should be ("DateTime")
vars.get(2).name should be ("createdAt")
vars.get(2).name should be ("created_at")
vars.get(2).defaultValue should be ("null")
vars.get(2).baseType should be ("DateTime")
vars.get(2).hasMore should equal (null)

View File

@ -224,12 +224,12 @@ class PetApi {
* getPetById
*
* Find pet by ID
* petId, int: ID of pet that needs to be fetched (required)
* pet_id, int: ID of pet that needs to be fetched (required)
*
* @return Pet
*/
public function getPetById($petId) {
public function getPetById($pet_id) {
// parse inputs
$resourcePath = "/pet/{petId}";
@ -244,9 +244,9 @@ class PetApi {
// path params
if($petId !== null) {
if($pet_id !== null) {
$resourcePath = str_replace("{" . "petId" . "}",
$this->apiClient->toPathValue($petId), $resourcePath);
$this->apiClient->toPathValue($pet_id), $resourcePath);
}
@ -276,14 +276,14 @@ class PetApi {
* updatePetWithForm
*
* Updates a pet in the store with form data
* petId, string: ID of pet that needs to be updated (required)
* pet_id, string: ID of pet that needs to be updated (required)
* * name, string: Updated name of the pet (required)
* * status, string: Updated status of the pet (required)
*
* @return
*/
public function updatePetWithForm($petId, $name, $status) {
public function updatePetWithForm($pet_id, $name, $status) {
// parse inputs
$resourcePath = "/pet/{petId}";
@ -298,9 +298,9 @@ class PetApi {
// path params
if($petId !== null) {
if($pet_id !== null) {
$resourcePath = str_replace("{" . "petId" . "}",
$this->apiClient->toPathValue($petId), $resourcePath);
$this->apiClient->toPathValue($pet_id), $resourcePath);
}
// form params
if ($name !== null) {
@ -331,12 +331,12 @@ class PetApi {
*
* Deletes a pet
* api_key, string: (required)
* * petId, int: Pet id to delete (required)
* * pet_id, int: Pet id to delete (required)
*
* @return
*/
public function deletePet($api_key, $petId) {
public function deletePet($api_key, $pet_id) {
// parse inputs
$resourcePath = "/pet/{petId}";
@ -354,9 +354,9 @@ class PetApi {
$headerParams['api_key'] = $this->apiClient->toHeaderValue($api_key);
}
// path params
if($petId !== null) {
if($pet_id !== null) {
$resourcePath = str_replace("{" . "petId" . "}",
$this->apiClient->toPathValue($petId), $resourcePath);
$this->apiClient->toPathValue($pet_id), $resourcePath);
}
@ -380,14 +380,14 @@ class PetApi {
* uploadFile
*
* uploads an image
* petId, int: ID of pet to update (required)
* * additionalMetadata, string: Additional data to pass to server (required)
* pet_id, int: ID of pet to update (required)
* * additional_metadata, string: Additional data to pass to server (required)
* * file, file: file to upload (required)
*
* @return
*/
public function uploadFile($petId, $additionalMetadata, $file) {
public function uploadFile($pet_id, $additional_metadata, $file) {
// parse inputs
$resourcePath = "/pet/{petId}/uploadImage";
@ -402,13 +402,13 @@ class PetApi {
// path params
if($petId !== null) {
if($pet_id !== null) {
$resourcePath = str_replace("{" . "petId" . "}",
$this->apiClient->toPathValue($petId), $resourcePath);
$this->apiClient->toPathValue($pet_id), $resourcePath);
}
// form params
if ($additionalMetadata !== null) {
$formParams['additionalMetadata'] = $this->apiClient->toFormValue($additionalMetadata);
if ($additional_metadata !== null) {
$formParams['additionalMetadata'] = $this->apiClient->toFormValue($additional_metadata);
}// form params
if ($file !== null) {
$formParams['file'] = '@' . $this->apiClient->toFormValue($file);

View File

@ -129,12 +129,12 @@ class StoreApi {
* getOrderById
*
* Find purchase order by ID
* orderId, string: ID of pet that needs to be fetched (required)
* order_id, string: ID of pet that needs to be fetched (required)
*
* @return Order
*/
public function getOrderById($orderId) {
public function getOrderById($order_id) {
// parse inputs
$resourcePath = "/store/order/{orderId}";
@ -149,9 +149,9 @@ class StoreApi {
// path params
if($orderId !== null) {
if($order_id !== null) {
$resourcePath = str_replace("{" . "orderId" . "}",
$this->apiClient->toPathValue($orderId), $resourcePath);
$this->apiClient->toPathValue($order_id), $resourcePath);
}
@ -181,12 +181,12 @@ class StoreApi {
* deleteOrder
*
* Delete purchase order by ID
* orderId, string: ID of the order that needs to be deleted (required)
* order_id, string: ID of the order that needs to be deleted (required)
*
* @return
*/
public function deleteOrder($orderId) {
public function deleteOrder($order_id) {
// parse inputs
$resourcePath = "/store/order/{orderId}";
@ -201,9 +201,9 @@ class StoreApi {
// path params
if($orderId !== null) {
if($order_id !== null) {
$resourcePath = str_replace("{" . "orderId" . "}",
$this->apiClient->toPathValue($orderId), $resourcePath);
$this->apiClient->toPathValue($order_id), $resourcePath);
}

View File

@ -25,27 +25,27 @@
class Order implements ArrayAccess {
static $swaggerTypes = array(
'id' => 'int',
'petId' => 'int',
'pet_id' => 'int',
'quantity' => 'int',
'shipDate' => 'DateTime',
'ship_date' => 'DateTime',
'status' => 'string',
'complete' => 'boolean'
);
static $attributeMap = array(
'id' => 'id',
'petId' => 'petId',
'pet_id' => 'petId',
'quantity' => 'quantity',
'shipDate' => 'shipDate',
'ship_date' => 'shipDate',
'status' => 'status',
'complete' => 'complete'
);
public $id; /* int */
public $petId; /* int */
public $pet_id; /* int */
public $quantity; /* int */
public $shipDate; /* DateTime */
public $ship_date; /* DateTime */
/**
* Order Status
*/
@ -54,9 +54,9 @@ class Order implements ArrayAccess {
public function __construct(array $data) {
$this->id = $data["id"];
$this->petId = $data["petId"];
$this->pet_id = $data["pet_id"];
$this->quantity = $data["quantity"];
$this->shipDate = $data["shipDate"];
$this->ship_date = $data["ship_date"];
$this->status = $data["status"];
$this->complete = $data["complete"];
}

View File

@ -27,7 +27,7 @@ class Pet implements ArrayAccess {
'id' => 'int',
'category' => 'Category',
'name' => 'string',
'photoUrls' => 'array[string]',
'photo_urls' => 'array[string]',
'tags' => 'array[Tag]',
'status' => 'string'
);
@ -36,7 +36,7 @@ class Pet implements ArrayAccess {
'id' => 'id',
'category' => 'category',
'name' => 'name',
'photoUrls' => 'photoUrls',
'photo_urls' => 'photoUrls',
'tags' => 'tags',
'status' => 'status'
);
@ -45,7 +45,7 @@ class Pet implements ArrayAccess {
public $id; /* int */
public $category; /* Category */
public $name; /* string */
public $photoUrls; /* array[string] */
public $photo_urls; /* array[string] */
public $tags; /* array[Tag] */
/**
* pet status in the store
@ -56,7 +56,7 @@ class Pet implements ArrayAccess {
$this->id = $data["id"];
$this->category = $data["category"];
$this->name = $data["name"];
$this->photoUrls = $data["photoUrls"];
$this->photo_urls = $data["photo_urls"];
$this->tags = $data["tags"];
$this->status = $data["status"];
}

View File

@ -26,47 +26,47 @@ class User implements ArrayAccess {
static $swaggerTypes = array(
'id' => 'int',
'username' => 'string',
'firstName' => 'string',
'lastName' => 'string',
'first_name' => 'string',
'last_name' => 'string',
'email' => 'string',
'password' => 'string',
'phone' => 'string',
'userStatus' => 'int'
'user_status' => 'int'
);
static $attributeMap = array(
'id' => 'id',
'username' => 'username',
'firstName' => 'firstName',
'lastName' => 'lastName',
'first_name' => 'firstName',
'last_name' => 'lastName',
'email' => 'email',
'password' => 'password',
'phone' => 'phone',
'userStatus' => 'userStatus'
'user_status' => 'userStatus'
);
public $id; /* int */
public $username; /* string */
public $firstName; /* string */
public $lastName; /* string */
public $first_name; /* string */
public $last_name; /* string */
public $email; /* string */
public $password; /* string */
public $phone; /* string */
/**
* User Status
*/
public $userStatus; /* int */
public $user_status; /* int */
public function __construct(array $data) {
$this->id = $data["id"];
$this->username = $data["username"];
$this->firstName = $data["firstName"];
$this->lastName = $data["lastName"];
$this->first_name = $data["first_name"];
$this->last_name = $data["last_name"];
$this->email = $data["email"];
$this->password = $data["password"];
$this->phone = $data["phone"];
$this->userStatus = $data["userStatus"];
$this->user_status = $data["user_status"];
}
public function offsetExists($offset) {

View File

@ -0,0 +1,60 @@
<?php
/**
* Copyright 2015 Reverb Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* $model.description$
*
* NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
*
*/
class category implements ArrayAccess {
static $swaggerTypes = array(
'id' => 'int',
'name' => 'string'
);
static $attributeMap = array(
'id' => 'id',
'name' => 'name'
);
public $id; /* int */
public $name; /* string */
public function __construct(array $data) {
$this->id = $data["id"];
$this->name = $data["name"];
}
public function offsetExists($offset) {
return isset($this->$offset);
}
public function offsetGet($offset) {
return $this->$offset;
}
public function offsetSet($offset, $value) {
$this->$offset = $value;
}
public function offsetUnset($offset) {
unset($this->$offset);
}
}

View File

@ -0,0 +1,79 @@
<?php
/**
* Copyright 2015 Reverb Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* $model.description$
*
* NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
*
*/
class order implements ArrayAccess {
static $swaggerTypes = array(
'id' => 'int',
'pet_id' => 'int',
'quantity' => 'int',
'ship_date' => 'DateTime',
'status' => 'string',
'complete' => 'boolean'
);
static $attributeMap = array(
'id' => 'id',
'pet_id' => 'petId',
'quantity' => 'quantity',
'ship_date' => 'shipDate',
'status' => 'status',
'complete' => 'complete'
);
public $id; /* int */
public $pet_id; /* int */
public $quantity; /* int */
public $ship_date; /* DateTime */
/**
* Order Status
*/
public $status; /* string */
public $complete; /* boolean */
public function __construct(array $data) {
$this->id = $data["id"];
$this->pet_id = $data["pet_id"];
$this->quantity = $data["quantity"];
$this->ship_date = $data["ship_date"];
$this->status = $data["status"];
$this->complete = $data["complete"];
}
public function offsetExists($offset) {
return isset($this->$offset);
}
public function offsetGet($offset) {
return $this->$offset;
}
public function offsetSet($offset, $value) {
$this->$offset = $value;
}
public function offsetUnset($offset) {
unset($this->$offset);
}
}

View File

@ -0,0 +1,79 @@
<?php
/**
* Copyright 2015 Reverb Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* $model.description$
*
* NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
*
*/
class pet implements ArrayAccess {
static $swaggerTypes = array(
'id' => 'int',
'category' => 'Category',
'name' => 'string',
'photo_urls' => 'array[string]',
'tags' => 'array[Tag]',
'status' => 'string'
);
static $attributeMap = array(
'id' => 'id',
'category' => 'category',
'name' => 'name',
'photo_urls' => 'photoUrls',
'tags' => 'tags',
'status' => 'status'
);
public $id; /* int */
public $category; /* Category */
public $name; /* string */
public $photo_urls; /* array[string] */
public $tags; /* array[Tag] */
/**
* pet status in the store
*/
public $status; /* string */
public function __construct(array $data) {
$this->id = $data["id"];
$this->category = $data["category"];
$this->name = $data["name"];
$this->photo_urls = $data["photo_urls"];
$this->tags = $data["tags"];
$this->status = $data["status"];
}
public function offsetExists($offset) {
return isset($this->$offset);
}
public function offsetGet($offset) {
return $this->$offset;
}
public function offsetSet($offset, $value) {
$this->$offset = $value;
}
public function offsetUnset($offset) {
unset($this->$offset);
}
}

View File

@ -0,0 +1,60 @@
<?php
/**
* Copyright 2015 Reverb Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* $model.description$
*
* NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
*
*/
class tag implements ArrayAccess {
static $swaggerTypes = array(
'id' => 'int',
'name' => 'string'
);
static $attributeMap = array(
'id' => 'id',
'name' => 'name'
);
public $id; /* int */
public $name; /* string */
public function __construct(array $data) {
$this->id = $data["id"];
$this->name = $data["name"];
}
public function offsetExists($offset) {
return isset($this->$offset);
}
public function offsetGet($offset) {
return $this->$offset;
}
public function offsetSet($offset, $value) {
$this->$offset = $value;
}
public function offsetUnset($offset) {
unset($this->$offset);
}
}

View File

@ -0,0 +1,87 @@
<?php
/**
* Copyright 2015 Reverb Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* $model.description$
*
* NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually.
*
*/
class user implements ArrayAccess {
static $swaggerTypes = array(
'id' => 'int',
'username' => 'string',
'first_name' => 'string',
'last_name' => 'string',
'email' => 'string',
'password' => 'string',
'phone' => 'string',
'user_status' => 'int'
);
static $attributeMap = array(
'id' => 'id',
'username' => 'username',
'first_name' => 'firstName',
'last_name' => 'lastName',
'email' => 'email',
'password' => 'password',
'phone' => 'phone',
'user_status' => 'userStatus'
);
public $id; /* int */
public $username; /* string */
public $first_name; /* string */
public $last_name; /* string */
public $email; /* string */
public $password; /* string */
public $phone; /* string */
/**
* User Status
*/
public $user_status; /* int */
public function __construct(array $data) {
$this->id = $data["id"];
$this->username = $data["username"];
$this->first_name = $data["first_name"];
$this->last_name = $data["last_name"];
$this->email = $data["email"];
$this->password = $data["password"];
$this->phone = $data["phone"];
$this->user_status = $data["user_status"];
}
public function offsetExists($offset) {
return isset($this->$offset);
}
public function offsetGet($offset) {
return $this->$offset;
}
public function offsetSet($offset, $value) {
$this->$offset = $value;
}
public function offsetUnset($offset) {
unset($this->$offset);
}
}