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

Issue #4 - sealed classes #5

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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
build
*~
*.swp
out
out
gradle.properties
*.iml
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ repositories {
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
compile 'org.reflections:reflections:0.9.11'

testCompile "com.winterbe:expekt:0.5.0"
testCompile "org.jetbrains.spek:spek-api:$spek_version"
Expand Down
Empty file modified gradlew
100644 → 100755
Empty file.
431 changes: 291 additions & 140 deletions src/main/kotlin/me/ntrrgc/tsGenerator/TypeScriptGenerator.kt

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions src/main/kotlin/me/ntrrgc/tsGenerator/unfold.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package me.ntrrgc.tsGenerator

fun <T,R> Iterable<T>.unfold(f: (Iterable<T>) -> Pair<R?, Iterable<T>>): Iterable<R> {
return if (iterator().hasNext()) {
val (first, remaining) = f(this)
if (first !== null) {
listOf(first) + remaining.unfold(f)
} else {
remaining.unfold(f)
}
} else {
emptyList()
}
}
18 changes: 2 additions & 16 deletions src/test/kotlin/me/ntrrgc/tsGenerator/tests/EnumDefinition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@

package me.ntrrgc.tsGenerator.tests

class EnumDefinition(val code: String): TypeScriptDefinition {
override fun toString(): String {
return code
}
import java.util.SortedSet

override fun equals(other: Any?): Boolean {
if (other !is EnumDefinition) {
return false
}

return this.code == other.code
}

override fun hashCode(): Int {
return code.hashCode()
}
}
data class EnumDefinition(val name: String, val union: SortedSet<String>): TypeScriptDefinition
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package me.ntrrgc.tsGenerator.tests

import java.util.SortedSet

data class SealedClassDefinition(
val name: String,
val classes: SortedSet<ClassDefinition>,
val union: SortedSet<String>
): TypeScriptDefinition, Comparable<SealedClassDefinition> {

private val eqHashList = listOf(classes, name, union)

override fun equals(other: Any?): Boolean =
if (other !is SealedClassDefinition) false
else {
eqHashList == other.eqHashList
}

override fun hashCode(): Int = eqHashList.hashCode()

override fun compareTo(other: SealedClassDefinition): Int {
classes.size.compareTo(other.classes.size).let {
if (it != 0) return@compareTo it
}

classes.zip(other.classes).asSequence()
.map { (c1, c2) -> c1.compareTo(c2) }
.firstOrNull { it != 0 }
.let {
if (it != null) return@compareTo it
}

name.compareTo(other.name)
.let {
if (it != 0) return@compareTo it
}

union.size.compareTo(other.union.size).let {
if (it != 0) return@compareTo it
}

union.zip(other.union).asSequence()
.map { (c1, c2) -> c1.compareTo(c2) }
.firstOrNull { it != 0 }
.let {
if (it != null) return@compareTo it
}

return 0
}

}
8 changes: 8 additions & 0 deletions src/test/kotlin/me/ntrrgc/tsGenerator/tests/TypeDefinition.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package me.ntrrgc.tsGenerator.tests

import me.ntrrgc.tsGenerator.unfold
import java.util.SortedSet

object TypeDefinition {

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2017 Alicia Boya García
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.ntrrgc.tsGenerator.tests

import me.ntrrgc.tsGenerator.unfold

interface TypeScriptDefinition {

companion object {
operator fun invoke(tsCode: String): TypeScriptDefinition {
val typedefs = tsCode.lines().unfold { lines ->
fun Iterable<String>.indexOfTypeDeclaration(): Int = indexOfFirst { it.trim().startsWith("interface") || it.trim().startsWith("type") }
.takeIf { it >= 0 } ?: count()

val restOfLines = lines.drop(lines.indexOfTypeDeclaration())
val classDefLineCount = 1 + restOfLines.drop(1).indexOfTypeDeclaration()
Pair(
restOfLines.take(classDefLineCount).filter { it.isNotBlank() },
restOfLines.drop(classDefLineCount))
}
val classes = typedefs.mapNotNull { code ->
if (code.firstOrNull()?.trim()?.startsWith("interface") == true) {
ClassDefinition(code.joinToString("\n"))
} else {
null
}
}.toSortedSet()

val unionDefs = typedefs
.filter { code ->
code.firstOrNull()?.trim()?.startsWith("type") == true
}
.map { code -> code.joinToString("\n").trim() }
.map { unionDef ->
val (_, name, _, def) = unionDef.split(Regex("\\s+"), limit = 4)
val union = def.trimStart('=', ' ', '\n', '(')
.trimEnd(' ', '\n', ')', ';')
.split(Regex("\\s*\\|\\s*"))
.toSortedSet()
name to union
}

return if (classes.size == 1 && unionDefs.isEmpty()) {
classes.single()
} else if (classes.isEmpty() && unionDefs.size == 1 && unionDefs.single().second.all { it.startsWith('"') && it.endsWith('"') }) {
EnumDefinition(unionDefs.single().first, unionDefs.single().second)
} else if (unionDefs.size == 1 && unionDefs.single().second.none { it.startsWith('"') && it.endsWith('"') }) {
SealedClassDefinition(unionDefs.single().first, classes, unionDefs.single().second)
} else {
throw RuntimeException("Unknown definition type: ${tsCode.trim()}")
}
}
}

}

This file was deleted.

Loading