MoshiX

Extensions for Moshi including IR plugins, moshi-sealed, and more.

APACHE-2.0 License

Stars
490

Bot releases are hidden (Show)

MoshiX -

Published by ZacSweers over 2 years ago

Fix: Fix IR lookups of setOf() overloads. There are two setOf() functions with one arg - one
is the vararg and the other is a shorthand for Collections.singleton(element). It's important we
pick the right one, otherwise we can accidentally send a vararg array into the singleton()
function.

Dependency updates:

Kotlin 1.6.21
kotlinpoet 1.11.0
kotlinx-metadata 0.4.2
MoshiX -

Published by ZacSweers over 2 years ago

Fix: Fix support for nested sealed types that don't use @JsonClass.

MoshiX -

Published by ZacSweers over 2 years ago

New: moshi-sealed now supports nested sealed subtypes!

In some cases, it's useful to have more than one level of sealed types that share the same label key.

sealed interface Response {
  data class Success(val value: String) : Response
  sealed interface Failure : Response {
   data class ErrorMap(val errors: List<String>) : Failure
   data class ErrorString(val error: String) : Failure
  }
}

moshi-sealed now supports this out of the box via @NestedSealed annotation. Simply indicate the nested type with this
annotation.

@JsonClass(generateAdapter = true, generator = "sealed:type")
sealed interface Response {
  @TypeLabel("success")
  @JsonClass(generateAdapter = true)
  data class Success(val value: String) : Response

  @NestedSealed
  sealed interface Failure : Response {
    @TypeLabel("error_map")
    @JsonClass(generateAdapter = true)
    data class ErrorMap(val errors: List<String>) : Failure

    @TypeLabel("error_string")
    @JsonClass(generateAdapter = true)
    data class ErrorString(val error: String) : Failure
  }
}

In this case, now Failure's subtypes will also participate in Response decoding based on the type label key.

Caveats:

  • @DefaultObject is only supported on direct subtypes.
  • If you want to look up a subtype rather than the root parent sealed type (i.e. moshi.adapter<Response.Failure>()),
    you must add the optional NestedSealed.Factory JsonAdapter.Factory to your Moshi instance for runtime lookup.
    val moshi = Moshi.Builder()
      .add(NestedSealed.Factory())
      .build()
    

Kapt is no longer supported by moshi-sealed

moshi-sealed has many implementations - kotlin-reflect, kotlinx-metadata, KSP, Java sealed classes, and
recently IR. These are a lot to maintain! To cut down on maintenance, Kapt is no longer supported and has been removed
in this release. Please consider migrating to KSP or Moshi-IR.

Fix: Properly report all originating files in KSP

With Kotlin 1.5.0, sealed types could now exist across multiple files. moshi-sealed's KSP support previously assumed single files when reporting originating elements, and now properly reports all files if sealed types are spread across multiple files.

MoshiX -

Published by ZacSweers over 2 years ago

moshi-ir

  • Fix: Use FilesSubpluginOption to fix build cache relocatability when generating proguard rules.
MoshiX -

Published by ZacSweers over 2 years ago

moshi-ir

  • Fix: Nested type argument use in properties would fail in 0.16.5's new type rendering. This is now fixed.
    Example failing case would've been something like this:
    @JsonClass(generateAdapter = true)
    class Foo<T>(val value: List<T>)
    
MoshiX -

Published by ZacSweers over 2 years ago

  • Enhancement: Generate manual Type construction in moshi-ir adapter lookups. Prior to this, we generated IR
    code that leveraged typeOf(), but this appears to be too late to leverage compiler intrinsics support for it and
    appears to cause some issues if kotlin-reflect is on the classpath. This should improve runtime performance as a
    result.
MoshiX -

Published by ZacSweers almost 3 years ago

  • Fix: Add moshi/moshi-sealed-runtime dependencies as implementation rather than api when applying the
    moshi-ir plugin. #200
  • Fix: A second attempt at fixing extension point issues with AnalysisHandlerExtension in moshi-ir's proguard
    rule generation. #201

Thanks to @gpeal for contributing to this release!

MoshiX -

Published by ZacSweers almost 3 years ago

Fix: Build new type parameters when generating classes in moshi-ir rather than incorrectly reuse the original class's parameters. Resolves this issue (that was originally believed to be a Compose issue).

MoshiX -

Published by ZacSweers almost 3 years ago

  • Fix: Pass generateProguardRules Gradle plugin option correctly.
  • Fix: Best-effort avoid synchronization race with IntelliJ openapi when registering proguard rule gen extension
MoshiX -

Published by ZacSweers almost 3 years ago

  • moshi-ir now supports dynamic generation of proguard rules, bringing it to feature parity with Moshi's existing
    code gen.
    • Note that if you have any issues, this can be disabled via the Gradle extension's generateProguardRules
      property and using the manual rules mentioned in version 0.16.0's notes.
      moshi {
        generateProguardRules.set(false)
      }
      
  • New: To help with debugging moshi-ir, a new debug property is available in the Gradle extension. It is off
    by default and can be enabled like below. Please try this out and include its output when reporting issues. Thanks!
    moshi {
      debug.set(true)
    }
    
MoshiX -

Published by ZacSweers almost 3 years ago

New: moshi-ir

An experimental Kotlin IR implementation of Moshi code gen and moshi-sealed code gen.

The goal of this is to have functional parity with their native Kapt/KSP code gen analogues but run as a fully
embedded IR plugin.

