Skip to content

Commit cbd213a

Browse files
committed
Add support for Kotlin annotations
1 parent c3ee2ad commit cbd213a

File tree

6 files changed

+57
-9
lines changed

6 files changed

+57
-9
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[typeshare(kotlin = "Serializable, Immutable, Parcelize")]
2+
struct MyType {
3+
value: String,
4+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@file:NoLiveLiterals
2+
3+
package com.agilebits.onepassword
4+
5+
import androidx.compose.runtime.NoLiveLiterals
6+
import kotlinx.serialization.*
7+
8+
@Serializable
9+
@Immutable
10+
@Parcelize
11+
data class MyType (
12+
val value: String
13+
)
14+

core/src/language/kotlin.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ impl Language for Kotlin {
116116
self.write_comments(w, 0, &rs.comments)?;
117117
writeln!(w, "@Serializable")?;
118118

119+
for decorator in rs
120+
.decorators
121+
.get(&SupportedLanguage::Kotlin)
122+
.unwrap_or(&Vec::<String>::new())
123+
.into_iter()
124+
.filter(|d| d != &&"Serializable")
125+
{
126+
writeln!(w, "@{}", decorator)?;
127+
}
128+
119129
if rs.fields.is_empty() {
120130
// If the struct has no fields, we can define it as an static object.
121131
writeln!(w, "object {}\n", rs.id.renamed)?;

core/src/language/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ impl TryFrom<&Ident> for SupportedLanguage {
6363
}
6464
}
6565

66+
impl ToString for SupportedLanguage {
67+
fn to_string(&self) -> String {
68+
match self {
69+
SupportedLanguage::Go => "go",
70+
SupportedLanguage::Kotlin => "kotlin",
71+
SupportedLanguage::Scala => "scala",
72+
SupportedLanguage::Swift => "swift",
73+
SupportedLanguage::TypeScript => "typescript",
74+
}
75+
.to_owned()
76+
}
77+
}
78+
6679
/// Language-specific state and processing.
6780
///
6881
/// The `Language` implementation is allowed to maintain mutable state, and it

core/src/parser.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -616,15 +616,19 @@ fn get_decorators(attrs: &[syn::Attribute]) -> HashMap<SupportedLanguage, Vec<St
616616
// The resulting HashMap, Key is the language, and the value is a vector of decorators words that will be put onto structures
617617
let mut out: HashMap<SupportedLanguage, Vec<String>> = HashMap::new();
618618

619-
for value in get_typeshare_name_value_meta_items(attrs, "swift").filter_map(literal_as_string) {
620-
let decorators: Vec<String> = value.split(',').map(|s| s.trim().to_string()).collect();
621-
622-
// lastly, get the entry in the hashmap output and extend the value, or insert what we have already found
623-
let decs = out.entry(SupportedLanguage::Swift).or_default();
624-
decs.extend(decorators);
625-
// Sorting so all the added decorators will be after the normal ([`String`], `Codable`) in alphabetical order
626-
decs.sort_unstable();
627-
decs.dedup(); //removing any duplicates just in case
619+
for language in SupportedLanguage::all_languages() {
620+
for value in get_typeshare_name_value_meta_items(attrs, &language.to_string())
621+
.filter_map(literal_as_string)
622+
{
623+
let decorators: Vec<String> = value.split(',').map(|s| s.trim().to_string()).collect();
624+
625+
// lastly, get the entry in the hashmap output and extend the value, or insert what we have already found
626+
let decs = out.entry(language).or_default();
627+
decs.extend(decorators);
628+
// Sorting so all the added decorators will be after the normal ([`String`], `Codable`) in alphabetical order
629+
decs.sort_unstable();
630+
decs.dedup(); //removing any duplicates just in case
631+
}
628632
}
629633

630634
//return our hashmap mapping of language -> Vec<decorators>

core/tests/snapshot_tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,9 @@ tests! {
390390
scala,
391391
typescript
392392
];
393+
can_generate_kotlin_annotations: [
394+
kotlin
395+
];
393396
can_generate_slice_of_user_type: [swift, kotlin, scala, typescript, go];
394397
can_generate_readonly_fields: [
395398
typescript

0 commit comments

Comments
 (0)