Skip to content

Commit

Permalink
Merge pull request #82 from macisamuele/maci-allow-codegeneated-moshi…
Browse files Browse the repository at this point in the history
…-adaapters

Allow Code-geneated Moshi Adapters
  • Loading branch information
macisamuele committed Jan 27, 2020
2 parents b81d32c + bb3694d commit bb15a09
Show file tree
Hide file tree
Showing 27 changed files with 182 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.iml
.*
.gradle
.idea/
build/
java_pid*.hprof
local.properties
Expand Down
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,20 @@ We're looking forward to more platforms to support in the future. Contributions

You can find some **examples** in this repository to help you set up your generator environment.

* [samples/generated-code](/samples/generated-code) Contains an example of an Android Library where the code is generated inside the `/scr/main/java` folder. You can use this example to see how the generated code will _look like_ (like [here](/samples/generated-code/src/main/java/com/yelp/codegen/generatecodesamples/apis/DefaultApi.kt))
* [samples/generated-code](/samples/generated-code)<sup>*</sup> Contains an example of an Android Library where the code is generated inside the `/scr/main/java` folder. You can use this example to see how the generated code will _look like_ (like [here](/samples/generated-code/src/main/java/com/yelp/codegen/generatecodesamples/apis/DefaultApi.kt)).

* [samples/groovy-android](/samples/groovy-android) Contains an example of an Android Library configured with a `build.gradle` file, using the classical Gradle/Groovy as scripting language.
* [samples/groovy-android](/samples/groovy-android)<sup>*</sup> Contains an example of an Android Library configured with a `build.gradle` file, using the classical Gradle/Groovy as scripting language.

* [samples/kotlin-android](/samples/kotlin-android) Contains an example of an Android Library configured with a `build.gradle.kts` file, using Kotlin as scripting language.
* [samples/kotlin-android](/samples/kotlin-android)<sup>*</sup> Contains an example of an Android Library configured with a `build.gradle.kts` file, using Kotlin as scripting language.