Benefits

  • Significantly faster build times.
    • No extra Kapt or KSP tasks, no extra source files to compile. This runs directly in kotlinc and generates IR that is
      lowered directly into bytecode.
  • No reflection required at runtime to support default parameter values.
  • Feature parity with Moshi's native code gen.
  • More detailed error messages for unexpected null values and missing properties. Now all errors are accumulated and
    reported at the end, rather than failing eagerly with just the first one encountered.

Cons

  • No support for Proguard file generation for now #193. You will
    need to add this manually to your rules if you use R8/Proguard.
    • One option is to use IR in debug builds and Kapt/KSP in release builds, the latter of which do still generate
      proguard rules.
    # Keep names for JsonClass-annotated classes
    -keepnames @com.squareup.moshi.JsonClass class **
    
    # Keep generated adapter classes' constructors
    -keepclassmembers class *JsonAdapter {
        public <init>(...);
    }
    
  • Kotlin IR is not a stable API and may change in future Kotlin versions. While I'll try to publish quickly to adjust to
    these, you should be aware. If you have any issues, you can always fall back to Kapt/KSP.

Installation

Simply apply the Gradle plugin in your project to use it. You can enable moshi-sealed code gen via the moshi
extension.

The Gradle plugin is published to Maven Central, so ensure you have mavenCentral() visible to your buildscript
classpath.

Maven Central

plugins {
  kotlin("jvm")
  id("dev.zacsweers.moshix") version "x.y.z"
}

moshi {
  // Opt-in to enable moshi-sealed, disabled by default.
  enableSealed.set(true)
}

Other

  • Update to Kotlin 1.6.10
  • Update to KSP 1.6.10-1.0.2
MoshiX -

Published by ZacSweers almost 3 years ago

  • Update to Moshi 1.13.0
  • Removed: The moshi-ksp artifact has been upstreamed to Moshi itself as is no longer published.
  • Removed: The moshi-records-reflect artifact has been upstreamed to Moshi itself as is no longer published.
  • Update to Kotlin 1.6.0
  • Update to KotlinPoet 1.10.2
MoshiX -

Published by ZacSweers about 3 years ago

  • Build against JDK 17.
    • This means that moshi-sealed-java-sealed-reflect's support of sealed classes in Java is now out of preview and requires Java 17 to use.
    • moshi-records-reflect still targets Java 16 for maximum compatibility.
    • All other artifacts still target Java 8.
  • Update Kotlin to 1.5.31
  • Update KotlinPoet to 1.10.1
MoshiX -

Published by ZacSweers about 3 years ago

  • Update KSP to 1.5.30-1.0.0 stable!
  • moshi-sealed-ksp has now been merged into moshi-sealed-codegen. This artifact can be used for both kapt and
    ksp.
  • moshi-ksp is now soft-deprecated and will be fully deprecated once Moshi's next release is out with formal support.
MoshiX -

Published by ZacSweers about 3 years ago

  • Update Kotlin to 1.5.30.
  • Update KSP to 1.5.30-1.0.0-beta08.
  • Enhancement: RecordsJsonAdapterFactory is now aligned with the upstreamed implementation on Moshi itself.
    • Note that this is now soft-deprecated and will be fully deprecated once Moshi's next release is out with formal support.
    • This includes using a few more modern language APIs like MethodHandle and better unpacking of different runtime exceptions. Full details can be found in the PR.
  • Fix: Avoid implicitly converting elements to KotlinPoet in CodeBlocks to avoid noisy logging.
  • Fix: Improve self-referencing type variables parsing in moshi-ksp (see #125 and #151).

Special thanks to @yigit for contributing to this release!

MoshiX -

Published by ZacSweers about 3 years ago

  • Fix: RecordsJsonAdapterFactory now properly respects @JsonQualifier annotations on components.
  • Fix: RecordsJsonAdapterFactory now supports non-public constructors (i.e. package or file-private).
  • Fix: Crash in moshi-ksp when dealing with generic typealias properties.
MoshiX -

Published by ZacSweers about 3 years ago

  • Update to KSP 1.5.21-1.0.0-beta07.
  • Fix: Previously if you had a @JsonClass-annotated Java file with a custom generator, moshi-ksp would error
    out anyway due to it not being a Kotlin class. This is now fixed and it will safely ignore these files.
  • Fix: Generate missing @OptIn(ExperimentalStdLibApi::class) annotations in moshi-sealed when object
    adapters are used, as we use Moshi's reified addAdapter extension.

Thanks to @gabrielittner for contributing to this release!

MoshiX -

Published by ZacSweers over 3 years ago

  • Update to KSP 1.5.21-1.0.0-beta05.
  • Update to Kotlin 1.5.21.
  • Update to Dokka 1.5.0.
  • Update to KotlinPoet 1.9.0.
  • Test against JDK 17 early access previews.
  • New: moshi-ksp and moshi-sealed's codegen both support a new moshi.generateProguardRules option. This can be
    set to false to disable proguard rule generation.
  • Fix: Artifacts now ship with a -module-name attribute set to their artifact ID to help avoid module name
    collisions.

Thanks to @SeongUgJung and @slmlt for contributing to this release!

MoshiX -

Published by ZacSweers over 3 years ago

  • moshi-ksp - Fix a bug where supertypes compiled outside the current compilation weren't recognized as Kotlin types.
MoshiX -

Published by ZacSweers over 3 years ago

  • Update to KSP 1.5.10-1.0.0-beta01
  • Update to Kotlin 1.5.10
Package Rankings
Top 15.43% on Repo1.maven.org