Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reading property function types #1311

Merged
merged 4 commits into from
Mar 14, 2021

Conversation

ZacSweers
Copy link
Collaborator

@ZacSweers ZacSweers commented Mar 1, 2021

Resolves #1265. This also moves @Suppress to be a file:-level suppression due to the kotlin.functions.* imports. We support simple function types as well as FunctionN types for functions with 23+ parameters.

Now for this snippet

@JsonClass(generateAdapter = true)
data class LambdaTypeName(
  val id: String,
  @Transient
  val custom: ((String) -> Boolean)? = null
)

We generate this:

// Code generated by moshi-kotlin-codegen. Do not edit.
@file:Suppress("DEPRECATION", "unused", "ClassName", "REDUNDANT_PROJECTION",
    "RedundantExplicitType", "LocalVariableName", "RedundantVisibilityModifier",
    "PLATFORM_CLASS_MAPPED_TO_KOTLIN")

package com.squareup.moshi.kotlin.codegen

import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import com.squareup.moshi.`internal`.Util
import java.lang.NullPointerException
import java.lang.reflect.Constructor
import kotlin.Int
import kotlin.String
import kotlin.Suppress
import kotlin.Unit
import kotlin.collections.emptySet
import kotlin.jvm.Volatile
import kotlin.jvm.functions.Function1
import kotlin.text.buildString

public class LambdaTypeNameJsonAdapter(
  moshi: Moshi
) : JsonAdapter<LambdaTypeName>() {
  private val options: JsonReader.Options = JsonReader.Options.of("id")

  private val stringAdapter: JsonAdapter<String> = moshi.adapter(String::class.java, emptySet(),
      "id")

  @Volatile
  private var constructorRef: Constructor<LambdaTypeName>? = null

  public override fun toString(): String = buildString(36) {
      append("GeneratedJsonAdapter(").append("LambdaTypeName").append(')') }

  public override fun fromJson(reader: JsonReader): LambdaTypeName {
    var id: String? = null
    var mask0 = -1
    reader.beginObject()
    while (reader.hasNext()) {
      when (reader.selectName(options)) {
        0 -> id = stringAdapter.fromJson(reader) ?: throw Util.unexpectedNull("id", "id", reader)
        -1 -> {
          // Unknown name, skip it.
          reader.skipName()
          reader.skipValue()
        }
      }
    }
    reader.endObject()
    @Suppress("UNCHECKED_CAST")
    val localConstructor: Constructor<LambdaTypeName> = this.constructorRef ?:
        LambdaTypeName::class.java.getDeclaredConstructor(String::class.java, Function1::class.java,
        Int::class.javaPrimitiveType, Util.DEFAULT_CONSTRUCTOR_MARKER).also { this.constructorRef =
        it }
    return localConstructor.newInstance(
        id ?: throw Util.missingProperty("id", "id", reader),
        null,
        mask0,
        null
    )
  }

  public override fun toJson(writer: JsonWriter, value: LambdaTypeName?): Unit {
    if (value == null) {
      throw NullPointerException("value was null! Wrap in .nullSafe() to write nullable values.")
    }
    writer.beginObject()
    writer.name("id")
    stringAdapter.toJson(writer, value.id)
    writer.endObject()
  }
}

Copy link
Collaborator

@Egorand Egorand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@ZacSweers ZacSweers merged commit 3bc4751 into square:master Mar 14, 2021
@ZacSweers ZacSweers deleted the z/deepCopyLambdaTypeName branch March 14, 2021 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants