Skip to content

Commit

Permalink
Add support for Kotlin annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
czocher committed Nov 26, 2023
1 parent c3ee2ad commit 6bda104
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 9 deletions.
4 changes: 4 additions & 0 deletions core/data/tests/can_generate_kotlin_annotations/input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[typeshare(kotlin = "Serializable, Immutable, Parcelize")]
struct MyType {
value: String,
}
14 changes: 14 additions & 0 deletions core/data/tests/can_generate_kotlin_annotations/output.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@file:NoLiveLiterals

package com.agilebits.onepassword

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

@Serializable
@Immutable
@Parcelize
data class MyType (
val value: String
)

11 changes: 11 additions & 0 deletions core/data/tests/can_generate_kotlin_annotations/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* This is a comment.
* Continued lovingly here
*/
export enum Colors {
Red = "Red",
Blue = "Blue",
/** Green is a cool color */
Green = "Green",
}

10 changes: 10 additions & 0 deletions core/src/language/kotlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ impl Language for Kotlin {
self.write_comments(w, 0, &rs.comments)?;
writeln!(w, "@Serializable")?;

for decorator in rs
.decorators
.get(&SupportedLanguage::Kotlin)
.unwrap_or(&Vec::<String>::new())
.into_iter()
.filter(|d| d != &&"Serializable")
{
writeln!(w, "@{}", decorator)?;
}

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)?;
Expand Down
13 changes: 13 additions & 0 deletions core/src/language/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ impl TryFrom<&Ident> for SupportedLanguage {
}
}

impl ToString for SupportedLanguage {
fn to_string(&self) -> String {
match self {
SupportedLanguage::Go => "go",
SupportedLanguage::Kotlin => "kotlin",
SupportedLanguage::Scala => "scala",
SupportedLanguage::Swift => "swift",
SupportedLanguage::TypeScript => "typescript",
}
.to_owned()
}
}

/// Language-specific state and processing.
///
/// The `Language` implementation is allowed to maintain mutable state, and it
Expand Down
22 changes: 13 additions & 9 deletions core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,15 +616,19 @@ fn get_decorators(attrs: &[syn::Attribute]) -> HashMap<SupportedLanguage, Vec<St
// The resulting HashMap, Key is the language, and the value is a vector of decorators words that will be put onto structures
let mut out: HashMap<SupportedLanguage, Vec<String>> = HashMap::new();

for value in get_typeshare_name_value_meta_items(attrs, "swift").filter_map(literal_as_string) {
let decorators: Vec<String> = value.split(',').map(|s| s.trim().to_string()).collect();

// lastly, get the entry in the hashmap output and extend the value, or insert what we have already found
let decs = out.entry(SupportedLanguage::Swift).or_default();
decs.extend(decorators);
// Sorting so all the added decorators will be after the normal ([`String`], `Codable`) in alphabetical order
decs.sort_unstable();
decs.dedup(); //removing any duplicates just in case
for language in SupportedLanguage::all_languages() {
for value in get_typeshare_name_value_meta_items(attrs, &language.to_string())
.filter_map(literal_as_string)
{
let decorators: Vec<String> = value.split(',').map(|s| s.trim().to_string()).collect();

// lastly, get the entry in the hashmap output and extend the value, or insert what we have already found
let decs = out.entry(language).or_default();
decs.extend(decorators);
// Sorting so all the added decorators will be after the normal ([`String`], `Codable`) in alphabetical order
decs.sort_unstable();
decs.dedup(); //removing any duplicates just in case
}
}

//return our hashmap mapping of language -> Vec<decorators>
Expand Down
3 changes: 3 additions & 0 deletions core/tests/snapshot_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ tests! {
scala,
typescript
];
can_generate_kotlin_annotations: [
kotlin
];
can_generate_slice_of_user_type: [swift, kotlin, scala, typescript, go];
can_generate_readonly_fields: [
typescript
Expand Down

0 comments on commit 6bda104

Please sign in to comment.