Skip to content

Commit d55c5d1

Browse files
authored
Fix a problem with fns returning tuples with Attributes (#440)
Signed-off-by: Ana Hobden <[email protected]>
1 parent ed9f061 commit d55c5d1

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

pgx-tests/src/tests/pg_extern_args_tests.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
// Copyright 2020 ZomboDB, LLC <[email protected]>. All rights reserved. Use of this source code is
22
// governed by the MIT license that can be found in the LICENSE file.
33

4+
use pgx::*;
5+
6+
#[pg_extern(immutable)]
7+
fn returns_tuple_with_attributes() -> (
8+
name!(arg, String),
9+
name!(arg2, String),
10+
) {
11+
("hi".to_string(), "bye".to_string())
12+
}
13+
414
#[cfg(any(test, feature = "pg_test"))]
515
#[pgx::pg_schema]
616
mod tests {

pgx-utils/src/sql_entity_graph/pg_extern/attribute.rs

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ pub enum Attribute {
2626
Sql(ToSqlConfig),
2727
}
2828

29-
impl ToTokens for Attribute {
30-
fn to_tokens(&self, tokens: &mut TokenStream2) {
31-
let quoted = match self {
29+
impl Attribute {
30+
pub(crate) fn to_sql_entity_graph_tokens(&self) -> TokenStream2 {
31+
match self {
3232
Attribute::Immutable => quote! { pgx::datum::sql_entity_graph::ExternArgs::Immutable },
3333
Attribute::Strict => quote! { pgx::datum::sql_entity_graph::ExternArgs::Strict },
3434
Attribute::Stable => quote! { pgx::datum::sql_entity_graph::ExternArgs::Stable },
@@ -65,7 +65,52 @@ impl ToTokens for Attribute {
6565
}
6666
// This attribute is handled separately
6767
Attribute::Sql(_) => {
68-
return;
68+
quote! { }
69+
}
70+
}
71+
}
72+
}
73+
74+
impl ToTokens for Attribute {
75+
fn to_tokens(&self, tokens: &mut TokenStream2) {
76+
let quoted = match self {
77+
Attribute::Immutable => quote! { immutable },
78+
Attribute::Strict => quote! { strict },
79+
Attribute::Stable => quote! { stable },
80+
Attribute::Volatile => quote! { volatile },
81+
Attribute::Raw => quote! { raw },
82+
Attribute::NoGuard => quote! { no_guard },
83+
Attribute::ParallelSafe => {
84+
quote! { parallel_safe }
85+
}
86+
Attribute::ParallelUnsafe => {
87+
quote! { parallel_unsafe }
88+
}
89+
Attribute::ParallelRestricted => {
90+
quote! { parallel_restricted }
91+
}
92+
Attribute::Error(s) => {
93+
quote! { error = #s }
94+
}
95+
Attribute::Schema(s) => {
96+
quote! { schema = #s }
97+
}
98+
Attribute::Name(s) => {
99+
quote! { name = #s }
100+
}
101+
Attribute::Cost(s) => {
102+
quote! { cost = #s }
103+
}
104+
Attribute::Requires(items) => {
105+
let items_iter = items
106+
.iter()
107+
.map(|x| x.to_token_stream())
108+
.collect::<Vec<_>>();
109+
quote! { requires = [#(#items_iter),*] }
110+
}
111+
// This attribute is handled separately
112+
Attribute::Sql(to_sql_config) => {
113+
quote! { sql = #to_sql_config }
69114
}
70115
};
71116
tokens.append_all(quoted);
@@ -128,7 +173,7 @@ impl Parse for Attribute {
128173
}
129174
}
130175
}
131-
_ => return Err(syn::Error::new(Span::call_site(), "Invalid option")),
176+
e => return Err(syn::Error::new(Span::call_site(), format!("Invalid option `{}` inside `{} {}`", e, ident.to_string(), input.to_string()))),
132177
};
133178
Ok(found)
134179
}

pgx-utils/src/sql_entity_graph/pg_extern/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@ impl ToTokens for PgExtern {
219219
let name = self.name();
220220
let schema = self.schema();
221221
let schema_iter = schema.iter();
222-
let extern_attrs = self.attrs.iter().collect::<Punctuated<_, Token![,]>>();
222+
let extern_attrs = self.attrs.iter()
223+
.map(|attr| attr.to_sql_entity_graph_tokens())
224+
.collect::<Punctuated<_, Token![,]>>();
223225
let search_path = self.search_path().into_iter();
224226
let inputs = self.inputs().unwrap();
225227
let returns = match self.returns() {

0 commit comments

Comments
 (0)