Skip to content

Commit 1585433

Browse files
rename fromJsonObject to fromJsonPrimitive or fromJsonElement
1 parent bf22e3c commit 1585433

File tree

16 files changed

+187
-119
lines changed

16 files changed

+187
-119
lines changed

buildSrc/src/main/kotlin/com/datadog/gradle/plugin/jsonschema/generator/ClassJsonElementDeserializerGenerator.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class ClassJsonElementDeserializerGenerator(
220220
"%L.add(%T.%L(it))",
221221
Identifier.PARAM_COLLECTION,
222222
arrayType.items.asKotlinTypeName(rootTypeName),
223-
Identifier.FUN_FROM_JSON_OBJ
223+
Identifier.FUN_FROM_JSON_ELEMENT
224224
)
225225
is TypeDefinition.Class -> addStatement(
226226
"%L.add(%T.%L(it.asJsonObject))",
@@ -267,12 +267,12 @@ class ClassJsonElementDeserializerGenerator(
267267
rootTypeName: String
268268
) {
269269
val opt = if (nullable) "?" else ""
270-
beginControlFlow("$assignee = $getter$opt.asJsonObject$opt.let")
270+
beginControlFlow("$assignee = $getter$opt.let")
271271

272272
addStatement(
273273
"%T.%L(it)",
274274
propertyType.asKotlinTypeName(rootTypeName),
275-
Identifier.FUN_FROM_JSON_OBJ
275+
Identifier.FUN_FROM_JSON_ELEMENT
276276
)
277277
endControlFlow()
278278
}

buildSrc/src/main/kotlin/com/datadog/gradle/plugin/jsonschema/generator/Identifier.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ object Identifier {
1313
const val FUN_TO_JSON_ELT = "toJsonElement"
1414
const val FUN_FROM_JSON = "fromJson"
1515
const val FUN_FROM_JSON_OBJ = "fromJsonObject"
16+
const val FUN_FROM_JSON_PRIMITIVE = "fromJsonPrimitive"
17+
const val FUN_FROM_JSON_ELEMENT = "fromJsonElement"
1618

1719
const val PARAM_JSON_STR = "jsonString"
1820
const val PARAM_JSON_ARRAY = "jsonArray"
1921
const val PARAM_JSON_OBJ = "jsonObject"
2022
const val PARAM_JSON_ELEMENT = "jsonElement"
23+
const val PARAM_JSON_PRIMITIVE = "jsonPrimitive"
2124
const val PARAM_JSON_VALUE = "jsonValue"
2225
const val PARAM_ADDITIONAL_PROPS = "additionalProperties"
2326
const val PARAM_COLLECTION = "collection"

buildSrc/src/main/kotlin/com/datadog/gradle/plugin/jsonschema/generator/MultiClassGenerator.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ class MultiClassGenerator(
103103
funBuilder.beginControlFlow("try")
104104

105105
funBuilder.addStatement(
106-
"val %L = %T.parseString(%L).asJsonObject",
107-
Identifier.PARAM_JSON_OBJ,
106+
"val %L = %T.parseString(%L)",
107+
Identifier.PARAM_JSON_ELEMENT,
108108
ClassNameRef.JsonParser,
109109
Identifier.PARAM_JSON_STR
110110
)
111111
funBuilder.addStatement(
112112
"return %L(%L)",
113-
Identifier.FUN_FROM_JSON_OBJ,
114-
Identifier.PARAM_JSON_OBJ
113+
Identifier.FUN_FROM_JSON_ELEMENT,
114+
Identifier.PARAM_JSON_ELEMENT
115115
)
116116

117117
funBuilder.nextControlFlow(
@@ -133,7 +133,7 @@ class MultiClassGenerator(
133133
rootTypeName: String
134134
): FunSpec {
135135
val returnType = definition.asKotlinTypeName(rootTypeName)
136-
val funBuilder = FunSpec.builder(Identifier.FUN_FROM_JSON_OBJ)
136+
val funBuilder = FunSpec.builder(Identifier.FUN_FROM_JSON_ELEMENT)
137137
.addAnnotation(AnnotationSpec.builder(JvmStatic::class).build())
138138
.throws(ClassNameRef.JsonParseException)
139139
.addParameter(Identifier.PARAM_JSON_ELEMENT, ClassNameRef.JsonElement)
@@ -177,7 +177,7 @@ class MultiClassGenerator(
177177
funBuilder.addStatement(
178178
"%T.%L(%L)",
179179
typeName,
180-
Identifier.FUN_FROM_JSON_OBJ,
180+
Identifier.FUN_FROM_JSON_PRIMITIVE,
181181
Identifier.PARAM_JSON_ELEMENT
182182
)
183183
funBuilder.nextControlFlow("else")

buildSrc/src/main/kotlin/com/datadog/gradle/plugin/jsonschema/generator/OneOfPrimitiveOptionGenerator.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ class OneOfPrimitiveOptionGenerator(
8787

8888
funBuilder.addStatement(
8989
"val %L = %T.parseString(%L)",
90-
Identifier.PARAM_JSON_OBJ,
90+
Identifier.PARAM_JSON_ELEMENT,
9191
ClassNameRef.JsonParser,
9292
Identifier.PARAM_JSON_STR
9393
)
9494
funBuilder.beginControlFlow("try")
9595
funBuilder.addStatement(
9696
"return %L(%L.asJsonPrimitive)",
97-
Identifier.FUN_FROM_JSON_OBJ,
98-
Identifier.PARAM_JSON_OBJ
97+
Identifier.FUN_FROM_JSON_PRIMITIVE,
98+
Identifier.PARAM_JSON_ELEMENT
9999
)
100100
funBuilder.nextControlFlow("catch (%L: %T)", Identifier.CAUGHT_EXCEPTION, ClassNameRef.IllegalStateException)
101101
funBuilder.addStatement(
@@ -111,10 +111,10 @@ class OneOfPrimitiveOptionGenerator(
111111

112112
private fun generateElementDeserializer(className: String, definition: TypeDefinition.Primitive): FunSpec {
113113
val returnType = ClassName.bestGuess(className)
114-
val funBuilder = FunSpec.builder(Identifier.FUN_FROM_JSON_OBJ).apply {
114+
val funBuilder = FunSpec.builder(Identifier.FUN_FROM_JSON_PRIMITIVE).apply {
115115
addAnnotation(AnnotationSpec.builder(JvmStatic::class).build())
116116
throws(ClassNameRef.JsonParseException)
117-
addParameter(Identifier.PARAM_JSON_OBJ, ClassNameRef.JsonPrimitive)
117+
addParameter(Identifier.PARAM_JSON_PRIMITIVE, ClassNameRef.JsonPrimitive)
118118
returns(returnType)
119119
}
120120

@@ -124,18 +124,18 @@ class OneOfPrimitiveOptionGenerator(
124124

125125
funBuilder.beginControlFlow(
126126
"if (%L.${typeInfo.isFuncName})",
127-
Identifier.PARAM_JSON_OBJ
127+
Identifier.PARAM_JSON_PRIMITIVE
128128
)
129129
funBuilder.addStatement(
130130
"return %T(%L.${typeInfo.asFuncName})",
131131
returnType,
132-
Identifier.PARAM_JSON_OBJ
132+
Identifier.PARAM_JSON_PRIMITIVE
133133
)
134134
funBuilder.nextControlFlow("else")
135135
funBuilder.addStatement(
136136
"throw %T(\"Can't convert %L to ${typeInfo.typeName}\")",
137137
ClassNameRef.JsonParseException,
138-
Identifier.PARAM_JSON_OBJ
138+
Identifier.PARAM_JSON_PRIMITIVE
139139
)
140140
funBuilder.endControlFlow()
141141

buildSrc/src/test/kotlin/com/datadog/gradle/plugin/jsonschema/TestDefinitions.kt

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -686,31 +686,38 @@ val Household = TypeDefinition.Class(
686686
type = TypeDefinition.OneOfClass(
687687
name = "Situation",
688688
options = listOf(
689-
TypeDefinition.Class(
690-
name = "Marriage",
691-
required = setOf("spouses"),
692-
properties = listOf(
693-
TypeProperty(
694-
name = "spouses",
695-
type = TypeDefinition.Array(
696-
items = TypeDefinition.Primitive(JsonPrimitiveType.STRING)
689+
TypeDefinition.OneOfClass.Option.Class(
690+
TypeDefinition.Class(
691+
name = "Marriage",
692+
required = setOf("spouses"),
693+
properties = listOf(
694+
TypeProperty(
695+
name = "spouses",
696+
type = TypeDefinition.Array(
697+
items = TypeDefinition.Primitive(JsonPrimitiveType.STRING)
698+
)
697699
)
698700
)
699701
)
700702
),
701-
TypeDefinition.Class(
702-
name = "Cotenancy",
703-
required = setOf("roommates"),
704-
properties = listOf(
705-
TypeProperty(
706-
name = "roommates",
707-
type = TypeDefinition.Array(
708-
items = TypeDefinition.Primitive(JsonPrimitiveType.STRING)
703+
TypeDefinition.OneOfClass.Option.Class(
704+
TypeDefinition.Class(
705+
name = "Cotenancy",
706+
required = setOf("roommates"),
707+
properties = listOf(
708+
TypeProperty(
709+
name = "roommates",
710+
type = TypeDefinition.Array(
711+
items = TypeDefinition.Primitive(JsonPrimitiveType.STRING)
712+
)
709713
)
710714
)
711715
)
716+
),
717+
TypeDefinition.OneOfClass.Option.Primitive(
718+
primitive = TypeDefinition.Primitive(type = JsonPrimitiveType.INTEGER)
712719
)
713-
).map { TypeDefinition.OneOfClass.Option.Class(it) }
720+
)
714721
)
715722
)
716723
)

buildSrc/src/test/kotlin/com/example/forgery/HouseholdForgeryFactory.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class HouseholdForgeryFactory : ForgeryFactory<Household> {
3333
),
3434
Household.Situation.Cotenancy(
3535
roommates = forge.aList { anAlphabeticalString() }
36+
),
37+
Household.Situation.Long(
38+
item = forge.aLong()
3639
)
3740
)
3841
)

buildSrc/src/test/kotlin/com/example/model/Animal.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ public sealed class Animal {
133133
@Throws(JsonParseException::class)
134134
public fun fromJson(jsonString: String): Animal {
135135
try {
136-
val jsonObject = JsonParser.parseString(jsonString).asJsonObject
137-
return fromJsonObject(jsonObject)
136+
val jsonElement = JsonParser.parseString(jsonString)
137+
return fromJsonElement(jsonElement)
138138
} catch (e: IllegalStateException) {
139139
throw JsonParseException(
140140
"Unable to parse json into one of type Animal",
@@ -145,7 +145,7 @@ public sealed class Animal {
145145

146146
@JvmStatic
147147
@Throws(JsonParseException::class)
148-
public fun fromJsonObject(jsonElement: JsonElement): Animal {
148+
public fun fromJsonElement(jsonElement: JsonElement): Animal {
149149
val errors = mutableListOf<Throwable>()
150150
val asFish = try {
151151
if (jsonElement is JsonObject) {

buildSrc/src/test/kotlin/com/example/model/Household.kt

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.google.gson.JsonPrimitive
99
import java.lang.IllegalStateException
1010
import java.lang.NullPointerException
1111
import java.lang.NumberFormatException
12+
import java.lang.UnsupportedOperationException
1213
import kotlin.Boolean
1314
import kotlin.Long
1415
import kotlin.String
@@ -56,12 +57,12 @@ public data class Household(
5657
val pets = jsonObject.get("pets")?.asJsonArray?.let { jsonArray ->
5758
val collection = ArrayList<Animal>(jsonArray.size())
5859
jsonArray.forEach {
59-
collection.add(Animal.fromJsonObject(it))
60+
collection.add(Animal.fromJsonElement(it))
6061
}
6162
collection
6263
}
63-
val situation = jsonObject.get("situation")?.asJsonObject?.let {
64-
Situation.fromJsonObject(it)
64+
val situation = jsonObject.get("situation")?.let {
65+
Situation.fromJsonElement(it)
6566
}
6667
return Household(pets, situation)
6768
} catch (e: IllegalStateException) {
@@ -202,8 +203,8 @@ public data class Household(
202203
@Throws(JsonParseException::class)
203204
public fun fromJson(jsonString: String): Animal {
204205
try {
205-
val jsonObject = JsonParser.parseString(jsonString).asJsonObject
206-
return fromJsonObject(jsonObject)
206+
val jsonElement = JsonParser.parseString(jsonString)
207+
return fromJsonElement(jsonElement)
207208
} catch (e: IllegalStateException) {
208209
throw JsonParseException(
209210
"Unable to parse json into one of type Animal",
@@ -214,7 +215,7 @@ public data class Household(
214215

215216
@JvmStatic
216217
@Throws(JsonParseException::class)
217-
public fun fromJsonObject(jsonElement: JsonElement): Animal {
218+
public fun fromJsonElement(jsonElement: JsonElement): Animal {
218219
val errors = mutableListOf<Throwable>()
219220
val asFish = try {
220221
if (jsonElement is JsonObject) {
@@ -371,13 +372,50 @@ public data class Household(
371372
}
372373
}
373374

375+
public data class Long(
376+
public val item: kotlin.Long,
377+
) : Situation() {
378+
override fun toJson(): JsonElement = JsonPrimitive(item)
379+
380+
public companion object {
381+
@JvmStatic
382+
@Throws(JsonParseException::class)
383+
public fun fromJson(jsonString: String): Long {
384+
val jsonElement = JsonParser.parseString(jsonString)
385+
try {
386+
return fromJsonPrimitive(jsonElement.asJsonPrimitive)
387+
} catch (e: IllegalStateException) {
388+
throw JsonParseException("Unable to parse json into type Long", e)
389+
}
390+
}
391+
392+
@JvmStatic
393+
@Throws(JsonParseException::class)
394+
public fun fromJsonPrimitive(jsonPrimitive: JsonPrimitive): Long {
395+
try {
396+
if (jsonPrimitive.isNumber) {
397+
return Long(jsonPrimitive.asLong)
398+
} else {
399+
throw JsonParseException("Can't convert jsonPrimitive to Long")
400+
}
401+
} catch (e: IllegalStateException) {
402+
throw JsonParseException("Unable to parse json into type Long", e)
403+
} catch (e: NumberFormatException) {
404+
throw JsonParseException("Unable to parse json into type Long", e)
405+
} catch (e: UnsupportedOperationException) {
406+
throw JsonParseException("Unable to parse json into type Long", e)
407+
}
408+
}
409+
}
410+
}
411+
374412
public companion object {
375413
@JvmStatic
376414
@Throws(JsonParseException::class)
377415
public fun fromJson(jsonString: String): Situation {
378416
try {
379-
val jsonObject = JsonParser.parseString(jsonString).asJsonObject
380-
return fromJsonObject(jsonObject)
417+
val jsonElement = JsonParser.parseString(jsonString)
418+
return fromJsonElement(jsonElement)
381419
} catch (e: IllegalStateException) {
382420
throw JsonParseException(
383421
"Unable to parse json into one of type Situation",
@@ -388,7 +426,7 @@ public data class Household(
388426

389427
@JvmStatic
390428
@Throws(JsonParseException::class)
391-
public fun fromJsonObject(jsonElement: JsonElement): Situation {
429+
public fun fromJsonElement(jsonElement: JsonElement): Situation {
392430
val errors = mutableListOf<Throwable>()
393431
val asMarriage = try {
394432
if (jsonElement is JsonObject) {
@@ -412,9 +450,21 @@ public data class Household(
412450
errors.add(e)
413451
null
414452
}
453+
val asLong = try {
454+
if (jsonElement is JsonPrimitive) {
455+
Long.fromJsonPrimitive(jsonElement)
456+
} else {
457+
throw JsonParseException("Unable to parse json into type "
458+
+ "kotlin.Long")
459+
}
460+
} catch (e: JsonParseException) {
461+
errors.add(e)
462+
null
463+
}
415464
val result = arrayOf(
416465
asMarriage,
417466
asCotenancy,
467+
asLong,
418468
).firstOrNull { it != null }
419469
if (result == null) {
420470
val message = "Unable to parse json into one of type \n" + "Situation\n" +

0 commit comments

Comments
 (0)