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

[Kotlin] support array, list in collection type #1564

Merged
merged 4 commits into from
Nov 29, 2018
Merged
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
5 changes: 5 additions & 0 deletions docs/generators/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
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"),
Expand All @@ -47,6 +49,17 @@ public enum DateLibrary {
}
}

public enum CollectionType {
ARRAY("array"),
LIST("list");

public final String value;

CollectionType(String value) {
this.value = value;
}
}

/**
* Constructs an instance of `KotlinClientCodegen`.
*/
Expand Down Expand Up @@ -74,6 +87,13 @@ public KotlinClientCodegen() {
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() {
Expand All @@ -92,6 +112,10 @@ public void setDateLibrary(String library) {
this.dateLibrary = library;
}

public void setCollectionType(String collectionType) {
this.collectionType = collectionType;
}

@Override
public void processOpts() {
super.processOpts();
Expand All @@ -116,6 +140,15 @@ public void processOpts() {
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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,33 @@ public void arrayPropertyTest() {
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();
Expand Down