Repository: 1Password/typeshare
Pull request #56 (2023-01-27 01:59:36)

Empty structs are now represented as `object`s in Kotlin
Follow-up PR to #51.


Patch:
diff --git a/Cargo.lock b/Cargo.lock
index 3208a36..db19325 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -60,9 +60,9 @@ dependencies = [
 
 [[package]]
 name = "bumpalo"
-version = "3.12.0"
+version = "3.11.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535"
+checksum = "c1ad822118d20d2c234f427000d5acc36eabe1e29a348c89b63dd60b13f28e5d"
 
 [[package]]
 name = "cfg-if"
diff --git a/core/src/language/kotlin.rs b/core/src/language/kotlin.rs
index c371929..a83589b 100644
--- a/core/src/language/kotlin.rs
+++ b/core/src/language/kotlin.rs
@@ -103,7 +103,8 @@ impl Language for Kotlin {
         writeln!(w, "@Serializable")?;
 
         if rs.fields.is_empty() {
-            writeln!(w, "class {}\n", rs.id.renamed)?;
+            // If the struct has no fields, we can define it as an static object.
+            writeln!(w, "object {}\n", rs.id.renamed)?;
         } else {
             writeln!(
                 w,


Test patch:
diff --git a/core/data/tests/can_generate_algebraic_enum/output.kt b/core/data/tests/can_generate_algebraic_enum/output.kt
index 988b7b2..f30ff39 100644
--- a/core/data/tests/can_generate_algebraic_enum/output.kt
+++ b/core/data/tests/can_generate_algebraic_enum/output.kt
@@ -11,7 +11,7 @@ import kotlinx.serialization.*
 
 /// Struct comment
 @Serializable
-class ItemDetailsFieldValue
+object ItemDetailsFieldValue
 
 /// Enum comment
 @Serializable
diff --git a/core/data/tests/can_generate_empty_algebraic_enum/output.kt b/core/data/tests/can_generate_empty_algebraic_enum/output.kt
index 24262f5..d1a4836 100644
--- a/core/data/tests/can_generate_empty_algebraic_enum/output.kt
+++ b/core/data/tests/can_generate_empty_algebraic_enum/output.kt
@@ -10,7 +10,7 @@ import androidx.compose.runtime.NoLiveLiterals
 import kotlinx.serialization.*
 
 @Serializable
-class AddressDetails
+object AddressDetails
 
 @Serializable
 sealed class Address {
diff --git a/core/data/tests/can_generate_simple_struct_with_a_comment/output.kt b/core/data/tests/can_generate_simple_struct_with_a_comment/output.kt
index 78166b6..074a546 100644
--- a/core/data/tests/can_generate_simple_struct_with_a_comment/output.kt
+++ b/core/data/tests/can_generate_simple_struct_with_a_comment/output.kt
@@ -10,7 +10,7 @@ import androidx.compose.runtime.NoLiveLiterals
 import kotlinx.serialization.*
 
 @Serializable
-class Location
+object Location
 
 /// This is a comment.
 @Serializable
diff --git a/core/data/tests/can_generate_unit_structs/output.kt b/core/data/tests/can_generate_unit_structs/output.kt
index 45242c6..38f14c9 100644
--- a/core/data/tests/can_generate_unit_structs/output.kt
+++ b/core/data/tests/can_generate_unit_structs/output.kt
@@ -10,5 +10,5 @@ import androidx.compose.runtime.NoLiveLiterals
 import kotlinx.serialization.*
 
 @Serializable
-class UnitStruct
+object UnitStruct
 
diff --git a/core/data/tests/can_handle_serde_rename/output.kt b/core/data/tests/can_handle_serde_rename/output.kt
index bce25b0..fa44f8b 100644
--- a/core/data/tests/can_handle_serde_rename/output.kt
+++ b/core/data/tests/can_handle_serde_rename/output.kt
@@ -10,7 +10,7 @@ import androidx.compose.runtime.NoLiveLiterals
 import kotlinx.serialization.*
 
 @Serializable
-class OtherType
+object OtherType
 
 /// This is a comment.
 @Serializable
diff --git a/core/data/tests/can_handle_serde_rename_on_top_level/output.kt b/core/data/tests/can_handle_serde_rename_on_top_level/output.kt
index 3703ca8..c221bda 100644
--- a/core/data/tests/can_handle_serde_rename_on_top_level/output.kt
+++ b/core/data/tests/can_handle_serde_rename_on_top_level/output.kt
@@ -10,7 +10,7 @@ import androidx.compose.runtime.NoLiveLiterals
 import kotlinx.serialization.*
 
 @Serializable
-class OtherType
+object OtherType
 
 /// This is a comment.
 @Serializable
diff --git a/core/data/tests/generate_types/output.kt b/core/data/tests/generate_types/output.kt
index 543fcbb..eb56b68 100644
--- a/core/data/tests/generate_types/output.kt
+++ b/core/data/tests/generate_types/output.kt
@@ -10,7 +10,7 @@ import androidx.compose.runtime.NoLiveLiterals
 import kotlinx.serialization.*
 
 @Serializable
-class CustomType
+object CustomType
 
 @Serializable
 data class Types (
diff --git a/core/data/tests/generates_empty_structs_and_initializers/output.kt b/core/data/tests/generates_empty_structs_and_initializers/output.kt
index 35ae9d2..03c8f36 100644
--- a/core/data/tests/generates_empty_structs_and_initializers/output.kt
+++ b/core/data/tests/generates_empty_structs_and_initializers/output.kt
@@ -10,5 +10,5 @@ import androidx.compose.runtime.NoLiveLiterals
 import kotlinx.serialization.*
 
 @Serializable
-class MyEmptyStruct
+object MyEmptyStruct
 
diff --git a/core/data/tests/test_algebraic_enum_case_name_support/output.kt b/core/data/tests/test_algebraic_enum_case_name_support/output.kt
index 07608fe..5aad7f2 100644
--- a/core/data/tests/test_algebraic_enum_case_name_support/output.kt
+++ b/core/data/tests/test_algebraic_enum_case_name_support/output.kt
@@ -10,7 +10,7 @@ import androidx.compose.runtime.NoLiveLiterals
 import kotlinx.serialization.*
 
 @Serializable
-class ItemDetailsFieldValue
+object ItemDetailsFieldValue
 
 @Serializable
 sealed class AdvancedColors {
diff --git a/core/data/tests/use_correct_decoded_variable_name/output.kt b/core/data/tests/use_correct_decoded_variable_name/output.kt
index 35ae9d2..03c8f36 100644
--- a/core/data/tests/use_correct_decoded_variable_name/output.kt
+++ b/core/data/tests/use_correct_decoded_variable_name/output.kt
@@ -10,5 +10,5 @@ import androidx.compose.runtime.NoLiveLiterals
 import kotlinx.serialization.*
 
 @Serializable
-class MyEmptyStruct
+object MyEmptyStruct