[go: nahoru, domu]

Make argument default params last

When generating an argument in kotlin from safe args, default parameters
should always come after none defaults in the argument's constructor.

RelNote: "When generating arguments, safe args now puts parameters
without default values before those with default values."
Test: ./gradlew --rerun-tasks
navigation:navigation-safe-args-generator:test
Bug: 198493585

Change-Id: I8970968c48deb3a1437437df9a3db696ed497372
diff --git a/navigation/navigation-safe-args-generator/src/main/kotlin/androidx/navigation/safe/args/generator/kotlin/KotlinNavWriter.kt b/navigation/navigation-safe-args-generator/src/main/kotlin/androidx/navigation/safe/args/generator/kotlin/KotlinNavWriter.kt
index 46cd09b..1439810 100644
--- a/navigation/navigation-safe-args-generator/src/main/kotlin/androidx/navigation/safe/args/generator/kotlin/KotlinNavWriter.kt
+++ b/navigation/navigation-safe-args-generator/src/main/kotlin/androidx/navigation/safe/args/generator/kotlin/KotlinNavWriter.kt
@@ -200,7 +200,7 @@
                         name = arg.sanitizedName,
                         type = arg.type.typeName().copy(nullable = arg.isNullable)
                     ).apply { arg.defaultValue?.let { defaultValue(it.write()) } }.build()
-                }
+                }.sortedBy { it.defaultValue != null }
             )
             .build()
 
diff --git a/navigation/navigation-safe-args-generator/src/test/test-data/expected/kotlin_nav_writer_test/MainFragmentArgs.kt b/navigation/navigation-safe-args-generator/src/test/test-data/expected/kotlin_nav_writer_test/MainFragmentArgs.kt
index 1ca1ba9..95ca60f 100644
--- a/navigation/navigation-safe-args-generator/src/test/test-data/expected/kotlin_nav_writer_test/MainFragmentArgs.kt
+++ b/navigation/navigation-safe-args-generator/src/test/test-data/expected/kotlin_nav_writer_test/MainFragmentArgs.kt
@@ -20,12 +20,12 @@
 
 public data class MainFragmentArgs(
   public val main: String,
+  public val floatArrayArg: FloatArray,
+  public val objectArrayArg: Array<ActivityInfo>,
   public val optional: Int = -1,
   public val reference: Int = R.drawable.background,
   public val referenceZeroDefaultValue: Int = 0,
   public val floatArg: Float = 1F,
-  public val floatArrayArg: FloatArray,
-  public val objectArrayArg: Array<ActivityInfo>,
   public val boolArg: Boolean = true,
   public val optionalParcelable: ActivityInfo? = null,
   public val enumArg: AccessMode = AccessMode.READ