Skip to content
Open
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
72 changes: 66 additions & 6 deletions src/main/java/com/chargebee/sdk/php/v4/Common.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.chargebee.sdk.php.v4;

import static com.chargebee.GenUtil.singularize;
import static com.chargebee.GenUtil.toCamelCase;
import static com.chargebee.GenUtil.*;
import static com.chargebee.openapi.Extension.SDK_ENUM_API_NAME;
import static com.chargebee.openapi.Extension.SUB_RESOURCE_NAME;
import static com.chargebee.openapi.Resource.*;
Expand All @@ -16,9 +15,14 @@
public class Common {

public static final String CHARGEBEE_ENUMS_BASE_PATH = "\\Chargebee\\Enums\\";
public static final String CHARGEBEE_CLASSBASED_ENUMS_BASE_PATH =
"\\Chargebee\\ClassBasedEnums\\";

public static final String CHARGEBEE_RESOURCES_BASE_PATH = "\\Chargebee\\Resources\\";
public static final String ENUMS_DIRECTORY = "Enums";

public static final String CLASS_BASED_ENUMS_DIRECTORY = "ClassBasedEnums";

public static String dataType(Schema schema) {
if (schema instanceof StringSchema
|| schema instanceof EmailSchema
Expand Down Expand Up @@ -159,6 +163,55 @@ public static Column listOfEnumParser(Attribute attribute) {
attribute, CHARGEBEE_ENUMS_BASE_PATH, String.format("%sType", singularize(type)));
}

public static Column globalClassBasedEnumParser(Attribute attribute) {
return createEnumColumn(attribute, CHARGEBEE_CLASSBASED_ENUMS_BASE_PATH);
}

public static Column localClassBasedEnumParser(
Attribute attribute, Resource res, Attribute subResourceAttribute) {
String type;
String resourceName = res.name;

if (subResourceAttribute.isDependentAttribute() || attribute.isDependentAttribute()) {
type = toCamelCase(res.name) + toCamelCase(attribute.name);
} else {
if (attribute.isExternalEnum()) {
if (attribute.getEnumApiName() == null
|| attribute.getEnumApiName().equalsIgnoreCase(attribute.name)) {
type = toCamelCase(attribute.name);
} else {
type = firstCharLower(attribute.getEnumApiName());
resourceName = attribute.getEnumApiName();
type =
type.contains(".")
? type.substring(type.lastIndexOf('.') + 1)
: toCamelCase(attribute.name);
resourceName =
resourceName.contains(".")
? resourceName.substring(0, resourceName.lastIndexOf('.'))
: "";
}
} else {
type = subResourceName(subResourceAttribute, attribute);
}
}
String basePath;
// When an enum is marked as external but has no external reference in the OpenAPI specs,
// it is treated as a global enum.
if (resourceName.equals("")) {
basePath = CHARGEBEE_CLASSBASED_ENUMS_BASE_PATH;
} else {
basePath =
CHARGEBEE_RESOURCES_BASE_PATH
+ resourceName
+ BACK_SLASH
+ CLASS_BASED_ENUMS_DIRECTORY
+ BACK_SLASH;
}

return createEnumColumn(attribute, basePath, type);
}

public static Column localEnumParser(Attribute attribute, Resource res) {
return createEnumColumn(
attribute,
Expand All @@ -179,12 +232,12 @@ private static Column createEnumColumn(Attribute attribute, String basePath) {
return column;
}

private static Column createEnumColumn(
Attribute attribute, String basePath, String fieldTypePHP) {
private static Column createEnumColumn(Attribute attribute, String basePath, String type) {
String enumType = basePath + type;
Column column = new Column();
column.setName(attribute.name);
column.setFieldTypePHP(basePath + fieldTypePHP);
column.setPhpDocField(basePath + fieldTypePHP);
column.setFieldTypePHP(enumType);
column.setPhpDocField(enumType);
column.setIsOptional(true);
column.setApiName(attribute.name);
return column;
Expand All @@ -196,4 +249,11 @@ public static String getGlobalResourceReferenceName(Schema schema) {
+ BACK_SLASH
+ subResourceName(schema);
}

private static String subResourceName(Attribute subResourceAttribute, Attribute attribute) {
if (subResourceAttribute.subResourceName() != null) {
return subResourceAttribute.subResourceName() + toCamelCase(attribute.name);
}
return singularize(toCamelCase(subResourceAttribute.name)) + toCamelCase(attribute.name);
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/chargebee/sdk/php/v4/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class Constants {
public static final String RESOURCES = "resources";
public static final String LIST_RESPONSE_OBJECT = "listResponseObject";
public static final String CLASS_BASED_ENUM = "ClassBasedEnums";
public static final String PHP_FILE_NAME_EXTENSION = ".php";
public static final String RESPONSE = "response";
public static final String API_DOCS_URL = "https://apidocs.chargebee.com/docs/api/";
Expand Down Expand Up @@ -43,5 +44,6 @@ public class Constants {
public static final String RESOURCES_NAME_LIST = "resourcesNameList";
public static final String SEMICOLON = ";";
public static final String CHARGEBEE_ENUMS = "Chargebee\\Enums";
public static final String CHARGEBEE_CLASS_BASED_ENUMS = "Chargebee\\ClassBasedEnums";
public static final String PASCAL_CASE_ENUMS = "Enums";
}
13 changes: 11 additions & 2 deletions src/main/java/com/chargebee/sdk/php/v4/PHP_V4.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ protected Map<String, String> templatesDefinition() {
"/templates/php/v4/listResponse.php.hbs",
CHARGEBEE_CLIENT,
"/templates/php/v4/chargebeeClient.php.hbs",
CLASS_BASED_ENUM,
"/templates/php/v4/classBasedEnum.php.hbs",
LIST_RESPONSE_OBJECT,
"/templates/php/v4/listResponseObject.php.hbs",
ACTION_CONTRACT,
Expand All @@ -58,7 +60,9 @@ private Map<String, FileGenerator> initializeGenerators() {
"client",
new ClientFileGenerator(this),
"actionContract",
new ActionContractFileGenerator(this));
new ActionContractFileGenerator(this),
"classBasedEnums",
new ClassBasedEnumFileGenerator(this));
}

@Override
Expand All @@ -74,6 +78,10 @@ protected List<FileOp> generateSDK(String outputPath, Spec spec) throws IOExcept
fileOps.addAll(
generators.get("response").generate(outputPath + "/Responses", filteredResources));
fileOps.addAll(generators.get("enum").generate(outputPath + "/Enums", spec.globalEnums()));
fileOps.addAll(
generators
.get("classBasedEnums")
.generate(outputPath + "/ClassBasedEnums", spec.globalEnums()));
fileOps.add(generators.get("client").generateSingle(outputPath, filteredResources));
fileOps.addAll(
generators
Expand Down Expand Up @@ -104,7 +112,8 @@ private List<FileOp> createBaseDirectories(String basePath) {
new FileOp.CreateDirectory(basePath, "/Enums"),
new FileOp.CreateDirectory(basePath, "/Actions"),
new FileOp.CreateDirectory(basePath, "/Responses"),
new FileOp.CreateDirectory(basePath, "/Actions/Contracts"));
new FileOp.CreateDirectory(basePath, "/Actions/Contracts"),
new FileOp.CreateDirectory(basePath, "/ClassBasedEnums"));
}

private List<com.chargebee.openapi.Resource> filterResources(
Expand Down
33 changes: 30 additions & 3 deletions src/main/java/com/chargebee/sdk/php/v4/SubResourceParser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.chargebee.sdk.php.v4;

import static com.chargebee.GenUtil.*;
import static com.chargebee.sdk.php.v4.Common.localClassBasedEnumParser;

import com.chargebee.openapi.Attribute;
import com.chargebee.openapi.Resource;
Expand All @@ -16,13 +17,13 @@ public static List<com.chargebee.sdk.php.v4.models.Resource> listSubResources(
return new ResourceAssist()
.setResource(activeResource).subResource().stream()
.filter(SubResourceParser::isValidSubResource)
.map(SubResourceParser::createSubResource)
.map(attribute -> createSubResource(attribute, activeResource))
.collect(Collectors.toList());
}

public static List<Column> getSubResourceCols(Attribute subResourceAttribute) {
return subResourceAttribute.attributes().stream()
.filter(Attribute::isNotHiddenAttribute)
.filter(attribute -> attribute.isNotHiddenAttribute() && !attribute.isEnumAttribute())
.map(Common::columnParser)
.collect(Collectors.toList());
}
Expand All @@ -33,11 +34,14 @@ private static boolean isValidSubResource(Attribute attribute) {
&& !Resource.isGlobalResourceReference(attribute.schema);
}

private static com.chargebee.sdk.php.v4.models.Resource createSubResource(Attribute attribute) {
private static com.chargebee.sdk.php.v4.models.Resource createSubResource(
Attribute attribute, Resource activeResource) {
com.chargebee.sdk.php.v4.models.Resource subResource =
new com.chargebee.sdk.php.v4.models.Resource();
subResource.setClazName(determineClassName(attribute));
subResource.setCols(getSubResourceCols(attribute));
subResource.setGlobalEnumCols(generateGlobalClassBasedEnumColumn(attribute));
subResource.setLocalEnumCols(generateLocalClassBasedEnumColumn(attribute, activeResource));
return subResource;
}

Expand All @@ -46,4 +50,27 @@ private static String determineClassName(Attribute attribute) {
? singularize(toClazName(attribute.name))
: attribute.subResourceName();
}

public static List<Column> generateGlobalClassBasedEnumColumn(Attribute subResourceAttribute) {
return subResourceAttribute.attributes().stream()
.filter(
attribute ->
attribute.isNotHiddenAttribute()
&& attribute.isEnumAttribute()
&& (attribute.isGlobalEnumAttribute() || attribute.isGenSeparate()))
.map(Common::globalClassBasedEnumParser)
.collect(Collectors.toList());
}

public static List<Column> generateLocalClassBasedEnumColumn(
Attribute subResourceAttribute, Resource res) {
return subResourceAttribute.attributes().stream()
.filter(
attribute ->
attribute.isNotHiddenAttribute()
&& attribute.isEnumAttribute()
&& !(attribute.isGlobalEnumAttribute() || attribute.isGenSeparate()))
.map(attribute -> localClassBasedEnumParser(attribute, res, subResourceAttribute))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.chargebee.sdk.php.v4.generators;

import static com.chargebee.GenUtil.firstCharUpper;
import static com.chargebee.GenUtil.toCamelCase;
import static com.chargebee.sdk.php.v4.Constants.*;

import com.chargebee.openapi.Enum;
import com.chargebee.sdk.FileOp;
import com.chargebee.sdk.php.v4.PHP_V4;
import com.github.jknack.handlebars.Template;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

public class ClassBasedEnumFileGenerator implements FileGenerator {

private final PHP_V4 phpGenerator;
private final Template enumTemplate;

public ClassBasedEnumFileGenerator(PHP_V4 phpGenerator) {
this.phpGenerator = phpGenerator;
this.enumTemplate = phpGenerator.getTemplateContent(CLASS_BASED_ENUM);
}

@Override
public List<FileOp> generate(String outputPath, List<?> items) throws IOException {
List<Enum> enums = (List<Enum>) items;
List<FileOp> fileOps = new ArrayList<>();
enums = enums.stream().sorted(Comparator.comparing(e -> e.name)).collect(Collectors.toList());
for (Enum enumObj : enums) {
Map<String, Object> enumMap = createEnumMap(enumObj, CHARGEBEE_CLASS_BASED_ENUMS);
String content = enumTemplate.apply(enumMap);
String fileName = enumObj.name + PHP_FILE_NAME_EXTENSION;
fileOps.add(new FileOp.WriteString(outputPath, fileName, content));
}

return fileOps;
}

private Map<String, Object> createEnumMap(Enum enumObj, String namespace) {
return Map.of(
"name",
firstCharUpper(toCamelCase(enumObj.name)),
"possibleValues",
enumObj.validValues().stream()
.map(value -> Map.of("name", value))
.collect(Collectors.toList()),
"deprecatedEnums",
!enumObj.deprecatedValues().isEmpty()
? enumObj.deprecatedValues().stream()
.map(value -> Map.of("name", value))
.collect(Collectors.toList())
: new ArrayList<>(),
"IsParamBlankOption",
enumObj.isParamBlankOption(),
"namespace",
namespace);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ public class ResourceFileGenerator implements FileGenerator {
private final PHP_V4 phpGenerator;
private final Template resourceTemplate;
private final Template enumTemplate;
private final Template classBasedEnumTemplate;

public ResourceFileGenerator(PHP_V4 phpGenerator) {
this.phpGenerator = phpGenerator;
this.resourceTemplate = phpGenerator.getTemplateContent(Constants.RESOURCES);
this.enumTemplate = phpGenerator.getTemplateContent(ENUMS);
this.classBasedEnumTemplate = phpGenerator.getTemplateContent(CLASS_BASED_ENUM);
}

@Override
Expand All @@ -34,15 +36,15 @@ public List<FileOp> generate(String outputPath, List<?> items) throws IOExceptio
List<FileOp> fileOps = new ArrayList<>();

for (Resource resource : resources) {
fileOps.addAll(generateResourceFiles(outputPath, resource));
fileOps.addAll(generateResourceFiles(outputPath, resource, resources));
}

fileOps.addAll(generateContentFile(outputPath, resources));
return fileOps;
}

private List<FileOp> generateResourceFiles(String outputPath, Resource resource)
throws IOException {
private List<FileOp> generateResourceFiles(
String outputPath, Resource resource, List<Resource> resourceList) throws IOException {
List<FileOp> fileOps = new ArrayList<>();
fileOps.add(new FileOp.CreateDirectory(outputPath, resource.name));
var resourceModel = createResourceModel(resource);
Expand All @@ -55,6 +57,7 @@ private List<FileOp> generateResourceFiles(String outputPath, Resource resource)
new ResourceAssist().setResource(resource).enums();
if (!resourceEnums.isEmpty()) {
fileOps.addAll(generateEnumFiles(outputPath, resource.name, resourceEnums));
fileOps.addAll(generateClassBasedEnumFiles(outputPath, resource.name, resourceEnums));
}

return fileOps;
Expand Down Expand Up @@ -159,6 +162,35 @@ private List<FileOp> generateEnumFiles(
return fileOps;
}

private List<FileOp> generateClassBasedEnumFiles(
String outputPath, String resourceName, List<com.chargebee.openapi.Enum> enums)
throws IOException {
List<FileOp> fileOps = new ArrayList<>();
String enumDirPath =
outputPath + FORWARD_SLASH + resourceName + FORWARD_SLASH + CLASS_BASED_ENUM;
fileOps.add(
new FileOp.CreateDirectory(outputPath + FORWARD_SLASH + resourceName, CLASS_BASED_ENUM));
String namespace =
CHARGEBEE
+ BACK_SLASH
+ "Resources"
+ BACK_SLASH
+ resourceName
+ BACK_SLASH
+ CLASS_BASED_ENUM;
List<Map<String, Object>> enumMaps =
enums.stream()
.map(e -> createEnumMap(e, namespace))
.filter(map -> !map.isEmpty())
.collect(Collectors.toList());
for (Map<String, Object> enumMap : enumMaps) {
String content = classBasedEnumTemplate.apply(enumMap);
String fileName = firstCharUpper(toCamelCase(enumMap.get(NAME).toString()));
fileOps.add(new FileOp.WriteString(enumDirPath, fileName + DOT_PHP, content));
}
return fileOps;
}

private Map<String, Object> createEnumMap(com.chargebee.openapi.Enum enumObj, String namespace) {
return Map.of(
"name", firstCharUpper(toCamelCase(enumObj.name)),
Expand Down
Loading