Skip to content

Commit 0913073

Browse files
committed
add filename to downloaded file
1 parent 2081a60 commit 0913073

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.fractalwrench.json2kotlin
2+
3+
data class ConversionInfo(val filename: String)

core/src/main/kotlin/com/fractalwrench/json2kotlin/Kotlin2JsonConverter.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ class Kotlin2JsonConverter(private val buildDelegate: SourceBuildDelegate = Gson
1717
/**
1818
* Converts an InputStream of JSON to Kotlin source code, writing the result to the OutputStream.
1919
*/
20-
fun convert(input: InputStream, output: OutputStream, args: ConversionArgs) {
20+
fun convert(input: InputStream, output: OutputStream, args: ConversionArgs): ConversionInfo {
2121
try {
2222
val jsonRoot = jsonReader.readJsonTree(input, args)
2323
val jsonStack = treeTraverser.traverse(jsonRoot, args.rootClassName)
2424
val generator = TypeSpecGenerator(buildDelegate, ::defaultGroupingStrategy)
2525
val typeSpecs = generator.generateTypeSpecs(jsonStack)
26+
27+
val name = typeSpecs.peek().name
28+
val conversionInfo = ConversionInfo("$name.kt")
2629
sourceFileWriter.writeSourceFile(typeSpecs, args, output)
30+
return conversionInfo
2731
} catch (e: JsonSyntaxException) {
2832
throw IllegalArgumentException("Invalid JSON supplied", e)
2933
}

spring/src/main/kotlin/com/fractalwrench/json2kotlin/ConversionController.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ class ConversionController {
2929

3030
@PostMapping("/")
3131
fun convertToKotlin(model: Model, @ModelAttribute conversionForm: ConversionForm): String {
32-
val os = conversionService.convert(conversionForm.json)
32+
val pair = conversionService.convert(conversionForm.json)
3333
model.addAttribute(formKey, conversionForm)
34-
model.addAttribute("kotlin", String(os.toByteArray()))
35-
model.addAttribute("filename", "Foo.kt") // FIXME!
34+
model.addAttribute("kotlin", String(pair.first.toByteArray()))
35+
model.addAttribute("filename", pair.second)
3636
return displayConversionForm(model)
3737
}
3838

spring/src/main/kotlin/com/fractalwrench/json2kotlin/KotlinConversionService.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@ class KotlinConversionService {
1212
val maxPayloadSize = 10000
1313
}
1414

15-
fun convert(json: String): ByteArrayOutputStream {
15+
fun convert(json: String): Pair<ByteArrayOutputStream, String> {
1616
val os = ByteArrayOutputStream()
1717

1818
if (json.length > maxPayloadSize) {
1919
throw IllegalArgumentException("JSON input cannot be larger than $maxPayloadSize")
2020
}
2121

2222
val inputStream = ByteArrayInputStream(json.toByteArray())
23-
24-
Kotlin2JsonConverter().convert(inputStream, os, ConversionArgs())
25-
return os
23+
val conversionInfo = Kotlin2JsonConverter().convert(inputStream, os, ConversionArgs())
24+
return Pair(os, conversionInfo.filename)
2625
}
2726
}

0 commit comments

Comments
 (0)