Skip to content

Commit

Permalink
Merge pull request #159 from smohammad/saad/kotlin-prefix
Browse files Browse the repository at this point in the history
Support prefixing Kotlin type names
  • Loading branch information
Cerulan Lumina authored Apr 2, 2024
2 parents 631bf70 + ea964ec commit 5a3cbdd
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 32 deletions.
1 change: 1 addition & 0 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const DEFAULT_CONFIG_FILE_NAME: &str = "typeshare.toml";
pub struct KotlinParams {
pub package: String,
pub module_name: String,
pub prefix: String,
pub type_mappings: HashMap<String, String>,
}

Expand Down
14 changes: 14 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const VERSION: &str = env!("CARGO_PKG_VERSION");

const ARG_TYPE: &str = "TYPE";
const ARG_SWIFT_PREFIX: &str = "SWIFTPREFIX";
const ARG_KOTLIN_PREFIX: &str = "KOTLINPREFIX";
const ARG_JAVA_PACKAGE: &str = "JAVAPACKAGE";
const ARG_MODULE_NAME: &str = "MODULENAME";
const ARG_SCALA_PACKAGE: &str = "SCALAPACKAGE";
Expand Down Expand Up @@ -72,6 +73,14 @@ fn build_command() -> Command<'static> {
.takes_value(true)
.required(false),
)
.arg(
Arg::new(ARG_KOTLIN_PREFIX)
.short('k')
.long("kotlin-prefix")
.help("Prefix for generated Kotlin types")
.takes_value(true)
.required(false),
)
.arg(
Arg::new(ARG_JAVA_PACKAGE)
.short('j')
Expand Down Expand Up @@ -202,6 +211,7 @@ fn main() {
Some(SupportedLanguage::Kotlin) => Box::new(Kotlin {
package: config.kotlin.package,
module_name: config.kotlin.module_name,
prefix: config.kotlin.prefix,
type_mappings: config.kotlin.type_mappings,
..Default::default()
}),
Expand Down Expand Up @@ -318,6 +328,10 @@ fn override_configuration(mut config: Config, options: &ArgMatches) -> Config {
config.swift.prefix = swift_prefix.to_string();
}

if let Some(kotlin_prefix) = options.value_of(ARG_KOTLIN_PREFIX) {
config.kotlin.prefix = kotlin_prefix.to_string();
}

if let Some(java_package) = options.value_of(ARG_JAVA_PACKAGE) {
config.kotlin.package = java_package.to_string();
}
Expand Down
11 changes: 2 additions & 9 deletions core/data/tests/can_apply_prefix_correctly/output.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
@file:NoLiveLiterals

package com.agilebits.onepassword

import androidx.compose.runtime.NoLiveLiterals
import kotlinx.serialization.*

@Serializable
data class ItemDetailsFieldValue (
data class OPItemDetailsFieldValue (
val hello: String
)

@Serializable
sealed class AdvancedColors {
sealed class OPAdvancedColors {
@Serializable
@SerialName("String")
data class String(val c: String): AdvancedColors()
Expand Down
11 changes: 2 additions & 9 deletions core/data/tests/can_generate_empty_algebraic_enum/output.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
@file:NoLiveLiterals

package com.agilebits.onepassword

import androidx.compose.runtime.NoLiveLiterals
import kotlinx.serialization.*

@Serializable
object AddressDetails
object OPAddressDetails

@Serializable
sealed class Address {
sealed class OPAddress {
@Serializable
@SerialName("FixedAddress")
data class FixedAddress(val content: AddressDetails): Address()
Expand Down
26 changes: 14 additions & 12 deletions core/src/language/kotlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub struct Kotlin {
pub package: String,
/// Name of the Kotlin module
pub module_name: String,
/// The prefix to append to user-defined types
pub prefix: String,
/// Conversions from Rust type names to Kotlin type names.
pub type_mappings: HashMap<String, String>,
/// Whether or not to exclude the version header that normally appears at the top of generated code.
Expand Down Expand Up @@ -97,11 +99,12 @@ impl Language for Kotlin {

fn write_type_alias(&mut self, w: &mut dyn Write, ty: &RustTypeAlias) -> std::io::Result<()> {
self.write_comments(w, 0, &ty.comments)?;
let type_name = format!("{}{}", &self.prefix, ty.id.original);

writeln!(
w,
"typealias {}{} = {}\n",
ty.id.original,
type_name,
(!ty.generic_types.is_empty())
.then(|| format!("<{}>", ty.generic_types.join(", ")))
.unwrap_or_default(),
Expand All @@ -118,11 +121,12 @@ impl Language for Kotlin {

if rs.fields.is_empty() {
// If the struct has no fields, we can define it as an static object.
writeln!(w, "object {}\n", rs.id.renamed)?;
writeln!(w, "object {}{}\n", self.prefix, rs.id.renamed)?;
} else {
writeln!(
w,
"data class {}{} (",
"data class {}{}{} (",
self.prefix,
rs.id.renamed,
(!rs.generic_types.is_empty())
.then(|| format!("<{}>", rs.generic_types.join(", ")))
Expand Down Expand Up @@ -152,9 +156,11 @@ impl Language for Kotlin {
}

fn write_enum(&mut self, w: &mut dyn Write, e: &RustEnum) -> std::io::Result<()> {
let type_name = format!("{}{}", &self.prefix, &e.shared().id.renamed);

// Generate named types for any anonymous struct variants of this enum
self.write_types_for_anonymous_structs(w, e, &|variant_name| {
format!("{}{}Inner", &e.shared().id.renamed, variant_name)
format!("{}{}Inner", type_name, variant_name)
})?;

self.write_comments(w, 0, &e.shared().comments)?;
Expand All @@ -165,19 +171,15 @@ impl Language for Kotlin {
.unwrap_or_default();

match e {
RustEnum::Unit(shared) => {
RustEnum::Unit(..) => {
write!(
w,
"enum class {}{}(val string: String) ",
shared.id.renamed, generic_parameters
type_name, generic_parameters
)?;
}
RustEnum::Algebraic { shared, .. } => {
write!(
w,
"sealed class {}{} ",
shared.id.renamed, generic_parameters
)?;
RustEnum::Algebraic { .. } => {
write!(w, "sealed class {}{} ", type_name, generic_parameters)?;
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/tests/snapshot_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,8 @@ tests! {
typescript,
go
];
can_apply_prefix_correctly: [ swift { prefix: "OP".to_string(), }, kotlin, scala, typescript, go ];
can_generate_empty_algebraic_enum: [ swift { prefix: "OP".to_string(), }, kotlin, scala, typescript, go ];
can_apply_prefix_correctly: [ swift { prefix: "OP".to_string(), }, kotlin { prefix: "OP".to_string(), }, scala, typescript, go ];
can_generate_empty_algebraic_enum: [ swift { prefix: "OP".to_string(), }, kotlin { prefix: "OP".to_string(), }, scala, typescript, go ];
can_generate_algebraic_enum_with_skipped_variants: [swift, kotlin, scala, typescript, go];
can_generate_struct_with_skipped_fields: [swift, kotlin, scala, typescript, go];
enum_is_properly_named_with_serde_overrides: [swift, kotlin, scala, typescript, go];
Expand Down

0 comments on commit 5a3cbdd

Please sign in to comment.