Skip to content

Commit 132c639

Browse files
authored
Merge pull request 1Password#190 from jonduran3000/main
Kotlin: hide value of redacted inline value classes
2 parents ccccb33 + 5912903 commit 132c639

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

core/data/tests/struct_decorator/output.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import kotlinx.serialization.SerialName
66
@Serializable
77
@JvmInline
88
value class BestHockeyTeams5(
9-
val value: String
9+
private val value: String
1010
) {
11+
fun unwrap() = value
12+
1113
override fun toString(): String = "***"
1214
}
1315

core/src/language/kotlin.rs

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,18 @@ impl Language for Kotlin {
138138
},
139139
&[],
140140
false,
141+
match ty.is_redacted {
142+
true => Visibility::Private,
143+
false => Visibility::Public,
144+
},
141145
)?;
142146

143147
writeln!(w)?;
144148

145149
if ty.is_redacted {
146150
writeln!(w, ") {{")?;
151+
writeln!(w, "\tfun unwrap() = value")?;
152+
writeln!(w)?;
147153
writeln!(w, "\toverride fun toString(): String = \"***\"")?;
148154
writeln!(w, "}}")?;
149155
} else {
@@ -196,10 +202,22 @@ impl Language for Kotlin {
196202

197203
if let Some((last, elements)) = rs.fields.split_last() {
198204
for f in elements.iter() {
199-
self.write_element(w, f, rs.generic_types.as_slice(), requires_serial_name)?;
205+
self.write_element(
206+
w,
207+
f,
208+
rs.generic_types.as_slice(),
209+
requires_serial_name,
210+
Visibility::Public,
211+
)?;
200212
writeln!(w, ",")?;
201213
}
202-
self.write_element(w, last, rs.generic_types.as_slice(), requires_serial_name)?;
214+
self.write_element(
215+
w,
216+
last,
217+
rs.generic_types.as_slice(),
218+
requires_serial_name,
219+
Visibility::Public,
220+
)?;
203221
writeln!(w)?;
204222
}
205223

@@ -275,6 +293,11 @@ impl Language for Kotlin {
275293
}
276294
}
277295

296+
enum Visibility {
297+
Public,
298+
Private,
299+
}
300+
278301
impl Kotlin {
279302
fn write_enum_variants(&mut self, w: &mut dyn Write, e: &RustEnum) -> std::io::Result<()> {
280303
match e {
@@ -402,6 +425,7 @@ impl Kotlin {
402425
f: &RustField,
403426
generic_types: &[String],
404427
requires_serial_name: bool,
428+
visibility: Visibility,
405429
) -> std::io::Result<()> {
406430
self.write_comments(w, 1, &f.comments)?;
407431
if requires_serial_name {
@@ -414,16 +438,28 @@ impl Kotlin {
414438
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?,
415439
};
416440

417-
write!(
418-
w,
419-
"\tval {}: {}{}",
420-
remove_dash_from_identifier(&f.id.renamed),
421-
ty,
422-
(f.has_default && !f.ty.is_optional())
423-
.then_some("? = null")
424-
.or_else(|| f.ty.is_optional().then_some(" = null"))
425-
.unwrap_or_default()
426-
)
441+
match visibility {
442+
Visibility::Public => write!(
443+
w,
444+
"\tval {}: {}{}",
445+
remove_dash_from_identifier(&f.id.renamed),
446+
ty,
447+
(f.has_default && !f.ty.is_optional())
448+
.then_some("? = null")
449+
.or_else(|| f.ty.is_optional().then_some(" = null"))
450+
.unwrap_or_default()
451+
),
452+
Visibility::Private => write!(
453+
w,
454+
"\tprivate val {}: {}{}",
455+
remove_dash_from_identifier(&f.id.renamed),
456+
ty,
457+
(f.has_default && !f.ty.is_optional())
458+
.then_some("? = null")
459+
.or_else(|| f.ty.is_optional().then_some(" = null"))
460+
.unwrap_or_default()
461+
),
462+
}
427463
}
428464

429465
fn write_comment(

0 commit comments

Comments
 (0)