-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Specify value types for maps in json schema
- Loading branch information
1 parent
6cafe1f
commit 42f84ee
Showing
4 changed files
with
213 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/schema-generator/src/main/kotlin/org/icpclive/generator/schema/gen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.icpclive.generator.schema | ||
|
||
|
||
import com.github.ajalt.clikt.core.CliktCommand | ||
import com.github.ajalt.clikt.core.subcommands | ||
|
||
|
||
class MainCommand : CliktCommand() { | ||
override fun run() {} | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
MainCommand().subcommands(JsonCommand(), TSCommand()).main(args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/schema-generator/src/main/kotlin/org/icpclive/generator/schema/ts.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.icpclive.generator.schema | ||
|
||
import com.github.ajalt.clikt.core.CliktCommand | ||
import com.github.ajalt.clikt.parameters.options.* | ||
import dev.adamko.kxstsgen.KxsTsGenerator | ||
import dev.adamko.kxstsgen.core.TsDeclaration | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.serializer | ||
import java.io.File | ||
|
||
// This is a copy-pasted generating method to work around a bug. | ||
fun KxsTsGenerator.patchedGenerate(vararg serializers: KSerializer<*>) : String { | ||
return serializers | ||
.toSet() | ||
.flatMap { serializer -> descriptorsExtractor(serializer) } | ||
.toSet() | ||
.flatMap { descriptor -> elementConverter(descriptor) } | ||
.toSet() | ||
.groupBy { element -> sourceCodeGenerator.groupElementsBy(element) } | ||
.mapValues { (_, elements) -> | ||
elements | ||
.filterIsInstance<TsDeclaration>() | ||
.map { element -> sourceCodeGenerator.generateDeclaration(element) } | ||
.filter { it.isNotBlank() } | ||
.toSet() // workaround for https://github.com/adamko-dev/kotlinx-serialization-typescript-generator/issues/110 | ||
.joinToString(config.declarationSeparator) | ||
} | ||
.values | ||
.joinToString(config.declarationSeparator) | ||
} | ||
|
||
class TSCommand : CliktCommand(name = "type-script") { | ||
private val className by option(help = "Class name for which schema should be generated").multiple() | ||
private val output by option("--output", "-o", help = "File to print output").required() | ||
|
||
override fun run() { | ||
val tsGenerator = KxsTsGenerator() | ||
val things = className.map { serializer(Class.forName(it)) } | ||
File(output).printWriter().use { | ||
it.println(tsGenerator.patchedGenerate(*things.toTypedArray())) | ||
} | ||
} | ||
} |