Kotlin collection type (#1564)

* Allow specifying type

* Add test

* Update docs

* Don't modify types if the default option is chosen
This commit is contained in:
Simas Abramovas 2018-11-29 04:33:55 +02:00 committed by William Cheng
parent 41185d3c6f
commit 777bf1f3aa
3 changed files with 65 additions and 0 deletions

View File

@ -28,4 +28,9 @@ CONFIG OPTIONS for kotlin
java8 - Java 8 native JSR310
threetenbp - Threetenbp
collectionType
Option. Collection type to use
array - kotlin.Array
list - kotlin.collections.List
Back to the [generators list](README.md)

View File

@ -31,9 +31,11 @@ import java.util.Map;
public class KotlinClientCodegen extends AbstractKotlinCodegen {
public static final String DATE_LIBRARY = "dateLibrary";
public static final String COLLECTION_TYPE = "collectionType";
private static final Logger LOGGER = LoggerFactory.getLogger(KotlinClientCodegen.class);
protected String dateLibrary = DateLibrary.JAVA8.value;
protected String collectionType = CollectionType.ARRAY.value;
public enum DateLibrary {
STRING("string"),
@ -47,6 +49,17 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
}
}
public enum CollectionType {
ARRAY("array"),
LIST("list");
public final String value;
CollectionType(String value) {
this.value = value;
}
}
/**
* Constructs an instance of `KotlinClientCodegen`.
*/
@ -74,6 +87,13 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
dateOptions.put(DateLibrary.JAVA8.value, "Java 8 native JSR310");
dateLibrary.setEnum(dateOptions);
cliOptions.add(dateLibrary);
CliOption collectionType = new CliOption(COLLECTION_TYPE, "Option. Collection type to use");
Map<String, String> collectionOptions = new HashMap<>();
collectionOptions.put(CollectionType.ARRAY.value, "kotlin.Array");
collectionOptions.put(CollectionType.LIST.value, "kotlin.collections.List");
collectionType.setEnum(collectionOptions);
cliOptions.add(collectionType);
}
public CodegenType getTag() {
@ -92,6 +112,10 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
this.dateLibrary = library;
}
public void setCollectionType(String collectionType) {
this.collectionType = collectionType;
}
@Override
public void processOpts() {
super.processOpts();
@ -116,6 +140,15 @@ public class KotlinClientCodegen extends AbstractKotlinCodegen {
additionalProperties.put(DateLibrary.JAVA8.value, true);
}
if (additionalProperties.containsKey(COLLECTION_TYPE)) {
setCollectionType(additionalProperties.get(COLLECTION_TYPE).toString());
}
if (CollectionType.LIST.value.equals(collectionType)) {
typeMapping.put("array", "kotlin.collections.List");
typeMapping.put("list", "kotlin.collections.List");
}
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("build.gradle.mustache", "", "build.gradle"));
supportingFiles.add(new SupportingFile("settings.gradle.mustache", "", "settings.gradle"));

View File

@ -199,6 +199,33 @@ public class KotlinClientCodegenModelTest {
Assert.assertTrue(property.isContainer);
}
@Test(description = "convert a model with array property to a kotlin.collections.List")
public void listPropertyTest() {
final Schema model = getArrayTestSchema();
final KotlinClientCodegen codegen = new KotlinClientCodegen();
codegen.setCollectionType(KotlinClientCodegen.CollectionType.LIST.value);
codegen.processOpts();
final CodegenModel generated = codegen.fromModel("sample", model, Collections.singletonMap("sample", model));
Assert.assertEquals(generated.name, "sample");
Assert.assertEquals(generated.classname, "Sample");
Assert.assertEquals(generated.description, "a sample model");
Assert.assertEquals(generated.vars.size(), 2);
final CodegenProperty property = generated.vars.get(1);
Assert.assertEquals(property.baseName, "examples");
Assert.assertEquals(property.getter, "getExamples");
Assert.assertEquals(property.setter, "setExamples");
Assert.assertEquals(property.dataType, "kotlin.collections.List<kotlin.String>");
Assert.assertEquals(property.name, "examples");
Assert.assertEquals(property.defaultValue, "null");
Assert.assertEquals(property.baseType, "kotlin.collections.List");
Assert.assertEquals(property.containerType, "array");
Assert.assertFalse(property.required);
Assert.assertTrue(property.isContainer);
}
@Test(description = "convert a model with a map property")
public void mapPropertyTest() {
final Schema schema = getMapSchema();