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

Enum customization & export statements #2

Open
wants to merge 3 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
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ junitPlatform {
apply plugin: 'kotlin'

repositories {
maven { url "https://dl.bintray.com/jetbrains/spek" }
mavenCentral()
}

Expand All @@ -51,8 +50,8 @@ dependencies {
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

testCompile 'com.winterbe:expekt:0.5.0'
testCompile 'org.jetbrains.spek:spek-api:1.1.0-beta3'
testCompile 'org.jetbrains.spek:spek-api:1.1.0'
testRuntime 'org.junit.platform:junit-platform-launcher:1.0.0-M3'
testRuntime 'org.jetbrains.spek:spek-junit-platform-engine:1.1.0-beta3'
testRuntime 'org.jetbrains.spek:spek-junit-platform-engine:1.1.0'
testCompile 'com.google.code.findbugs:jsr305:3.0.1'
}
14 changes: 9 additions & 5 deletions src/main/kotlin/me/ntrrgc/tsGenerator/TypeScriptGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import kotlin.reflect.full.createType
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.full.superclasses
import kotlin.reflect.jvm.javaType
import me.ntrrgc.tsGenerator.defaultEnumTransformer

/**
* TypeScript definition generator.
Expand Down Expand Up @@ -78,7 +79,9 @@ class TypeScriptGenerator(
private val mappings: Map<KClass<*>, String> = mapOf(),
classTransformers: List<ClassTransformer> = listOf(),
ignoreSuperclasses: Set<KClass<*>> = setOf(),
private val intTypeName: String = "number"
private val intTypeName: String = "number",
addExportStatements: Boolean = false,
private val enumTransformer: (KClass<*>, Any) -> String = ::defaultEnumTransformer
) {
private val visitedClasses: MutableSet<KClass<*>> = java.util.HashSet()
private val generatedDefinitions = mutableListOf<String>()
Expand All @@ -88,6 +91,7 @@ class TypeScriptGenerator(
java.io.Serializable::class,
Comparable::class
).plus(ignoreSuperclasses)
private val export = if (addExportStatements) "export " else ""

init {
rootClasses.forEach { visitClass(it) }
Expand Down Expand Up @@ -183,9 +187,9 @@ class TypeScriptGenerator(
}

private fun generateEnum(klass: KClass<*>): String {
return "type ${klass.simpleName} = ${klass.java.enumConstants
.map { constant: Any ->
constant.toString().toJSString()
return "${export}type ${klass.simpleName} = ${klass.java.enumConstants
.map { constant: Any ->
enumTransformer(klass, constant).toJSString()
}
.joinToString(" | ")
};"
Expand Down Expand Up @@ -220,7 +224,7 @@ class TypeScriptGenerator(
""
}

return "interface ${klass.simpleName}$templateParameters$extendsString {\n" +
return "${export}interface ${klass.simpleName}$templateParameters$extendsString {\n" +
klass.declaredMemberProperties
.filter { !isFunctionType(it.returnType.javaType) }
.filter {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package me.ntrrgc.tsGenerator

import kotlin.reflect.KClass

fun defaultEnumTransformer(klass: KClass<*>, enumValue: Any) = enumValue.toString()