forked from loafle/openapi-generator-original
Add Elm language - BETA (#6947)
This is the initial Codegen for Elm 0.18. Please try it out and provide feedback. Not yet supported: * path variables; * additionalProperties; * authentication; * recursive types.
This commit is contained in:
committed by
William Cheng
parent
538f28e568
commit
d6539ba3c8
@@ -49,6 +49,7 @@ public class CodegenProperty implements Cloneable {
|
||||
public Map<String, Object> vendorExtensions;
|
||||
public boolean hasValidation; // true if pattern, maximum, etc are set (only used in the mustache template)
|
||||
public boolean isInherited;
|
||||
public String discriminatorValue;
|
||||
public String nameInCamelCase; // property name in camel case
|
||||
// enum name based on the property name, usually use as a prefix (e.g. VAR_NAME) for enum name (e.g. VAR_NAME_VALUE1)
|
||||
public String enumName;
|
||||
@@ -131,6 +132,7 @@ public class CodegenProperty implements Cloneable {
|
||||
result = prime * result + ((isMapContainer ? 13:31));
|
||||
result = prime * result + ((isListContainer ? 13:31));
|
||||
result = prime * result + Objects.hashCode(isInherited);
|
||||
result = prime * result + Objects.hashCode(discriminatorValue);
|
||||
result = prime * result + Objects.hashCode(nameInCamelCase);
|
||||
result = prime * result + Objects.hashCode(enumName);
|
||||
result = prime * result + ((maxItems == null) ? 0 : maxItems.hashCode());
|
||||
@@ -312,6 +314,9 @@ public class CodegenProperty implements Cloneable {
|
||||
if (!Objects.equals(this.isInherited, other.isInherited)) {
|
||||
return false;
|
||||
}
|
||||
if (!Objects.equals(this.discriminatorValue, other.discriminatorValue)) {
|
||||
return false;
|
||||
}
|
||||
if (!Objects.equals(this.nameInCamelCase, other.nameInCamelCase)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,511 @@
|
||||
package io.swagger.codegen.languages;
|
||||
|
||||
import io.swagger.codegen.CodegenConfig;
|
||||
import io.swagger.codegen.CodegenModel;
|
||||
import io.swagger.codegen.CodegenOperation;
|
||||
import io.swagger.codegen.CodegenParameter;
|
||||
import io.swagger.codegen.CodegenProperty;
|
||||
import io.swagger.codegen.CodegenResponse;
|
||||
import io.swagger.codegen.CodegenType;
|
||||
import io.swagger.codegen.DefaultCodegen;
|
||||
import io.swagger.codegen.SupportingFile;
|
||||
import io.swagger.models.Response;
|
||||
import io.swagger.models.parameters.Parameter;
|
||||
import io.swagger.models.properties.ArrayProperty;
|
||||
import io.swagger.models.properties.BooleanProperty;
|
||||
import io.swagger.models.properties.DateProperty;
|
||||
import io.swagger.models.properties.DateTimeProperty;
|
||||
import io.swagger.models.properties.DoubleProperty;
|
||||
import io.swagger.models.properties.FloatProperty;
|
||||
import io.swagger.models.properties.IntegerProperty;
|
||||
import io.swagger.models.properties.LongProperty;
|
||||
import io.swagger.models.properties.MapProperty;
|
||||
import io.swagger.models.properties.Property;
|
||||
import io.swagger.models.properties.StringProperty;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.Collator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
private static final String X_ENCODER = "x-encoder";
|
||||
private static final String X_DECODER = "x-decoder";
|
||||
private static final String X_DISCRIMINATOR_TYPE = "x-discriminator-value";
|
||||
private static final String X_UNION_TYPE = "x-union-type";
|
||||
|
||||
private Set<String> customPrimitives = new HashSet<String>();
|
||||
|
||||
protected String packageName = "swagger";
|
||||
protected String packageVersion = "1.0.0";
|
||||
|
||||
public CodegenType getTag() {
|
||||
return CodegenType.CLIENT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "elm";
|
||||
}
|
||||
|
||||
public String getHelp() {
|
||||
return "Generates a Elm client library (beta).";
|
||||
}
|
||||
|
||||
public ElmClientCodegen() {
|
||||
super();
|
||||
outputFolder = "generated-code/elm";
|
||||
modelTemplateFiles.put("model.mustache", ".elm");
|
||||
apiTemplateFiles.put("api.mustache", ".elm");
|
||||
templateDir = "elm";
|
||||
|
||||
supportsInheritance = true;
|
||||
|
||||
reservedWords = new HashSet<>(
|
||||
Arrays.asList(
|
||||
"if", "then", "else",
|
||||
"case", "of",
|
||||
"let", "in",
|
||||
"type",
|
||||
"module", "where",
|
||||
"import", "exposing",
|
||||
"as",
|
||||
"port")
|
||||
);
|
||||
|
||||
defaultIncludes = new HashSet<>(
|
||||
Arrays.asList(
|
||||
"List")
|
||||
);
|
||||
|
||||
languageSpecificPrimitives = new HashSet<>(
|
||||
Arrays.asList(
|
||||
"Bool",
|
||||
"Dict",
|
||||
"Float",
|
||||
"Int",
|
||||
"String")
|
||||
);
|
||||
|
||||
customPrimitives = new HashSet<>(
|
||||
Arrays.asList(
|
||||
"Byte",
|
||||
"DateOnly",
|
||||
"DateTime")
|
||||
);
|
||||
|
||||
instantiationTypes.clear();
|
||||
|
||||
typeMapping.clear();
|
||||
typeMapping.put("integer", "Int");
|
||||
typeMapping.put("long", "Int");
|
||||
typeMapping.put("number", "Float");
|
||||
typeMapping.put("float", "Float");
|
||||
typeMapping.put("double", "Float");
|
||||
typeMapping.put("boolean", "Bool");
|
||||
typeMapping.put("string", "String");
|
||||
typeMapping.put("array", "List");
|
||||
typeMapping.put("date", "DateOnly");
|
||||
typeMapping.put("DateTime", "DateTime");
|
||||
typeMapping.put("password", "String");
|
||||
typeMapping.put("file", "String");
|
||||
typeMapping.put("ByteArray", "Byte");
|
||||
typeMapping.put("binary", "String");
|
||||
|
||||
importMapping.clear();
|
||||
|
||||
cliOptions.clear();
|
||||
|
||||
supportingFiles.add(new SupportingFile("Byte.mustache", "src", "Byte.elm"));
|
||||
supportingFiles.add(new SupportingFile("DateOnly.mustache", "src", "DateOnly.elm"));
|
||||
supportingFiles.add(new SupportingFile("DateTime.mustache", "src", "DateTime.elm"));
|
||||
supportingFiles.add(new SupportingFile("Main.mustache", "src", "Main.elm"));
|
||||
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
||||
supportingFiles.add(new SupportingFile("elm-package.mustache", "", "elm-package.json"));
|
||||
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String escapeUnsafeCharacters(String input) {
|
||||
return input.replace("*/", "*_/").replace("/*", "/_*");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String escapeQuotationMark(String input) {
|
||||
return input.replace("\"", "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toApiName(String name) {
|
||||
if (name.length() == 0) {
|
||||
return "Default";
|
||||
}
|
||||
return initialCaps(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toModelName(String name) {
|
||||
return camelize(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toModelFilename(String name) {
|
||||
return toModelName(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toEnumName(CodegenProperty property) {
|
||||
return toModelName(property.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toVarName(String name) {
|
||||
final String varName = camelize(name, true);
|
||||
return isReservedWord(varName) ? escapeReservedWord(name) : varName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toEnumVarName(String value, String datatype) {
|
||||
final String camelized = camelize(value.replace(" ", "_").replace("(", "_").replace(")", "")); // TODO FIXME escape properly
|
||||
if (!Character.isUpperCase(camelized.charAt(0))) {
|
||||
return "N" + camelized;
|
||||
}
|
||||
return camelized;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String escapeReservedWord(String name) {
|
||||
return name + "_";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apiFileFolder() {
|
||||
return outputFolder + "/src/Request/" + apiPackage().replace('.', File.separatorChar);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String modelFileFolder() {
|
||||
return outputFolder + "/src/Data/" + modelPackage().replace('.', File.separatorChar);
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "static-method", "unchecked" })
|
||||
public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
|
||||
// Index all CodegenModels by model name.
|
||||
Map<String, CodegenModel> allModels = new HashMap<>();
|
||||
for (Map.Entry<String, Object> entry : objs.entrySet()) {
|
||||
String modelName = toModelName(entry.getKey());
|
||||
Map<String, Object> inner = (Map<String, Object>) entry.getValue();
|
||||
List<Map<String, Object>> models = (List<Map<String, Object>>) inner.get("models");
|
||||
for (Map<String, Object> mo : models) {
|
||||
CodegenModel cm = (CodegenModel) mo.get("model");
|
||||
allModels.put(modelName, cm);
|
||||
}
|
||||
}
|
||||
// Let parent know about all its children
|
||||
for (CodegenModel cm : allModels.values()) {
|
||||
CodegenModel parent = allModels.get(cm.parent);
|
||||
|
||||
if (parent != null) {
|
||||
if (parent.children == null) {
|
||||
parent.children = new ArrayList<>();
|
||||
parent.hasChildren = true;
|
||||
}
|
||||
parent.children.add(cm);
|
||||
Collections.sort(parent.children, new Comparator<CodegenModel>() {
|
||||
@Override
|
||||
public int compare(CodegenModel cm1, CodegenModel cm2) {
|
||||
return Collator.getInstance().compare(cm1.classname, cm2.classname);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
for (Map.Entry<String, Object> entry : objs.entrySet()) {
|
||||
Map<String, Object> inner = (Map<String, Object>) entry.getValue();
|
||||
List<Map<String, Object>> models = (List<Map<String, Object>>) inner.get("models");
|
||||
for (Map<String, Object> mo : models) {
|
||||
CodegenModel cm = (CodegenModel) mo.get("model");
|
||||
if (cm.isEnum) {
|
||||
this.addEncoderAndDecoder(cm.vendorExtensions, cm.classname, false);
|
||||
cm.vendorExtensions.put(X_UNION_TYPE, cm.classname);
|
||||
} else if (cm.isAlias) {
|
||||
this.addEncoderAndDecoder(cm.vendorExtensions, cm.dataType, true);
|
||||
}
|
||||
|
||||
List<ElmImport> elmImports = new ArrayList<>();
|
||||
for (CodegenProperty property : cm.allVars) {
|
||||
if (property.complexType != null) {
|
||||
elmImports.add(createPropertyImport(property));
|
||||
}
|
||||
}
|
||||
if (cm.discriminator != null) {
|
||||
for (CodegenModel child : cm.children) {
|
||||
// add child imports
|
||||
final ElmImport elmImport = new ElmImport();
|
||||
final String modulePrefix = customPrimitives.contains(child.classname) ? "" : "Data.";
|
||||
elmImport.moduleName = modulePrefix + child.classname;
|
||||
elmImport.exposures = new TreeSet<>();
|
||||
elmImport.exposures.add(child.classname);
|
||||
elmImport.exposures.add(child.classVarName + "Decoder");
|
||||
elmImport.exposures.add(child.classVarName + "Encoder");
|
||||
elmImport.hasExposures = true;
|
||||
elmImports.add(elmImport);
|
||||
|
||||
// set discriminator value to all children (recursively)
|
||||
this.setDiscriminatorValue(child, cm.discriminator, this.getDiscriminatorValue(child));
|
||||
|
||||
// add all non-discriminator vars
|
||||
int index = 0;
|
||||
for (CodegenProperty property : cm.vars) {
|
||||
if (!cm.discriminator.equals(property.baseName)) {
|
||||
child.vars.add(index++, property);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
inner.put("elmImports", elmImports);
|
||||
}
|
||||
}
|
||||
return objs;
|
||||
}
|
||||
|
||||
private void setDiscriminatorValue(CodegenModel model, String baseName, String value) {
|
||||
for (CodegenProperty prop : model.vars) {
|
||||
if (prop.baseName.equals(baseName)) {
|
||||
prop.discriminatorValue = value;
|
||||
}
|
||||
}
|
||||
for (CodegenProperty prop : model.allVars) {
|
||||
if (prop.baseName.equals(baseName)) {
|
||||
prop.discriminatorValue = value;
|
||||
}
|
||||
}
|
||||
if (model.children != null) {
|
||||
final boolean newDiscriminator = model.discriminator != null;
|
||||
for (CodegenModel child : model.children) {
|
||||
this.setDiscriminatorValue(child, baseName, newDiscriminator ? value : this.getDiscriminatorValue(child));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String getDiscriminatorValue(CodegenModel model) {
|
||||
return model.vendorExtensions.containsKey(X_DISCRIMINATOR_TYPE) ?
|
||||
(String) model.vendorExtensions.get(X_DISCRIMINATOR_TYPE) : model.classname;
|
||||
}
|
||||
|
||||
private ElmImport createPropertyImport(final CodegenProperty property) {
|
||||
final ElmImport elmImport = new ElmImport();
|
||||
final String modulePrefix = customPrimitives.contains(property.complexType) ? "" : "Data.";
|
||||
elmImport.moduleName = modulePrefix + property.complexType;
|
||||
elmImport.exposures = new TreeSet<>();
|
||||
elmImport.exposures.add(property.complexType);
|
||||
if (property.vendorExtensions.containsKey(X_DECODER)) {
|
||||
elmImport.exposures.add((String) property.vendorExtensions.get(X_DECODER));
|
||||
}
|
||||
if (property.vendorExtensions.containsKey(X_ENCODER)) {
|
||||
elmImport.exposures.add((String) property.vendorExtensions.get(X_ENCODER));
|
||||
}
|
||||
elmImport.hasExposures = true;
|
||||
return elmImport;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
|
||||
return postProcessModelsEnum(objs);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({ "static-method", "unchecked" })
|
||||
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");
|
||||
|
||||
Map<String, Set<String>> dependencies = new HashMap<>();
|
||||
|
||||
for (CodegenOperation op : ops) {
|
||||
String path = op.path;
|
||||
for (CodegenParameter param : op.pathParams) {
|
||||
final String var = param.isString ? param.paramName : "toString " + param.paramName;
|
||||
path = path.replace("{" + param.paramName + "}", "\" ++ " + var + " ++ \"");
|
||||
}
|
||||
op.path = ("\"" + path + "\"").replaceAll(" \\+\\+ \"\"", "");
|
||||
|
||||
if (op.bodyParam != null) {
|
||||
final String encoder = (String) op.bodyParam.vendorExtensions.get(X_ENCODER);
|
||||
if (encoder != null) {
|
||||
if (!dependencies.containsKey(op.bodyParam.dataType)) {
|
||||
dependencies.put(op.bodyParam.dataType, new TreeSet<String>());
|
||||
}
|
||||
dependencies.get(op.bodyParam.dataType).add(encoder);
|
||||
}
|
||||
}
|
||||
for (CodegenResponse resp : op.responses) {
|
||||
final String decoder = (String) resp.vendorExtensions.get(X_DECODER);
|
||||
if (decoder != null) {
|
||||
if (!dependencies.containsKey(resp.dataType)) {
|
||||
dependencies.put(resp.dataType, new TreeSet<String>());
|
||||
}
|
||||
dependencies.get(resp.dataType).add(decoder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<ElmImport> elmImports = new ArrayList<>();
|
||||
for (Map.Entry<String, Set<String>> entry : dependencies.entrySet()) {
|
||||
final ElmImport elmImport = new ElmImport();
|
||||
final String key = entry.getKey();
|
||||
elmImport.moduleName = "Data." + key;
|
||||
elmImport.exposures = entry.getValue();
|
||||
elmImport.exposures.add(key);
|
||||
elmImport.hasExposures = true;
|
||||
elmImports.add(elmImport);
|
||||
}
|
||||
operations.put("elmImports", elmImports);
|
||||
|
||||
return operations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toDefaultValue(Property p) {
|
||||
if (p instanceof StringProperty) {
|
||||
StringProperty sp = (StringProperty) p;
|
||||
if (sp.getDefault() != null) {
|
||||
return toOptionalValue("\"" + sp.getDefault().toString() + "\"");
|
||||
}
|
||||
return toOptionalValue(null);
|
||||
} else if (p instanceof BooleanProperty) {
|
||||
BooleanProperty bp = (BooleanProperty) p;
|
||||
if (bp.getDefault() != null) {
|
||||
return toOptionalValue(bp.getDefault() ? "True" : "False");
|
||||
}
|
||||
return toOptionalValue(null);
|
||||
} else if (p instanceof DateProperty) {
|
||||
return toOptionalValue(null);
|
||||
} else if (p instanceof DateTimeProperty) {
|
||||
return toOptionalValue(null);
|
||||
} else if (p instanceof DoubleProperty) {
|
||||
DoubleProperty dp = (DoubleProperty) p;
|
||||
if (dp.getDefault() != null) {
|
||||
return toOptionalValue(dp.getDefault().toString());
|
||||
}
|
||||
return toOptionalValue(null);
|
||||
} else if (p instanceof FloatProperty) {
|
||||
FloatProperty fp = (FloatProperty) p;
|
||||
if (fp.getDefault() != null) {
|
||||
return toOptionalValue(fp.getDefault().toString());
|
||||
}
|
||||
return toOptionalValue(null);
|
||||
} else if (p instanceof IntegerProperty) {
|
||||
IntegerProperty ip = (IntegerProperty) p;
|
||||
if (ip.getDefault() != null) {
|
||||
return toOptionalValue(ip.getDefault().toString());
|
||||
}
|
||||
return toOptionalValue(null);
|
||||
} else if (p instanceof LongProperty) {
|
||||
LongProperty lp = (LongProperty) p;
|
||||
if (lp.getDefault() != null) {
|
||||
return toOptionalValue(lp.getDefault().toString());
|
||||
}
|
||||
return toOptionalValue(null);
|
||||
} else {
|
||||
return toOptionalValue(null);
|
||||
}
|
||||
}
|
||||
|
||||
private String toOptionalValue(String value) {
|
||||
if (value == null) {
|
||||
return "Nothing";
|
||||
}
|
||||
return "(Just " + value + ")";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSwaggerType(Property p) {
|
||||
String swaggerType = super.getSwaggerType(p);
|
||||
String type;
|
||||
if (typeMapping.containsKey(swaggerType)) {
|
||||
type = typeMapping.get(swaggerType);
|
||||
if (languageSpecificPrimitives.contains(type)) {
|
||||
return type;
|
||||
}
|
||||
} else
|
||||
type = swaggerType;
|
||||
return toModelName(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTypeDeclaration(Property p) {
|
||||
if (p instanceof ArrayProperty) {
|
||||
ArrayProperty ap = (ArrayProperty) p;
|
||||
Property inner = ap.getItems();
|
||||
return getTypeDeclaration(inner);
|
||||
} else if (p instanceof MapProperty) {
|
||||
MapProperty mp = (MapProperty) p;
|
||||
Property inner = mp.getAdditionalProperties();
|
||||
return getTypeDeclaration(inner);
|
||||
}
|
||||
return super.getTypeDeclaration(p);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodegenProperty fromProperty(String name, Property p) {
|
||||
final CodegenProperty property = super.fromProperty(name, p);
|
||||
|
||||
final String dataType = property.isEnum ? property.baseName : property.datatype;
|
||||
addEncoderAndDecoder(property.vendorExtensions, dataType, property.isPrimitiveType && !property.isEnum);
|
||||
if (property.isEnum) {
|
||||
property.vendorExtensions.put(X_UNION_TYPE, property.datatypeWithEnum);
|
||||
}
|
||||
|
||||
return property;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodegenResponse fromResponse(String responseCode, Response resp) {
|
||||
final CodegenResponse response = super.fromResponse(responseCode, resp);
|
||||
if (response.dataType != null) {
|
||||
addEncoderAndDecoder(response.vendorExtensions, response.dataType, response.primitiveType);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
|
||||
final CodegenParameter parameter = super.fromParameter(param, imports);
|
||||
addEncoderAndDecoder(parameter.vendorExtensions, parameter.dataType, parameter.isPrimitiveType);
|
||||
return parameter;
|
||||
}
|
||||
|
||||
private void addEncoderAndDecoder(Map<String, Object> vendorExtensions, String dataType, Boolean isPrimitiveType) {
|
||||
final String baseName = camelize(dataType, true);
|
||||
String encoderName;
|
||||
String decoderName;
|
||||
if (isPrimitiveType) {
|
||||
encoderName = "Encode." + baseName;
|
||||
decoderName = "Decode." + baseName;
|
||||
} else {
|
||||
encoderName = baseName + "Encoder";
|
||||
decoderName = baseName + "Decoder";
|
||||
}
|
||||
if (!vendorExtensions.containsKey(X_ENCODER)) {
|
||||
vendorExtensions.put(X_ENCODER, encoderName);
|
||||
}
|
||||
if (!vendorExtensions.containsKey(X_DECODER)) {
|
||||
vendorExtensions.put(X_DECODER, decoderName);
|
||||
}
|
||||
}
|
||||
|
||||
private static class ElmImport {
|
||||
public String moduleName;
|
||||
public String as;
|
||||
public Set<String> exposures;
|
||||
public Boolean hasExposures;
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ io.swagger.codegen.languages.CppRestClientCodegen
|
||||
io.swagger.codegen.languages.CsharpDotNet2ClientCodegen
|
||||
io.swagger.codegen.languages.DartClientCodegen
|
||||
io.swagger.codegen.languages.ElixirClientCodegen
|
||||
io.swagger.codegen.languages.ElmClientCodegen
|
||||
io.swagger.codegen.languages.EiffelClientCodegen
|
||||
io.swagger.codegen.languages.ErlangClientCodegen
|
||||
io.swagger.codegen.languages.ErlangServerCodegen
|
||||
|
||||
18
modules/swagger-codegen/src/main/resources/elm/Byte.mustache
Normal file
18
modules/swagger-codegen/src/main/resources/elm/Byte.mustache
Normal file
@@ -0,0 +1,18 @@
|
||||
module Byte exposing (Byte, byteDecoder, byteEncoder)
|
||||
|
||||
import Json.Decode as Decode exposing (Decoder)
|
||||
import Json.Encode as Encode
|
||||
|
||||
|
||||
type alias Byte = String
|
||||
|
||||
|
||||
byteDecoder : Decoder Byte
|
||||
byteDecoder =
|
||||
Decode.string
|
||||
|
||||
|
||||
byteEncoder : Byte -> Encode.Value
|
||||
byteEncoder model =
|
||||
Encode.string model
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
module DateOnly exposing (DateOnly, dateOnlyDecoder, dateOnlyEncoder)
|
||||
|
||||
import Date
|
||||
import Date.Extra exposing (fromIsoString, toFormattedString)
|
||||
import Json.Decode as Decode exposing (Decoder)
|
||||
import Json.Encode as Encode
|
||||
|
||||
|
||||
type alias DateOnly =
|
||||
Date.Date
|
||||
|
||||
|
||||
dateOnlyDecoder : Decoder DateOnly
|
||||
dateOnlyDecoder =
|
||||
Decode.string
|
||||
|> Decode.andThen decodeIsoString
|
||||
|
||||
|
||||
dateOnlyEncoder : DateOnly -> Encode.Value
|
||||
dateOnlyEncoder model =
|
||||
Encode.string <| toFormattedString "yyyy-MM-dd" model
|
||||
|
||||
|
||||
decodeIsoString : String -> Decoder DateOnly
|
||||
decodeIsoString str =
|
||||
case fromIsoString str of
|
||||
Just date ->
|
||||
Decode.succeed date
|
||||
|
||||
Nothing ->
|
||||
Decode.fail <|
|
||||
"Cannot convert "
|
||||
++ str
|
||||
++ " to DateOnly"
|
||||
@@ -0,0 +1,34 @@
|
||||
module DateTime exposing (DateTime, dateTimeDecoder, dateTimeEncoder)
|
||||
|
||||
import Date
|
||||
import Date.Extra exposing (fromIsoString, toIsoString)
|
||||
import Json.Decode as Decode exposing (Decoder)
|
||||
import Json.Encode as Encode
|
||||
|
||||
|
||||
type alias DateTime =
|
||||
Date.Date
|
||||
|
||||
|
||||
dateTimeDecoder : Decoder DateTime
|
||||
dateTimeDecoder =
|
||||
Decode.string
|
||||
|> Decode.andThen decodeIsoString
|
||||
|
||||
|
||||
dateTimeEncoder : DateTime -> Encode.Value
|
||||
dateTimeEncoder model =
|
||||
Encode.string <| toIsoString model
|
||||
|
||||
|
||||
decodeIsoString : String -> Decoder DateTime
|
||||
decodeIsoString str =
|
||||
case fromIsoString str of
|
||||
Just date ->
|
||||
Decode.succeed date
|
||||
|
||||
Nothing ->
|
||||
Decode.fail <|
|
||||
"Cannot convert "
|
||||
++ str
|
||||
++ " to DateTime"
|
||||
43
modules/swagger-codegen/src/main/resources/elm/Main.mustache
Normal file
43
modules/swagger-codegen/src/main/resources/elm/Main.mustache
Normal file
@@ -0,0 +1,43 @@
|
||||
module Main exposing (..)
|
||||
|
||||
import Json.Decode as Decode
|
||||
import Html exposing (Html, button, div, text)
|
||||
import Html.Events exposing (onClick)
|
||||
import Http
|
||||
|
||||
|
||||
main : Program Never Model Msg
|
||||
main =
|
||||
Html.program
|
||||
{ init = init
|
||||
, view = view
|
||||
, update = update
|
||||
, subscriptions = subscriptions
|
||||
}
|
||||
|
||||
type alias Model =
|
||||
{ status : Maybe Int
|
||||
}
|
||||
|
||||
init : (Model, Cmd Msg)
|
||||
init =
|
||||
( Model Nothing, Cmd.none )
|
||||
|
||||
type Msg
|
||||
= NoOp
|
||||
|
||||
update : Msg -> Model -> (Model, Cmd Msg)
|
||||
update msg model =
|
||||
case msg of
|
||||
NoOp
|
||||
( model, Cmd.none )
|
||||
|
||||
|
||||
view : Model -> Html Msg
|
||||
view model =
|
||||
Html.text "main"
|
||||
|
||||
|
||||
subscriptions : Model -> Sub Msg
|
||||
subscriptions model =
|
||||
Sub.none
|
||||
@@ -0,0 +1,18 @@
|
||||
# Elm API client
|
||||
|
||||
{{#appDescription}}
|
||||
{{{appDescription}}}
|
||||
{{/appDescription}}
|
||||
|
||||
## Overview
|
||||
This API client was generated by the [swagger-codegen](https://github.com/swagger-api/swagger-codegen) project. By using the [swagger-spec](https://github.com/swagger-api/swagger-spec) from a remote server, you can easily generate an API client.
|
||||
|
||||
- API version: {{appVersion}}
|
||||
- Package version: {{packageVersion}}
|
||||
{{^hideGenerationTimestamp}}
|
||||
- Build date: {{generatedDate}}
|
||||
{{/hideGenerationTimestamp}}
|
||||
- Build package: {{generatorClass}}
|
||||
{{#infoUrl}}
|
||||
For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})
|
||||
{{/infoUrl}}
|
||||
@@ -0,0 +1,5 @@
|
||||
{{classVarName}}Decoder : Decoder {{classname}}
|
||||
{{classVarName}}Decoder =
|
||||
decode {{classname}}
|
||||
{{#allVars}}{{^discriminatorValue}} |> {{>fieldDecoder}}
|
||||
{{/discriminatorValue}}{{/allVars}}
|
||||
@@ -0,0 +1,7 @@
|
||||
{{classVarName}}Encoder : {{classname}} -> Encode.Value
|
||||
{{classVarName}}Encoder model =
|
||||
Encode.object
|
||||
{{#allVars}}
|
||||
{{#-first}}[{{/-first}}{{^-first}},{{/-first}} {{>fieldEncoder}}
|
||||
{{/allVars}}
|
||||
]
|
||||
33
modules/swagger-codegen/src/main/resources/elm/api.mustache
Normal file
33
modules/swagger-codegen/src/main/resources/elm/api.mustache
Normal file
@@ -0,0 +1,33 @@
|
||||
{{>licenseInfo}}
|
||||
|
||||
module Request.{{classname}} exposing ({{#operations}}{{#operation}}{{^-first}}, {{/-first}}{{operationId}}{{/operation}}{{/operations}})
|
||||
|
||||
{{>imports}}import Http
|
||||
import Json.Decode as Decode
|
||||
|
||||
|
||||
basePath : String
|
||||
basePath =
|
||||
"{{basePath}}"
|
||||
|
||||
|
||||
{{#operations}}
|
||||
{{#operation}}
|
||||
{-
|
||||
{{notes}}
|
||||
-}
|
||||
{{operationId}} : {{#pathParams}}{{dataType}} -> {{/pathParams}}{{#bodyParam}}{{dataType}} -> {{/bodyParam}}Http.Request {{#responses}}{{#-first}}{{^dataType}}(){{/dataType}}{{#isListContainer}}(List {{/isListContainer}}{{dataType}}{{#isListContainer}}){{/isListContainer}}{{/-first}}{{/responses}}
|
||||
{{operationId}} {{#pathParams}}{{paramName}} {{/pathParams}}{{#bodyParam}}model {{/bodyParam}}=
|
||||
{ method = "{{httpMethod}}"
|
||||
, url = basePath ++ {{{path}}}
|
||||
, headers = []
|
||||
, body = {{#bodyParam}}Http.jsonBody <| {{vendorExtensions.x-encoder}} model{{/bodyParam}}{{^bodyParam}}Http.emptyBody{{/bodyParam}}
|
||||
, expect = {{#responses}}{{#-first}}{{^dataType}}Http.expectStringResponse (\_ -> Ok ()){{/dataType}}{{#dataType}}Http.expectJson {{#isListContainer}}(Decode.list {{/isListContainer}}{{#vendorExtensions}}{{x-decoder}}{{/vendorExtensions}}{{#isListContainer}}){{/isListContainer}}{{/dataType}}{{/-first}}{{/responses}}
|
||||
, timeout = Just 30000
|
||||
, withCredentials = False
|
||||
}
|
||||
|> Http.request
|
||||
|
||||
|
||||
{{/operation}}
|
||||
{{/operations}}
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"version": "1.0.0",
|
||||
"summary": "helpful summary of your project, less than 80 characters",
|
||||
"repository": "https://github.com/user/project.git",
|
||||
"license": "BSD3",
|
||||
"source-directories": [
|
||||
"src"
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
"NoRedInk/elm-decode-pipeline": "3.0.0 <= v < 4.0.0",
|
||||
"elm-lang/core": "5.1.1 <= v < 6.0.0",
|
||||
"elm-lang/html": "2.0.0 <= v < 3.0.0",
|
||||
"elm-lang/http": "1.0.0 <= v < 2.0.0",
|
||||
"justinmimbs/elm-date-extra": "2.0.3 <= v < 3.0.0"
|
||||
},
|
||||
"elm-version": "0.18.0 <= v < 0.19.0"
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{{#required}}required{{/required}}{{^required}}optional{{/required}} "{{baseName}}" {{^required}}(Decode.nullable {{/required}}{{#isContainer}}(Decode.list {{/isContainer}}{{vendorExtensions.x-decoder}}{{#isContainer}}){{/isContainer}}{{^required}}){{/required}}{{^required}} {{{defaultValue}}}{{/required}}
|
||||
@@ -0,0 +1 @@
|
||||
( "{{baseName}}", {{#discriminatorValue}}Encode.string "{{discriminatorValue}}"{{/discriminatorValue}}{{^discriminatorValue}}{{^required}}withDefault Encode.null (map {{/required}}{{#isContainer}}(Encode.list << List.map {{/isContainer}}{{vendorExtensions.x-encoder}}{{#isContainer}}){{/isContainer}} model.{{name}}{{^required}}){{/required}}{{/discriminatorValue}} )
|
||||
@@ -0,0 +1 @@
|
||||
/elm-stuff
|
||||
@@ -0,0 +1,3 @@
|
||||
{{#elmImports}}
|
||||
import {{moduleName}}{{#as}} as {{as}}{{/as}}{{#hasExposures}} exposing ({{#exposures}}{{^-first}}, {{/-first}}{{.}}{{/exposures}}){{/hasExposures}}
|
||||
{{/elmImports}}
|
||||
@@ -0,0 +1,11 @@
|
||||
{-
|
||||
{{{appName}}}
|
||||
{{{appDescription}}}
|
||||
|
||||
{{#version}}OpenAPI spec version: {{{version}}}{{/version}}
|
||||
{{#infoEmail}}Contact: {{{infoEmail}}}{{/infoEmail}}
|
||||
|
||||
NOTE: This file is auto generated by the swagger code generator program.
|
||||
https://github.com/swagger-api/swagger-codegen.git
|
||||
Do not edit this file manually.
|
||||
-}
|
||||
@@ -0,0 +1,24 @@
|
||||
{{>licenseInfo}}
|
||||
|
||||
module Data.{{classname}} exposing ({{#models}}{{#model}}{{classname}}{{#hasChildren}}(..){{/hasChildren}}{{#isEnum}}(..){{/isEnum}}{{^isEnum}}{{#vars}}{{#isEnum}}, {{vendorExtensions.x-union-type}}(..){{/isEnum}}{{/vars}}{{/isEnum}}, {{classVarName}}Decoder, {{classVarName}}Encoder{{/model}}{{/models}})
|
||||
|
||||
{{>imports}}import Json.Decode as Decode exposing (Decoder)
|
||||
import Json.Decode.Pipeline exposing (decode, optional, required)
|
||||
import Json.Encode as Encode
|
||||
import Maybe exposing (map, withDefault)
|
||||
|
||||
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
{{#description}}
|
||||
{-
|
||||
{{{description}}}
|
||||
-}
|
||||
{{/description}}
|
||||
{{#isEnum}}{{>modelTypeUnion}}{{/isEnum}}{{^isEnum}}{{#hasChildren}}{{>modelTypeDiscriminator}}{{/hasChildren}}{{^hasChildren}}{{#isAlias}}{{>modelTypePrimitive}}{{/isAlias}}{{^isAlias}}{{>modelTypeAlias}}{{/isAlias}}{{/hasChildren}}{{/isEnum}}
|
||||
{{/model}}
|
||||
{{^-last}}
|
||||
|
||||
|
||||
{{/-last}}
|
||||
{{/models}}
|
||||
@@ -0,0 +1,28 @@
|
||||
|
||||
|
||||
type alias {{classname}} =
|
||||
{ {{#vars}}{{^-first}} , {{/-first}}{{name}} : {{^required}}Maybe {{/required}}{{#isContainer}}(List {{/isContainer}}{{#isEnum}}{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{datatype}}{{/isEnum}}{{#isContainer}}){{/isContainer}}
|
||||
{{/vars}} }
|
||||
{{#vars}}
|
||||
{{#isEnum}}
|
||||
|
||||
|
||||
{{>union}}
|
||||
{{/isEnum}}
|
||||
{{/vars}}
|
||||
|
||||
|
||||
{{>aliasDecoder}}
|
||||
|
||||
|
||||
{{>aliasEncoder}}
|
||||
{{#vars}}
|
||||
{{#isEnum}}
|
||||
|
||||
|
||||
{{>unionDecoder}}
|
||||
|
||||
|
||||
{{>unionEncoder}}
|
||||
{{/isEnum}}
|
||||
{{/vars}}
|
||||
@@ -0,0 +1,34 @@
|
||||
type {{classname}}
|
||||
{{#children}}
|
||||
{{#-first}}={{/-first}}{{^-first}}|{{/-first}} {{classname}}Type {{classname}}
|
||||
{{/children}}
|
||||
|
||||
|
||||
{{classVarName}}Decoder : Decoder {{classname}}
|
||||
{{classVarName}}Decoder =
|
||||
Decode.field "{{discriminator}}" Decode.string
|
||||
|> Decode.andThen {{classVarName}}TypeDecoder
|
||||
|
||||
|
||||
{{classVarName}}TypeDecoder : String -> Decoder {{classname}}
|
||||
{{classVarName}}TypeDecoder {{discriminator}} =
|
||||
case {{discriminator}} of
|
||||
{{#children}}
|
||||
"{{vendorExtensions.x-discriminator-value}}" ->
|
||||
Decode.map {{classname}}Type {{classVarName}}Decoder
|
||||
|
||||
{{/children}}
|
||||
_ ->
|
||||
Decode.fail <|
|
||||
"Trying to decode {{classname}}, but {{discriminator}} "
|
||||
++ toString {{discriminator}} ++ " is not supported."
|
||||
|
||||
|
||||
{{classVarName}}Encoder : {{classname}} -> Encode.Value
|
||||
{{classVarName}}Encoder model =
|
||||
case model of
|
||||
{{#children}}
|
||||
{{classname}}Type subModel ->
|
||||
{{classVarName}}Encoder subModel
|
||||
|
||||
{{/children}}
|
||||
@@ -0,0 +1,14 @@
|
||||
|
||||
|
||||
type alias {{classname}}
|
||||
= {{dataType}}
|
||||
|
||||
|
||||
{{classVarName}}Decoder : Decoder {{classname}}
|
||||
{{classVarName}}Decoder =
|
||||
{{vendorExtensions.x-decoder}}
|
||||
|
||||
|
||||
{{classVarName}}Encoder : {{classname}} -> Encode.Value
|
||||
{{classVarName}}Encoder =
|
||||
{{vendorExtensions.x-encoder}}
|
||||
@@ -0,0 +1,7 @@
|
||||
{{>union}}
|
||||
|
||||
|
||||
{{>unionDecoder}}
|
||||
|
||||
|
||||
{{>unionEncoder}}
|
||||
@@ -0,0 +1,6 @@
|
||||
type {{#vendorExtensions}}{{x-union-type}}{{/vendorExtensions}}
|
||||
{{#allowableValues}}
|
||||
{{#enumVars}}
|
||||
{{#-first}}={{/-first}}{{^-first}}|{{/-first}} {{name}}
|
||||
{{/enumVars}}
|
||||
{{/allowableValues}}
|
||||
@@ -0,0 +1,15 @@
|
||||
{{vendorExtensions.x-decoder}} : Decoder {{vendorExtensions.x-union-type}}
|
||||
{{vendorExtensions.x-decoder}} =
|
||||
Decode.string
|
||||
|> Decode.andThen (\str ->
|
||||
case str of
|
||||
{{#allowableValues}}
|
||||
{{#enumVars}}
|
||||
{{{value}}} ->
|
||||
Decode.succeed {{name}}
|
||||
|
||||
{{/enumVars}}
|
||||
{{/allowableValues}}
|
||||
other ->
|
||||
Decode.fail <| "Unknown type: " ++ other
|
||||
)
|
||||
@@ -0,0 +1,10 @@
|
||||
{{vendorExtensions.x-encoder}} : {{vendorExtensions.x-union-type}} -> Encode.Value
|
||||
{{vendorExtensions.x-encoder}} model =
|
||||
case model of
|
||||
{{#allowableValues}}
|
||||
{{#enumVars}}
|
||||
{{name}} ->
|
||||
Encode.string {{{value}}}
|
||||
|
||||
{{/enumVars}}
|
||||
{{/allowableValues}}
|
||||
Reference in New Issue
Block a user