From 6bda1046194c307a348e592b55009db87def7ec3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jan=20Czocha=C5=84ski?= Date: Sun, 26 Nov 2023 20:02:33 +0100 Subject: [PATCH] Add support for Kotlin annotations --- .../can_generate_kotlin_annotations/input.rs | 4 ++++ .../can_generate_kotlin_annotations/output.kt | 14 ++++++++++++ .../can_generate_kotlin_annotations/output.ts | 11 ++++++++++ core/src/language/kotlin.rs | 10 +++++++++ core/src/language/mod.rs | 13 +++++++++++ core/src/parser.rs | 22 +++++++++++-------- core/tests/snapshot_tests.rs | 3 +++ 7 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 core/data/tests/can_generate_kotlin_annotations/input.rs create mode 100644 core/data/tests/can_generate_kotlin_annotations/output.kt create mode 100644 core/data/tests/can_generate_kotlin_annotations/output.ts diff --git a/core/data/tests/can_generate_kotlin_annotations/input.rs b/core/data/tests/can_generate_kotlin_annotations/input.rs new file mode 100644 index 00000000..a9c9d2d6 --- /dev/null +++ b/core/data/tests/can_generate_kotlin_annotations/input.rs @@ -0,0 +1,4 @@ +#[typeshare(kotlin = "Serializable, Immutable, Parcelize")] +struct MyType { + value: String, +} diff --git a/core/data/tests/can_generate_kotlin_annotations/output.kt b/core/data/tests/can_generate_kotlin_annotations/output.kt new file mode 100644 index 00000000..45963e88 --- /dev/null +++ b/core/data/tests/can_generate_kotlin_annotations/output.kt @@ -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 +) + diff --git a/core/data/tests/can_generate_kotlin_annotations/output.ts b/core/data/tests/can_generate_kotlin_annotations/output.ts new file mode 100644 index 00000000..79b4e8f1 --- /dev/null +++ b/core/data/tests/can_generate_kotlin_annotations/output.ts @@ -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", +} + diff --git a/core/src/language/kotlin.rs b/core/src/language/kotlin.rs index cd0e22b2..e85de553 100644 --- a/core/src/language/kotlin.rs +++ b/core/src/language/kotlin.rs @@ -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::::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)?; diff --git a/core/src/language/mod.rs b/core/src/language/mod.rs index c96ba5ac..c1528cdd 100644 --- a/core/src/language/mod.rs +++ b/core/src/language/mod.rs @@ -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 diff --git a/core/src/parser.rs b/core/src/parser.rs index efc7b505..2307bf82 100644 --- a/core/src/parser.rs +++ b/core/src/parser.rs @@ -616,15 +616,19 @@ fn get_decorators(attrs: &[syn::Attribute]) -> HashMap> = HashMap::new(); - for value in get_typeshare_name_value_meta_items(attrs, "swift").filter_map(literal_as_string) { - let decorators: Vec = 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 = 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 diff --git a/core/tests/snapshot_tests.rs b/core/tests/snapshot_tests.rs index 62c50c1e..a4bd38eb 100644 --- a/core/tests/snapshot_tests.rs +++ b/core/tests/snapshot_tests.rs @@ -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