Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Kotlin annotations #151

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)

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())
.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
Loading