Skip to content

Commit 6417773

Browse files
committed
close io
1 parent c8d1c47 commit 6417773

File tree

6 files changed

+42
-29
lines changed

6 files changed

+42
-29
lines changed

TODO.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Ordered roughly in priority:
1010

1111
- Make website tool prettier
1212
- Throttle website tool/prepare for HN
13+
- Code style check
1314
- Docs + writeup
1415

1516
- Unit tests for individual methods (once codebase a bit less volatile)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ internal class ClassTypeHolder(val delegate: SourceBuildDelegate, groupingStrate
7070
}
7171

7272
private fun buildClass(commonElements: List<TypedJsonElement>, fields: Collection<String>): TypeSpec.Builder {
73-
val identifier = commonElements.last().kotlinIdentifier // FIXME should only pass in one if that's all that's needed!
73+
val identifier = commonElements.last().kotlinIdentifier
7474
val classBuilder = TypeSpec.classBuilder(identifier.capitalize())
7575
val constructor = FunSpec.constructorBuilder()
7676

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package com.fractalwrench.json2kotlin
22

3-
class ConversionArgs(val rootClassName: String = "Example", val packageName: String? = null)
3+
data class ConversionArgs(val rootClassName: String = "Example", val packageName: String? = null)

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ class JsonReader(private val jsonParser: JsonParser) {
1818
* Reads a JSON string using GSON.
1919
*/
2020
internal fun readJsonTree(input: InputStream, args: ConversionArgs): JsonObject {
21-
// TODO check IO is closed properly everywhere!
2221

23-
with(jsonParser.parse(BufferedReader(InputStreamReader(input)))) {
24-
return when {
25-
isJsonObject -> asJsonObject
26-
isJsonArray -> addRootWrapper(asJsonArray, args.rootClassName)
27-
else -> throw IllegalStateException("Failed to read json object")
22+
BufferedReader(InputStreamReader(input)).use {
23+
with(jsonParser.parse(it)) {
24+
return when {
25+
isJsonObject -> asJsonObject
26+
isJsonArray -> addRootWrapper(asJsonArray, args.rootClassName)
27+
else -> throw IllegalStateException("Failed to read json object")
28+
}
2829
}
2930
}
3031
}

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

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import java.util.HashSet
99

1010
internal class JsonTypeDetector {
1111

12-
internal fun typeForJsonField(jsonElement: JsonElement, key: String, jsonElementMap: Map<JsonElement, TypeSpec>): TypeName {
12+
internal fun typeForJsonField(jsonElement: JsonElement,
13+
key: String,
14+
jsonElementMap: Map<JsonElement, TypeSpec>): TypeName {
1315
with(jsonElement) {
1416
return when {
1517
isJsonPrimitive -> typeForJsonPrimitive(asJsonPrimitive)
@@ -21,7 +23,7 @@ internal class JsonTypeDetector {
2123
}
2224
}
2325

24-
internal fun typeForJsonPrimitive(primitive: JsonPrimitive): TypeName {
26+
private fun typeForJsonPrimitive(primitive: JsonPrimitive): TypeName {
2527
return when {
2628
primitive.isBoolean -> Boolean::class
2729
primitive.isNumber -> Number::class
@@ -30,41 +32,48 @@ internal class JsonTypeDetector {
3032
}.asTypeName()
3133
}
3234

33-
34-
// FIXME feels really messy from here on out
35-
36-
37-
internal fun typeForJsonObject(jsonObject: JsonObject, key: String, jsonElementMap: Map<JsonElement, TypeSpec>): TypeName {
35+
private fun typeForJsonObject(jsonObject: JsonObject,
36+
key: String,
37+
jsonElementMap: Map<JsonElement, TypeSpec>): TypeName {
3838
val existingTypeName = jsonElementMap[jsonObject]
39-
if (existingTypeName != null) {
40-
return ClassName.bestGuess(existingTypeName.name!!)
41-
}
42-
43-
val identifier = key.toKotlinIdentifier().capitalize()
39+
val identifier = existingTypeName?.name ?: key.toKotlinIdentifier().capitalize()
4440
return ClassName.bestGuess(identifier)
4541
}
4642

47-
internal fun typeForJsonArray(jsonArray: JsonArray, key: String, jsonElementMap: Map<JsonElement, TypeSpec>): TypeName {
43+
private fun typeForJsonArray(jsonArray: JsonArray,
44+
key: String,
45+
jsonElementMap: Map<JsonElement, TypeSpec>): TypeName {
46+
val pair = findAllArrayTypes(jsonArray, key, jsonElementMap)
47+
val arrayTypes = pair.first
48+
val nullable = pair.second
49+
val arrayType = deduceArrayType(arrayTypes, nullable)
50+
return ParameterizedTypeName.get(Array<Any>::class.asClassName(), arrayType)
51+
}
52+
53+
private fun findAllArrayTypes(jsonArray: JsonArray,
54+
key: String,
55+
jsonElementMap: Map<JsonElement, TypeSpec>): Pair<HashSet<TypeName>, Boolean> {
4856
val arrayTypes = HashSet<TypeName>()
4957
var nullable = false
5058

5159
jsonArray.withIndex().forEach {
5260
val sanitisedName = key.toKotlinIdentifier()
61+
val fieldKey = nameForArrayField(it.index, sanitisedName)
62+
5363
with(it.value) {
5464
when {
5565
isJsonPrimitive -> arrayTypes.add(typeForJsonPrimitive(asJsonPrimitive))
56-
isJsonArray -> arrayTypes.add(typeForJsonArray(asJsonArray, nameForArrayField(it.index, sanitisedName), jsonElementMap))
57-
isJsonObject -> arrayTypes.add(typeForJsonObject(asJsonObject, nameForArrayField(it.index, sanitisedName), jsonElementMap))
66+
isJsonArray -> arrayTypes.add(typeForJsonArray(asJsonArray, fieldKey, jsonElementMap))
67+
isJsonObject -> arrayTypes.add(typeForJsonObject(asJsonObject, fieldKey, jsonElementMap))
5868
isJsonNull -> nullable = true
5969
else -> throw IllegalStateException("Unexpected state in array")
6070
}
6171
}
6272
}
63-
val arrayType = deduceArrayType(arrayTypes, nullable)
64-
return ParameterizedTypeName.get(Array<Any>::class.asClassName(), arrayType)
73+
return Pair(arrayTypes, nullable)
6574
}
6675

67-
internal fun deduceArrayType(arrayTypes: HashSet<TypeName>, nullable: Boolean): TypeName {
76+
private fun deduceArrayType(arrayTypes: HashSet<TypeName>, nullable: Boolean): TypeName {
6877
val hasMultipleType = arrayTypes.size > 1 || arrayTypes.isEmpty()
6978
val arrayTypeName = when {
7079
hasMultipleType -> Any::class.asTypeName()

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ internal class SourceFileWriter {
2323
sourceFile.addType(stack.pop())
2424
}
2525

26-
val bufferedWriter = BufferedWriter(OutputStreamWriter(output))
27-
sourceFile.build().writeTo(bufferedWriter)
28-
bufferedWriter.flush()
26+
BufferedWriter(OutputStreamWriter(output)).use {
27+
sourceFile.build().writeTo(it)
28+
it.flush()
29+
}
30+
2931
}
3032

3133
}

0 commit comments

Comments
 (0)