-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generate json converter functions for krotoDC dataclasses (#25)
* generate json converter functions for krotoDC dataclasses * add `protobuf-java-util` dependency to README
- Loading branch information
1 parent
20d147f
commit 6118696
Showing
7 changed files
with
146 additions
and
0 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
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
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
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
53 changes: 53 additions & 0 deletions
53
.../kotlin/io/github/mscheong01/krotodc/specgenerators/function/FromJsonFunctionGenerator.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,53 @@ | ||
// Copyright 2024 Minsoo Cheong | ||
// | ||
// 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 io.github.mscheong01.krotodc.specgenerators.function | ||
|
||
import com.google.protobuf.Descriptors.Descriptor | ||
import com.squareup.kotlinpoet.ClassName | ||
import com.squareup.kotlinpoet.FunSpec | ||
import com.squareup.kotlinpoet.ParameterSpec | ||
import io.github.mscheong01.krotodc.import.FunSpecsWithImports | ||
import io.github.mscheong01.krotodc.import.Import | ||
import io.github.mscheong01.krotodc.specgenerators.FunSpecGenerator | ||
import io.github.mscheong01.krotodc.util.krotoDCTypeName | ||
import io.github.mscheong01.krotodc.util.protobufJavaTypeName | ||
|
||
class FromJsonFunctionGenerator : FunSpecGenerator<Descriptor> { | ||
override fun generate(descriptor: Descriptor): FunSpecsWithImports { | ||
val imports = mutableSetOf<Import>() | ||
val generatedType = descriptor.krotoDCTypeName | ||
val generatedTypeCompanion = ClassName( | ||
generatedType.packageName, | ||
*generatedType.simpleNames.toTypedArray(), | ||
"Companion" | ||
) | ||
val protoType = descriptor.protobufJavaTypeName | ||
val functionBuilder = FunSpec.builder("fromJson") | ||
.addParameter( | ||
ParameterSpec.builder( | ||
"json", | ||
String::class | ||
).build() | ||
) | ||
.receiver(generatedTypeCompanion) | ||
.returns(generatedType) | ||
functionBuilder.addCode("return %L.newBuilder().apply { JsonFormat.parser().ignoringUnknownFields().merge(json, this@apply) }.build().toDataClass();\n", protoType) | ||
|
||
imports.add(Import("com.google.protobuf.util", listOf("JsonFormat"))) | ||
return FunSpecsWithImports( | ||
listOf(functionBuilder.build()), | ||
imports | ||
) | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...in/kotlin/io/github/mscheong01/krotodc/specgenerators/function/ToJsonFunctionGenerator.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,37 @@ | ||
// Copyright 2024 Minsoo Cheong | ||
// | ||
// 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 io.github.mscheong01.krotodc.specgenerators.function | ||
|
||
import com.google.protobuf.Descriptors.Descriptor | ||
import com.squareup.kotlinpoet.FunSpec | ||
import io.github.mscheong01.krotodc.import.FunSpecsWithImports | ||
import io.github.mscheong01.krotodc.import.Import | ||
import io.github.mscheong01.krotodc.specgenerators.FunSpecGenerator | ||
import io.github.mscheong01.krotodc.util.krotoDCTypeName | ||
|
||
class ToJsonFunctionGenerator : FunSpecGenerator<Descriptor> { | ||
override fun generate(descriptor: Descriptor): FunSpecsWithImports { | ||
val imports = mutableSetOf<Import>() | ||
val generatedType = descriptor.krotoDCTypeName | ||
val functionBuilder = FunSpec.builder("toJson") | ||
.receiver(generatedType) | ||
.returns(String::class) | ||
functionBuilder.addCode("return JsonFormat.printer().print([email protected]())") | ||
imports.add(Import("com.google.protobuf.util", listOf("JsonFormat"))) | ||
return FunSpecsWithImports( | ||
listOf(functionBuilder.build()), | ||
imports | ||
) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
generator/src/test/kotlin/io/github/mscheong01/krotodc/JsonConverterTest.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,39 @@ | ||
// Copyright 2024 Minsoo Cheong | ||
// | ||
// 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 io.github.mscheong01.krotodc | ||
|
||
import io.github.mscheong01.test.Person | ||
import io.github.mscheong01.test.krotodc.person.fromJson | ||
import io.github.mscheong01.test.krotodc.person.toDataClass | ||
import io.github.mscheong01.test.krotodc.person.toJson | ||
import org.assertj.core.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
|
||
class JsonConverterTest { | ||
|
||
@Test | ||
fun `test simple message`() { | ||
val proto = Person.newBuilder() | ||
.setName("John") | ||
.setAge(30) | ||
.build() | ||
val dataClass = proto.toDataClass() | ||
val json = dataClass.toJson() | ||
Assertions.assertThat(json).isEqualTo( | ||
"{\n \"name\": \"John\",\n \"age\": 30\n}" | ||
) | ||
val deserializedDataClass = io.github.mscheong01.test.krotodc.Person.fromJson(json) | ||
Assertions.assertThat(dataClass).isEqualTo(deserializedDataClass) | ||
} | ||
} |