Skip to content

Commit a281654

Browse files
committed
Kotlin: hide value of redacted inline value classes
1 parent 4a73bd4 commit a281654

File tree

3 files changed

+70
-18
lines changed

3 files changed

+70
-18
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: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,26 +124,46 @@ impl Language for Kotlin {
124124
writeln!(w, "@JvmInline")?;
125125
writeln!(w, "value class {}{}(", self.prefix, ty.id.renamed)?;
126126

127-
self.write_element(
128-
w,
129-
&RustField {
130-
id: Id {
131-
original: String::from("value"),
132-
renamed: String::from("value"),
127+
if ty.is_redacted {
128+
self.write_private_element(
129+
w,
130+
&RustField {
131+
id: Id {
132+
original: String::from("value"),
133+
renamed: String::from("value"),
134+
},
135+
ty: ty.r#type.clone(),
136+
comments: vec![],
137+
has_default: false,
138+
decorators: HashMap::new(),
133139
},
134-
ty: ty.r#type.clone(),
135-
comments: vec![],
136-
has_default: false,
137-
decorators: HashMap::new(),
138-
},
139-
&[],
140-
false,
141-
)?;
140+
&[],
141+
false,
142+
)?;
143+
} else {
144+
self.write_element(
145+
w,
146+
&RustField {
147+
id: Id {
148+
original: String::from("value"),
149+
renamed: String::from("value"),
150+
},
151+
ty: ty.r#type.clone(),
152+
comments: vec![],
153+
has_default: false,
154+
decorators: HashMap::new(),
155+
},
156+
&[],
157+
false,
158+
)?;
159+
}
142160

143161
writeln!(w)?;
144162

145163
if ty.is_redacted {
146164
writeln!(w, ") {{")?;
165+
writeln!(w, "\tfun unwrap() = value")?;
166+
writeln!(w)?;
147167
writeln!(w, "\toverride fun toString(): String = \"***\"")?;
148168
writeln!(w, "}}")?;
149169
} else {
@@ -426,6 +446,36 @@ impl Kotlin {
426446
)
427447
}
428448

449+
fn write_private_element(
450+
&mut self,
451+
w: &mut dyn Write,
452+
f: &RustField,
453+
generic_types: &[String],
454+
requires_serial_name: bool,
455+
) -> std::io::Result<()> {
456+
self.write_comments(w, 1, &f.comments)?;
457+
if requires_serial_name {
458+
writeln!(w, "\t@SerialName({:?})", &f.id.renamed)?;
459+
}
460+
let ty = match f.type_override(SupportedLanguage::Kotlin) {
461+
Some(type_override) => type_override.to_owned(),
462+
None => self
463+
.format_type(&f.ty, generic_types)
464+
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?,
465+
};
466+
467+
write!(
468+
w,
469+
"\tprivate val {}: {}{}",
470+
remove_dash_from_identifier(&f.id.renamed),
471+
ty,
472+
(f.has_default && !f.ty.is_optional())
473+
.then_some("? = null")
474+
.or_else(|| f.ty.is_optional().then_some(" = null"))
475+
.unwrap_or_default()
476+
)
477+
}
478+
429479
fn write_comment(
430480
&self,
431481
w: &mut dyn Write,

core/src/rust_types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ pub enum RustType {
156156
/// - `SomeStruct<String>`
157157
/// - `SomeEnum<u32>`
158158
/// - `SomeTypeAlias<(), &str>`
159-
/// However, there are some generic types that are considered to be _special_. These
160-
/// include `Vec<T>` `HashMap<K, V>`, and `Option<T>`, which are part of `SpecialRustType` instead
161-
/// of `RustType::Generic`.
159+
/// However, there are some generic types that are considered to be _special_. These
160+
/// include `Vec<T>` `HashMap<K, V>`, and `Option<T>`, which are part of `SpecialRustType` instead
161+
/// of `RustType::Generic`.
162162
///
163163
/// If a generic type is type-mapped via `typeshare.toml`, the generic parameters will be dropped automatically.
164164
Generic {

0 commit comments

Comments
 (0)