Skip to content

Commit

Permalink
Merge pull request #190 from jonduran3000/main
Browse files Browse the repository at this point in the history
Kotlin: hide value of redacted inline value classes
  • Loading branch information
seanaye authored Aug 26, 2024
2 parents ccccb33 + 5912903 commit 132c639
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
4 changes: 3 additions & 1 deletion core/data/tests/struct_decorator/output.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import kotlinx.serialization.SerialName
@Serializable
@JvmInline
value class BestHockeyTeams5(
val value: String
private val value: String
) {
fun unwrap() = value

override fun toString(): String = "***"
}

Expand Down
60 changes: 48 additions & 12 deletions core/src/language/kotlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,18 @@ impl Language for Kotlin {
},
&[],
false,
match ty.is_redacted {
true => Visibility::Private,
false => Visibility::Public,
},
)?;

writeln!(w)?;

if ty.is_redacted {
writeln!(w, ") {{")?;
writeln!(w, "\tfun unwrap() = value")?;
writeln!(w)?;
writeln!(w, "\toverride fun toString(): String = \"***\"")?;
writeln!(w, "}}")?;
} else {
Expand Down Expand Up @@ -196,10 +202,22 @@ impl Language for Kotlin {

if let Some((last, elements)) = rs.fields.split_last() {
for f in elements.iter() {
self.write_element(w, f, rs.generic_types.as_slice(), requires_serial_name)?;
self.write_element(
w,
f,
rs.generic_types.as_slice(),
requires_serial_name,
Visibility::Public,
)?;
writeln!(w, ",")?;
}
self.write_element(w, last, rs.generic_types.as_slice(), requires_serial_name)?;
self.write_element(
w,
last,
rs.generic_types.as_slice(),
requires_serial_name,
Visibility::Public,
)?;
writeln!(w)?;
}

Expand Down Expand Up @@ -275,6 +293,11 @@ impl Language for Kotlin {
}
}

enum Visibility {
Public,
Private,
}

impl Kotlin {
fn write_enum_variants(&mut self, w: &mut dyn Write, e: &RustEnum) -> std::io::Result<()> {
match e {
Expand Down Expand Up @@ -402,6 +425,7 @@ impl Kotlin {
f: &RustField,
generic_types: &[String],
requires_serial_name: bool,
visibility: Visibility,
) -> std::io::Result<()> {
self.write_comments(w, 1, &f.comments)?;
if requires_serial_name {
Expand All @@ -414,16 +438,28 @@ impl Kotlin {
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?,
};

write!(
w,
"\tval {}: {}{}",
remove_dash_from_identifier(&f.id.renamed),
ty,
(f.has_default && !f.ty.is_optional())
.then_some("? = null")
.or_else(|| f.ty.is_optional().then_some(" = null"))
.unwrap_or_default()
)
match visibility {
Visibility::Public => write!(
w,
"\tval {}: {}{}",
remove_dash_from_identifier(&f.id.renamed),
ty,
(f.has_default && !f.ty.is_optional())
.then_some("? = null")
.or_else(|| f.ty.is_optional().then_some(" = null"))
.unwrap_or_default()
),
Visibility::Private => write!(
w,
"\tprivate val {}: {}{}",
remove_dash_from_identifier(&f.id.renamed),
ty,
(f.has_default && !f.ty.is_optional())
.then_some("? = null")
.or_else(|| f.ty.is_optional().then_some(" = null"))
.unwrap_or_default()
),
}
}

fn write_comment(
Expand Down

0 comments on commit 132c639

Please sign in to comment.