Skip to content

Commit

Permalink
Migrate to syn 2.0 crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Swoorup committed Apr 4, 2024
1 parent d62c8ff commit f66efbe
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ half = { version = "2.1", default-features = false }
proc-macro-error = "1"
proc-macro2 = "1"
quote = "1"
syn = "1"
syn = "2"
trybuild = "1.0"
pretty_assertions = "1.4"
33 changes: 32 additions & 1 deletion arrow_convert/tests/test_enum.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use arrow::array::*;
use arrow::{array::*, datatypes::*};
use arrow_convert::{
deserialize::TryIntoCollection, serialize::TryIntoArrow, ArrowDeserialize, ArrowField,
ArrowSerialize,
};
use pretty_assertions::assert_eq;

#[test]
fn test_dense_enum_unit_variant() {
Expand All @@ -22,6 +23,21 @@ fn test_dense_enum_unit_variant() {
TestEnum::VAL4,
];
let b: ArrayRef = enums.try_into_arrow().unwrap();
assert_eq!(
b.data_type(),
&DataType::Union(
UnionFields::new(
vec![0, 1, 2, 3],
vec![
Field::new("VAL1", DataType::Boolean, false),
Field::new("VAL2", DataType::Boolean, false),
Field::new("VAL3", DataType::Boolean, false),
Field::new("VAL4", DataType::Boolean, false),
]
),
UnionMode::Dense
)
);
let round_trip: Vec<TestEnum> = b.try_into_collection().unwrap();
assert_eq!(round_trip, enums);
}
Expand All @@ -44,6 +60,21 @@ fn test_sparse_enum_unit_variant() {
TestEnum::VAL4,
];
let b: ArrayRef = enums.try_into_arrow().unwrap();
assert_eq!(
b.data_type(),
&DataType::Union(
UnionFields::new(
vec![0, 1, 2, 3],
vec![
Field::new("VAL1", DataType::Boolean, false),
Field::new("VAL2", DataType::Boolean, false),
Field::new("VAL3", DataType::Boolean, false),
Field::new("VAL4", DataType::Boolean, false),
]
),
UnionMode::Sparse
)
);
let round_trip: Vec<TestEnum> = b.try_into_collection().unwrap();
assert_eq!(round_trip, enums);
}
Expand Down
112 changes: 55 additions & 57 deletions arrow_convert_derive/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use proc_macro2::Span;
use proc_macro_error::{abort, ResultExt};
use proc_macro_error::abort;

use syn::spanned::Spanned;
use syn::{DeriveInput, Ident, Lit, Meta, MetaNameValue, Visibility};
use syn::{DeriveInput, Ident, Lit, Meta, Visibility};

pub const ARROW_FIELD: &str = "arrow_field";
pub const FIELD_TYPE: &str = "type";
Expand Down Expand Up @@ -83,42 +84,38 @@ impl ContainerAttrs {
let mut is_transparent: Option<Span> = None;

for attr in attrs {
if let Ok(meta) = attr.parse_meta() {
if meta.path().is_ident(ARROW_FIELD) {
if let Meta::List(list) = meta {
for nested in list.nested {
if let syn::NestedMeta::Meta(meta) = nested {
match meta {
syn::Meta::NameValue(MetaNameValue {
lit: Lit::Str(string),
path,
..
}) if path.is_ident(UNION_TYPE) => {
match string.value().as_ref() {
UNION_TYPE_DENSE => {
is_dense = Some(true);
}
UNION_TYPE_SPARSE => {
is_dense = Some(false);
}
_ => {
abort!(path.span(), "Unexpected value for mode");
}
}
}

Meta::Path(path) if path.is_ident(TRANSPARENT) => {
is_transparent = Some(path.span());
if attr.path().is_ident(ARROW_FIELD) {
let _ = attr.parse_nested_meta(|meta| {
if let Meta::List(list) = &attr.meta {
list.parse_nested_meta(|nested| {
if nested.path.is_ident(TRANSPARENT) {
is_transparent = Some(nested.path.span());
Ok(())
} else if nested.path.is_ident(UNION_TYPE) {
let value = nested.value()?;
let Lit::Str(string) = value.parse()? else {
return Err(nested.error("Unexpected value for mode"));
};

match string.value().as_ref() {
UNION_TYPE_DENSE => {
is_dense = Some(true);
Ok(())
}

_ => {
abort!(meta.span(), "Unexpected attribute");
UNION_TYPE_SPARSE => {
is_dense = Some(false);
Ok(())
}
_ => Err(nested.error("Unexpected value for mode")),
}
} else {
Err(meta.error("Unexpected attribute"))
}
}
}
}
})?;
};

Ok(())
});
}
}

Expand All @@ -135,29 +132,29 @@ impl FieldAttrs {
let mut skip = false;

for attr in input {
if let Ok(meta) = attr.parse_meta() {
if meta.path().is_ident(ARROW_FIELD) {
if let Meta::List(list) = meta {
for nested in list.nested {
if let syn::NestedMeta::Meta(meta) = nested {
match meta {
Meta::NameValue(MetaNameValue {
lit: Lit::Str(string),
path,
..
}) if path.is_ident(FIELD_TYPE) => {
field_type =
Some(syn::parse_str(&string.value()).unwrap_or_abort());
}
Meta::Path(path) if path.is_ident(FIELD_SKIP) => skip = true,
_ => {
abort!(meta.span(), "Unexpected attribute");
}
}
}
if attr.path().is_ident(ARROW_FIELD) {
let _ = attr.parse_nested_meta(|meta| {
let Meta::List(list) = &attr.meta else {
return Err(meta.error("Unexpected attribute"));
};

list.parse_nested_meta(|nested| {
if nested.path.is_ident(FIELD_SKIP) {
skip = true;
Ok(())
} else if nested.path.is_ident(FIELD_TYPE) {
let value = nested.value()?;
let Lit::Str(string) = value.parse()? else {
return Err(meta.error("Unexpected attribute"));
};

field_type = Some(syn::parse_str(&string.value())?);
Ok(())
} else {
return Err(meta.error("Unexpected attribute"));
}
}
}
})
});
}
}

Expand Down Expand Up @@ -237,7 +234,8 @@ impl DeriveVariant {
(false, f.unnamed[0].ty.clone())
}
}
syn::Fields::Unit => (true, syn::parse_str("bool").unwrap_or_abort()),

syn::Fields::Unit => (true, syn::parse_str("bool").unwrap()),
};
DeriveVariant {
syn: input.clone(),
Expand Down

0 comments on commit f66efbe

Please sign in to comment.