Skip to content
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

Excel generation and extension factorization #11

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
package io.cellstore.codegen;

import io.swagger.codegen.CodegenModelFactory;
import io.swagger.codegen.CodegenModelType;
import io.swagger.codegen.CodegenOperation;
import io.swagger.codegen.CodegenParameter;
import io.swagger.codegen.CodegenProperty;
import io.swagger.codegen.DefaultCodegen;
import io.swagger.models.Model;
import io.swagger.models.Operation;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.parameters.SerializableParameter;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.PropertyBuilder;
import io.swagger.models.properties.PropertyBuilder.PropertyId;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class CellStoreCodegen extends DefaultCodegen {
protected static final Logger LOGGER = LoggerFactory.getLogger(CellStoreCodegen.class);

public CellStoreCodegen(){
CodegenModelFactory.setTypeMapping(CodegenModelType.OPERATION, CellStoreCodegenOperation.class);
CodegenModelFactory.setTypeMapping(CodegenModelType.PARAMETER, CellStoreCodegenParameter.class);
};

public CodegenOperation fromOperation(
String path,
String httpMethod,
Operation operation,
Map<String, Model> definitions)
{
// remove excluded parameters
List<Parameter> parameters = operation.getParameters();
List<Parameter> removeParams = new ArrayList<Parameter>();
if (parameters != null) {
for (Parameter param : parameters) {
if (!includeParameter(param))
{
removeParams.add(param);
}
}
for (Parameter param : removeParams){
parameters.remove(param);
}
operation.setParameters(parameters);
}

CellStoreCodegenOperation op = (CellStoreCodegenOperation) super.fromOperation(path, httpMethod, operation, definitions);

// find patterned and hardcoded Params
List<CodegenParameter> patternQueryParams = new ArrayList<CodegenParameter>();
List<CodegenParameter> hardcodedQueryParams = new ArrayList<CodegenParameter>();
if (op.queryParams != null) {
List<CodegenParameter> removeQueryParams = new ArrayList<CodegenParameter>();
for (CodegenParameter p : op.queryParams) {
CellStoreCodegenParameter param = (CellStoreCodegenParameter) p;
if(param.getParameterKind() == CellStoreCodegenParameter.Kind.PATTERN){
removeQueryParams.add(p);
patternQueryParams.add(param.copy());
}
else if(param.getParameterKind() == CellStoreCodegenParameter.Kind.HARDCODED)
{
removeQueryParams.add(p);
hardcodedQueryParams.add(param.copy());
}
}
for (CodegenParameter p : removeQueryParams) {
op.queryParams.remove(p);
}
}
op.patternQueryParams = addHasMore(patternQueryParams);
op.hardcodedQueryParams = addHasMore(hardcodedQueryParams);

// remove hard coded params from all params
if (op.allParams != null) {
List<CodegenParameter> removeAllParams = new ArrayList<CodegenParameter>();
for (CodegenParameter p : op.allParams) {
CellStoreCodegenParameter param = (CellStoreCodegenParameter) p;
if(param.getParameterKind() == CellStoreCodegenParameter.Kind.HARDCODED)
{
removeAllParams.add(p);
}
}
for (CodegenParameter p : removeAllParams) {
op.allParams.remove(p);
}
}

return op;
};

private List<CodegenParameter> addHasMore(List<CodegenParameter> objs) {
if (objs != null) {
for (int i = 0; i < objs.size(); i++) {
if (i > 0) {
objs.get(i).secondaryParam = new Boolean(true);
}
if (i < objs.size() - 1) {
objs.get(i).hasMore = new Boolean(true);
}
}
}
return objs;
}

@Override
public CodegenParameter fromParameter(Parameter param, Set<String> imports)
{
CellStoreCodegenParameter p =
(CellStoreCodegenParameter) super.fromParameter(param, imports);
p.setDescription(this, param);
p.setParamName(this, param);
if (p.defaultValue == null)
p.defaultValue = "null";

if(p.getParameterKind() == CellStoreCodegenParameter.Kind.PATTERN
|| p.getParameterKind() == CellStoreCodegenParameter.Kind.HARDCODED)
{
SerializableParameter qp = (SerializableParameter) param;
String type = qp.getType();
Map<PropertyId, Object> args = new HashMap<PropertyId, Object>();
String format = qp.getFormat();
args.put(PropertyId.ENUM, qp.getEnum());

Property inner = PropertyBuilder.build(type, format, args);
CodegenProperty pr = fromProperty("inner", inner);
p.baseType = pr.datatype;
p.isContainer = true;
imports.add(pr.baseType);

Property property = new MapProperty(inner);
CodegenProperty model = fromProperty(qp.getName(), property);
p.dataType = model.datatype;
p.isEnum = model.isEnum;
p._enum = model._enum;
}

if(p.getParameterKind() == CellStoreCodegenParameter.Kind.PATTERN)
{
p.isPatternParam = new Boolean(true);
String pattern = (String)p.vendorExtensions.get("x-name-pattern");
p.pattern = pattern;
int pos = pattern.lastIndexOf("::");
if(pos != -1){
p.patternSuffix = pattern.substring(pos);
p.patternSuffix = p.patternSuffix.replace("$", "");
} else {
p.patternSuffix = "";
}
} else if(p.getParameterKind() == CellStoreCodegenParameter.Kind.HARDCODED)
{
p.defaultValue = (String)p.vendorExtensions.get("x-binding-value");
}
return p;
}

public boolean includeParameter(Parameter param)
{
Map<String, Object> extensions = param.getVendorExtensions();
if (extensions.size() > 0)
{
Object excludeFromBindings = extensions.get("x-exclude-from-bindings");
if (excludeFromBindings != null)
{
if (excludeFromBindings instanceof Boolean)
{
if (((Boolean)excludeFromBindings).booleanValue())
return false;
}
else
{
String msg = "Invalid value for x-exclude-from-bindings, only booleans are allowed\n";
throw new RuntimeException(msg);
}
}
}
return true;
}

@Override
public Map<String, Object> postProcessOperations(Map<String, Object> operations) {
Map<String, Object> objs = (Map<String, Object>) operations.get("operations");
List<CodegenOperation> ops = (List<CodegenOperation>) objs.get("operation");
List<CodegenOperation> removeOps = new ArrayList<CodegenOperation>();
for (CodegenOperation o : ops) {
CellStoreCodegenOperation op = (CellStoreCodegenOperation) o;
if(!op.includeOperation())
removeOps.add(o);
}
for (CodegenOperation o : removeOps) {
ops.remove(o);
}
return operations;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.cellstore.codegen;

import io.swagger.codegen.CodegenOperation;
import io.swagger.codegen.CodegenParameter;

import java.util.ArrayList;
import java.util.List;

public class CellStoreCodegenOperation extends CodegenOperation {
public List<CodegenParameter> patternQueryParams = new ArrayList<CodegenParameter>();
public List<CodegenParameter> hardcodedQueryParams = new ArrayList<CodegenParameter>();

public boolean includeOperation()
{
if (vendorExtensions.size() > 0)
{
Object excludeFromBindings = vendorExtensions.get("x-exclude-from-bindings");
if (excludeFromBindings != null)
{
if (excludeFromBindings instanceof Boolean)
{
if (((Boolean)excludeFromBindings).booleanValue())
return false;
}
else
{
String msg = "Invalid value for x-exclude-from-bindings, only booleans are allowed\n";
throw new RuntimeException(msg);
}
}
}
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package io.cellstore.codegen;

import io.swagger.codegen.CodegenParameter;
import io.swagger.codegen.DefaultCodegen;
import io.swagger.models.parameters.Parameter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

public class CellStoreCodegenParameter extends CodegenParameter {
public Boolean isPatternParam;
public String pattern, patternSuffix;
public String conversion;

/**
* Determines whether this parameter is mandatory. If the parameter is in "path",
* this property is required and its value MUST be true. Otherwise, the property
* MAY be included and its default value is false.
*/
public Boolean required;

public enum Kind
{
NORMAL,
PATTERN,
HARDCODED
}

public Kind getParameterKind()
{
if (vendorExtensions.size() > 0)
{
Object bindingName = vendorExtensions.get("x-name-pattern");
Object hardcodedValue = vendorExtensions.get("x-binding-value");
if (bindingName != null && hardcodedValue != null)
throw new RuntimeException("x-name-pattern and x-binding-value are not allowed on the same parameter");

if (bindingName == null && hardcodedValue == null)
return Kind.NORMAL;

if (bindingName != null)
return Kind.PATTERN;
else
return Kind.HARDCODED;
}
return Kind.NORMAL;
}

public void setParamName(DefaultCodegen codegen, Parameter param){
if (vendorExtensions.size() > 0)
{
Object bindingName = vendorExtensions.get("x-binding-name");
if (bindingName != null)
{
if (bindingName instanceof String)
{
this.paramName = codegen.toParamName((String)bindingName);
return;
}
else
{
String msg = "Invalid value for x-binding-name, only strings are allowed\n";
throw new RuntimeException(msg);
}
}
}
this.paramName = codegen.toParamName(param.getName());
}

public void setDescription(DefaultCodegen codegen, Parameter param){
if (vendorExtensions.size() > 0)
{
Object bindingDescription = vendorExtensions.get("x-binding-description");
if (bindingDescription != null)
{
if (bindingDescription instanceof String)
{
this.description = codegen.escapeText((String)bindingDescription);
return;
}
else
{
String msg = "Invalid value for x-binding-description, only strings are allowed\n";
throw new RuntimeException(msg);
}
}
}
this.description = codegen.escapeText(param.getDescription());
}

public String toString() {
String output = "Parameter " + this.paramName + ":\n";;
output += " isFile: " + this.isFile + "\n";
output += " notFile: " + this.notFile + "\n";
output += " hasMore: " + this.hasMore + "\n";
output += " isContainer: " + this.isContainer + "\n";
output += " secondaryParam: " + this.secondaryParam + "\n";
output += " baseName: " + this.baseName + "\n";
output += " paramName: " + this.paramName + "\n";
output += " dataType: " + this.dataType + "\n";
output += " collectionFormat: " + this.collectionFormat + "\n";
output += " description: " + this.description + "\n";
output += " baseType: " + this.baseType + "\n";
output += " isFormParam: " + this.isFormParam + "\n";
output += " isQueryParam: " + this.isQueryParam + "\n";
output += " isPathParam: " + this.isPathParam + "\n";
output += " isHeaderParam: " + this.isHeaderParam + "\n";
output += " isCookieParam: " + this.isCookieParam + "\n";
output += " isBodyParam: " + this.isBodyParam + "\n";
output += " isPatternParam: " + this.isPatternParam + "\n";
output += " required: " + this.required + "\n";
output += " jsonSchema: " + this.jsonSchema + "\n";
output += " defaultValue: " + this.defaultValue + "\n";
output += " patternSuffix: " + this.patternSuffix + "\n";
output += " pattern: " + this.pattern + "\n";
output += " isEnum: " + this.isEnum + "\n";
if (this._enum != null) {
output += " _enum: " + this._enum.toString() + "\n";
}
if (this.allowableValues != null) {
output += " allowableValues: " + this.allowableValues.toString() + "\n";
}
output += " vendorExtensions: " + this.vendorExtensions.toString() + "\n";
output += " conversion: " + this.conversion + "\n";
output += " parameterKind: " + getParameterKind() + "\n";
return output;
}

public CellStoreCodegenParameter copy() {
CellStoreCodegenParameter output = new CellStoreCodegenParameter();
super.copy(output);
output.isPatternParam = this.isPatternParam;
output.patternSuffix = this.patternSuffix;
output.pattern = this.pattern;
output.conversion = this.conversion;
return output;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public class CodegenOperation {
public List<CodegenParameter> queryParams = new ArrayList<CodegenParameter>();
public List<CodegenParameter> headerParams = new ArrayList<CodegenParameter>();
public List<CodegenParameter> formParams = new ArrayList<CodegenParameter>();
public List<CodegenParameter> patternQueryParams = new ArrayList<CodegenParameter>();
public List<CodegenParameter> hardcodedQueryParams = new ArrayList<CodegenParameter>();
public List<CodegenSecurity> authMethods;
public List<String> tags;
public List<CodegenResponse> responses = new ArrayList<CodegenResponse>();
Expand Down
Loading