* [samples/kotlin-coroutines](/samples/kotlin-coroutines) Contains an example of an Android Library configured to output Kotlin Coroutines capable code.
* [samples/kotlin-android-moshi-codegen](/samples/kotlin-android-moshi-codegen) Based on samples/kotlin-android but the configuration is using [`moshi-codegen`](https://github.com/square/moshi#codegen) for improved parsing performances and it defines proguard rules.

* [samples/kotlin-coroutines](/samples/kotlin-coroutines)<sup>*</sup> Contains an example of an Android Library configured to output Kotlin Coroutines capable code.

* [samples/junit-tests](/samples/junit-tests) This sample contains specs used to test edge cases and scenarios that have been reported in the issue tracker or that are worth testing. Tests are executed using [`moshi-codegen`](https://github.com/square/moshi#codegen).

<sup>*</sup>: The project is using [`moshi-reflect`](https://github.com/square/moshi#reflection) to convert your Kotlin classes to and from JSON.

* [samples/junit-tests](/samples/junit-tests) This sample contains specs used to test edge cases and scenarios that have been reported in the issue tracker or that are worth testing.

## How the generated code will look like

Expand Down
1 change: 1 addition & 0 deletions plugin/src/main/java/com/yelp/codegen/KotlinGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ open class KotlinGenerator : SharedCodegen() {
// If there are any vars, we will mark them with the @Json annotation so we have to make sure to import it
if (codegenModel.vars.isNotEmpty() || codegenModel.isEnum) {
codegenModel.imports.add("com.squareup.moshi.Json")
codegenModel.imports.add("com.squareup.moshi.JsonClass")
}

// Add import for @XNullable annotation if there are any XNullable properties
Expand Down
4 changes: 3 additions & 1 deletion plugin/src/main/resources/kotlin/data_class.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* @property {{{name}}} {{description}}
{{/vars}}
*/
{{#hasVars}}data {{/hasVars}}class {{classname}} {{#hasVars}}(
{{#hasVars}}@JsonClass(generateAdapter = true)
data {{/hasVars}}class {{classname}} {{#hasVars}}(
{{#requiredVars}}
{{>data_class_req_var}}{{^-last}},
{{/-last}}{{/requiredVars}}{{#hasRequired}}{{#hasOptional}},
Expand All @@ -17,6 +18,7 @@
* {{{description}}}
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}
*/
@JsonClass(generateAdapter = false)
enum class {{enumName}}(val value: {{complexType}}){ {{#allowableValues}}{{#enumVars}}
@Json(name = {{{value}}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}}{{/-last}}{{/enumVars}}{{/allowableValues}}
}
Expand Down
1 change: 1 addition & 0 deletions plugin/src/main/resources/kotlin/enum_class.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* {{{description}}}
* Values: {{#allowableValues}}{{#enumVars}}{{&name}}{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}}
*/
@JsonClass(generateAdapter = false)
enum class {{classname}}(val value: {{dataType}}){
{{#allowableValues}}{{#enumVars}} @Json(name = {{{value}}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}
{{/enumVars}}{{/allowableValues}}}
1 change: 1 addition & 0 deletions samples/junit-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/src/main/java
2 changes: 2 additions & 0 deletions samples/junit-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ apply plugin: "com.android.library"
apply plugin: "kotlin-android"
apply plugin: "com.yelp.codegen.plugin"
apply plugin: "io.gitlab.arturbosch.detekt"
apply plugin: "kotlin-kapt"

android {
compileSdkVersion = 28
Expand All @@ -43,6 +44,7 @@ dependencies {
implementation "com.squareup.retrofit2:retrofit:2.7.1"
implementation "com.squareup.retrofit2:converter-moshi:2.7.1"
implementation "com.squareup.retrofit2:adapter-rxjava2:2.7.1"
kapt "com.squareup.moshi:moshi-kotlin-codegen:1.9.2"

// Date Support
implementation "com.jakewharton.threetenabp:threetenabp:1.2.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import org.threeten.bp.LocalDate
import org.threeten.bp.ZonedDateTime

Expand All @@ -16,6 +17,7 @@ import org.threeten.bp.ZonedDateTime
* @property datetimeProperty
* @property enumProperty
*/
@JsonClass(generateAdapter = true)
data class FormatResponses(
@Json(name = "date_property") @field:Json(name = "date_property") var dateProperty: LocalDate? = null,
@Json(name = "datetime_property") @field:Json(name = "datetime_property") var datetimeProperty: ZonedDateTime? = null,
Expand All @@ -25,6 +27,7 @@ data class FormatResponses(
*
* Values: VALUE1, VALUE2
*/
@JsonClass(generateAdapter = false)
enum class EnumPropertyEnum(val value: String) {
@Json(name = "VALUE1") VALUE1("VALUE1"),
@Json(name = "VALUE2") VALUE2("VALUE2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import java.math.BigDecimal

/**
*
* @property numberArray
* @property stringArray
*/
@JsonClass(generateAdapter = true)
data class PropertyArray(
@Json(name = "number_array") @field:Json(name = "number_array") var numberArray: List<BigDecimal>? = null,
@Json(name = "string_array") @field:Json(name = "string_array") var stringArray: List<String>? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import java.math.BigDecimal

/**
Expand All @@ -15,6 +16,7 @@ import java.math.BigDecimal
* @property objectMap
* @property stringMap
*/
@JsonClass(generateAdapter = true)
data class PropertyMap(
@Json(name = "number_map") @field:Json(name = "number_map") var numberMap: Map<String, BigDecimal>? = null,
@Json(name = "object_map") @field:Json(name = "object_map") var objectMap: Map<String, Any?>? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import java.math.BigDecimal

/**
Expand All @@ -17,6 +18,7 @@ import java.math.BigDecimal
* @property numberProperty
* @property stringProperty
*/
@JsonClass(generateAdapter = true)
data class RequiredTypeResponses(
@Json(name = "boolean_property") @field:Json(name = "boolean_property") var booleanProperty: Boolean,
@Json(name = "enum_property") @field:Json(name = "enum_property") var enumProperty: RequiredTypeResponses.EnumPropertyEnum,
Expand All @@ -28,6 +30,7 @@ data class RequiredTypeResponses(
*
* Values: VALUE1, VALUE2
*/
@JsonClass(generateAdapter = false)
enum class EnumPropertyEnum(val value: String) {
@Json(name = "VALUE1") VALUE1("VALUE1"),
@Json(name = "VALUE2") VALUE2("VALUE2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

/**
*
Expand All @@ -17,6 +18,7 @@ import com.squareup.moshi.Json
* @property `var`
* @property `when`
*/
@JsonClass(generateAdapter = true)
data class ReservedKeywords(
@Json(name = "class") @field:Json(name = "class") var `class`: String? = null,
@Json(name = "for") @field:Json(name = "for") var `for`: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

/**
*
* Values: VALUE1,VALUE2
*/
@JsonClass(generateAdapter = false)
enum class TopLevelEnum(val value: String) {
@Json(name = "TOP_LEVEL_VALUE1") VALUE1("TOP_LEVEL_VALUE1"),
@Json(name = "TOP_LEVEL_VALUE2") VALUE2("TOP_LEVEL_VALUE2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import java.math.BigDecimal

/**
Expand All @@ -16,6 +17,7 @@ import java.math.BigDecimal
* @property numberProperty
* @property stringProperty
*/
@JsonClass(generateAdapter = true)
data class TypeResponses(
@Json(name = "boolean_property") @field:Json(name = "boolean_property") var booleanProperty: Boolean? = null,
@Json(name = "integer_property") @field:Json(name = "integer_property") var integerProperty: Int? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import com.yelp.codegen.generatecodesamples.tools.XNullable
import org.threeten.bp.LocalDate
import org.threeten.bp.ZonedDateTime
Expand All @@ -17,6 +18,7 @@ import org.threeten.bp.ZonedDateTime
* @property datetimeProperty
* @property doubleProperty
*/
@JsonClass(generateAdapter = true)
data class XnullableFormatResponses(
@Json(name = "date_property") @field:Json(name = "date_property") @XNullable var dateProperty: LocalDate? = null,
@Json(name = "datetime_property") @field:Json(name = "datetime_property") @XNullable var datetimeProperty: ZonedDateTime? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import com.yelp.codegen.generatecodesamples.tools.XNullable
import java.math.BigDecimal

Expand All @@ -15,6 +16,7 @@ import java.math.BigDecimal
* @property numberArray
* @property stringArray
*/
@JsonClass(generateAdapter = true)
data class XnullablePropertyArray(
@Json(name = "number_array") @field:Json(name = "number_array") @XNullable var numberArray: List<BigDecimal?>? = null,
@Json(name = "string_array") @field:Json(name = "string_array") @XNullable var stringArray: List<String?>? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import com.yelp.codegen.generatecodesamples.tools.XNullable
import java.math.BigDecimal

Expand All @@ -16,6 +17,7 @@ import java.math.BigDecimal
* @property objectMap
* @property stringMap
*/
@JsonClass(generateAdapter = true)
data class XnullablePropertyMap(
@Json(name = "number_map") @field:Json(name = "number_map") @XNullable var numberMap: Map<String, BigDecimal?>? = null,
@Json(name = "object_map") @field:Json(name = "object_map") @XNullable var objectMap: Map<String, Any?>? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import com.yelp.codegen.generatecodesamples.tools.XNullable
import java.math.BigDecimal

Expand All @@ -15,6 +16,7 @@ import java.math.BigDecimal
* @property numberArray
* @property stringArray
*/
@JsonClass(generateAdapter = true)
data class XnullableRequiredPropertyArray(
@Json(name = "number_array") @field:Json(name = "number_array") @XNullable var numberArray: List<BigDecimal?>? = null,
@Json(name = "string_array") @field:Json(name = "string_array") @XNullable var stringArray: List<String?>? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import com.yelp.codegen.generatecodesamples.tools.XNullable
import java.math.BigDecimal

Expand All @@ -16,6 +17,7 @@ import java.math.BigDecimal
* @property objectMap
* @property stringMap
*/
@JsonClass(generateAdapter = true)
data class XnullableRequiredPropertyMap(
@Json(name = "number_map") @field:Json(name = "number_map") @XNullable var numberMap: Map<String, BigDecimal?>? = null,
@Json(name = "object_map") @field:Json(name = "object_map") @XNullable var objectMap: Map<String, Any?>? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import com.yelp.codegen.generatecodesamples.tools.XNullable
import java.math.BigDecimal

Expand All @@ -18,6 +19,7 @@ import java.math.BigDecimal
* @property numberProperty
* @property stringProperty
*/
@JsonClass(generateAdapter = true)
data class XnullableRequiredTypeResponses(
@Json(name = "boolean_property") @field:Json(name = "boolean_property") @XNullable var booleanProperty: Boolean? = null,
@Json(name = "enum_property") @field:Json(name = "enum_property") @XNullable var enumProperty: XnullableRequiredTypeResponses.EnumPropertyEnum? = null,
Expand All @@ -29,6 +31,7 @@ data class XnullableRequiredTypeResponses(
*
* Values: VALUE1, VALUE2
*/
@JsonClass(generateAdapter = false)
enum class EnumPropertyEnum(val value: String) {
@Json(name = "VALUE1") VALUE1("VALUE1"),
@Json(name = "VALUE2") VALUE2("VALUE2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import com.yelp.codegen.generatecodesamples.tools.XNullable
import java.math.BigDecimal

Expand All @@ -18,6 +19,7 @@ import java.math.BigDecimal
* @property numberProperty
* @property stringProperty
*/
@JsonClass(generateAdapter = true)
data class XnullableTypeResponses(
@Json(name = "boolean_property") @field:Json(name = "boolean_property") @XNullable var booleanProperty: Boolean? = null,
@Json(name = "enum_property") @field:Json(name = "enum_property") @XNullable var enumProperty: XnullableTypeResponses.EnumPropertyEnum? = null,
Expand All @@ -29,6 +31,7 @@ data class XnullableTypeResponses(
*
* Values: VALUE1, VALUE2
*/
@JsonClass(generateAdapter = false)
enum class EnumPropertyEnum(val value: String) {
@Json(name = "VALUE1") VALUE1("VALUE1"),
@Json(name = "VALUE2") VALUE2("VALUE2")
Expand Down
1 change: 1 addition & 0 deletions samples/kotlin-android-moshi-codegen/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/src/main/java
Loading

0 comments on commit bb15a09

Please sign in to comment.