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

remove unused generics/phantomdata in generated code #132

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
20 changes: 14 additions & 6 deletions core/data/tests/can_generate_generic_struct/input.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
#[typeshare]
pub struct GenericStruct<A, B> {
field_a: A,
field_b: Vec<B>
field_b: Vec<B>,
}

#[typeshare]
pub struct UnusedGenericsStruct<A, B> {
field_a: f32,
field_b: f32,
_phantom_data: std::marker::PhantomData<A, B>,
}

#[typeshare]
pub struct UnusedGenericsEmptyStruct<A, B, C> {}

#[typeshare]
#[serde(tag = "type", content = "content")]
pub enum EnumUsingGenericStruct{
pub enum EnumUsingGenericStruct {
VariantA(GenericStruct<String, f32>),
VariantB(GenericStruct<&'static str, i32>),
VariantC(GenericStruct<&'static str, bool>),
VariantD(GenericStructUsingGenericStruct<()>)
VariantD(GenericStructUsingGenericStruct<()>),
}

#[typeshare]
pub struct GenericStructUsingGenericStruct<T> {
struct_field: GenericStruct<String, T>,
second_struct_field: GenericStruct<T, String>,
third_struct_field: GenericStruct<T, Vec<T>>
third_struct_field: GenericStruct<T, Vec<T>>,
}


10 changes: 10 additions & 0 deletions core/data/tests/can_generate_generic_struct/output.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ data class GenericStruct<A, B> (
val field_b: List<B>
)

@Serializable
data class UnusedGenericsStruct (
val field_a: Float,
val field_b: Float
)

@Serializable
data class UnusedGenericsEmptyStruct (
)

@Serializable
data class GenericStructUsingGenericStruct<T> (
val struct_field: GenericStruct<String, T>,
Expand Down
7 changes: 7 additions & 0 deletions core/data/tests/can_generate_generic_struct/output.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ case class GenericStruct[A, B] (
field_b: Vector[B]
)

case class UnusedGenericsStruct (
field_a: Float,
field_b: Float
)

case class UnusedGenericsEmptyStruct ()

case class GenericStructUsingGenericStruct[T] (
struct_field: GenericStruct[String, T],
second_struct_field: GenericStruct[T, String],
Expand Down
14 changes: 14 additions & 0 deletions core/data/tests/can_generate_generic_struct/output.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ public struct CoreGenericStruct<A: Codable, B: Codable>: Codable {
}
}

public struct CoreUnusedGenericsStruct: Codable {
public let field_a: Float
public let field_b: Float

public init(field_a: Float, field_b: Float) {
self.field_a = field_a
self.field_b = field_b
}
}

public struct CoreUnusedGenericsEmptyStruct: Codable {
public init() {}
}

public struct CoreGenericStructUsingGenericStruct<T: Codable>: Codable {
public let struct_field: CoreGenericStruct<String, T>
public let second_struct_field: CoreGenericStruct<T, String>
Expand Down
8 changes: 8 additions & 0 deletions core/data/tests/can_generate_generic_struct/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ export interface GenericStruct<A, B> {
field_b: B[];
}

export interface UnusedGenericsStruct {
field_a: number;
field_b: number;
}

export interface UnusedGenericsEmptyStruct {
}

export interface GenericStructUsingGenericStruct<T> {
struct_field: GenericStruct<string, T>;
second_struct_field: GenericStruct<T, string>;
Expand Down
1 change: 0 additions & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! The core library for typeshare.
//! Contains the parser and language converters.

use language::Language;
use std::io::Write;
use thiserror::Error;
Expand Down
25 changes: 23 additions & 2 deletions core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,28 @@ fn parse_struct(s: &ItemStruct) -> Result<RustItem, ParseError> {
}));
}

let mut used_generic_types = Vec::new();

Ok(match &s.fields {
// Structs
Fields::Named(f) => {
let fields = f
.named
.iter()
.filter(|field| !is_skipped(&field.attrs))
.filter(|field| !is_skipped(&field.attrs) && !is_phantom_data(&field.ty))
.map(|f| {
let ty = if let Some(ty) = get_field_type_override(&f.attrs) {
ty.parse()?
} else {
RustType::try_from(&f.ty)?
};

for generic_type in &generic_types {
if ty.contains_type(generic_type) {
used_generic_types.push(generic_type.clone());
}
}

if serde_flatten(&f.attrs) {
return Err(ParseError::SerdeFlattenNotAllowed);
}
Expand All @@ -191,7 +199,10 @@ fn parse_struct(s: &ItemStruct) -> Result<RustItem, ParseError> {

RustItem::Struct(RustStruct {
id: get_ident(Some(&s.ident), &s.attrs, &None),
generic_types,
generic_types: generic_types
.into_iter()
.filter(|gt| used_generic_types.contains(gt))
.collect(),
fields,
comments: parse_comment_attrs(&s.attrs),
decorators: get_decorators(&s.attrs),
Expand Down Expand Up @@ -724,6 +735,16 @@ fn is_skipped(attrs: &[syn::Attribute]) -> bool {
})
}

fn is_phantom_data(ty: &syn::Type) -> bool {
if let syn::Type::Path(path) = ty {
if let Some(segment) = path.path.segments.last() {
return segment.ident == "PhantomData";
}
}

false
}

#[test]
fn test_rename_all_to_case() {
let test_word = "test_case";
Expand Down