-
-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a macro
pg_cast
for constructing PG casts from Rust functions.
To use the new macro, add the `pg_cast` and `pg_extern` macros on single-argument single-return value Rust function: ```rust [pg_extern] [pg_cast] fn test_cast(_value: Json) -> i32 { 0 } ``` It is possible to parametrize the cast by adding either `assignment` or `implicit` to the `pg_cast` macro as argument: ``` [pg_cast(assignment)] -> creates a PG cast that can be used for casting from one type to another at assignment time [pg_cast(implicit)] -> creates a PG cast that can be used for casting from one type to another in any context ``` TESTED=`cargo-pgrx pgrx test --features "pg13"`
- Loading branch information
Louis Kuang
committed
Dec 24, 2023
1 parent
f6c032e
commit 4ccf619
Showing
8 changed files
with
291 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//LICENSE Portions Copyright 2019-2021 ZomboDB, LLC. | ||
//LICENSE | ||
//LICENSE Portions Copyright 2021-2023 Technology Concepts & Design, Inc. | ||
//LICENSE | ||
//LICENSE Portions Copyright 2023-2023 PgCentral Foundation, Inc. <[email protected]> | ||
//LICENSE | ||
//LICENSE All rights reserved. | ||
//LICENSE | ||
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file. | ||
/*! | ||
`#[pg_cast]` related macro expansion for Rust to SQL translation | ||
> Like all of the [`sql_entity_graph`][crate] APIs, this is considered **internal** | ||
to the `pgrx` framework and very subject to change between versions. While you may use this, please do it with caution. | ||
*/ | ||
use proc_macro2::TokenStream as TokenStream2; | ||
use quote::{quote, ToTokens, TokenStreamExt}; | ||
|
||
/// A parsed `#[pg_cast]` operator. | ||
/// | ||
/// It is created during [`PgExtern`](crate::PgExtern) parsing. | ||
#[derive(Debug, Clone)] | ||
pub enum PgCast { | ||
Default, | ||
Assignment, | ||
Implicit, | ||
} | ||
|
||
impl ToTokens for PgCast { | ||
fn to_tokens(&self, tokens: &mut TokenStream2) { | ||
let quoted = match self { | ||
PgCast::Default => quote! { | ||
::pgrx::pgrx_sql_entity_graph::PgCastEntity::Default | ||
}, | ||
PgCast::Assignment => quote! { | ||
::pgrx::pgrx_sql_entity_graph::PgCastEntity::Assignment | ||
}, | ||
PgCast::Implicit => quote! { | ||
::pgrx::pgrx_sql_entity_graph::PgCastEntity::Implicit | ||
}, | ||
}; | ||
tokens.append_all(quoted); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//LICENSE Portions Copyright 2019-2021 ZomboDB, LLC. | ||
//LICENSE | ||
//LICENSE Portions Copyright 2021-2023 Technology Concepts & Design, Inc. | ||
//LICENSE | ||
//LICENSE Portions Copyright 2023-2023 PgCentral Foundation, Inc. <[email protected]> | ||
//LICENSE | ||
//LICENSE All rights reserved. | ||
//LICENSE | ||
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file. | ||
/*! | ||
`#[pg_extern]` related cast entities for Rust to SQL translation | ||
> Like all of the [`sql_entity_graph`][crate] APIs, this is considered **internal** | ||
to the `pgrx` framework and very subject to change between versions. While you may use this, please do it with caution. | ||
*/ | ||
|
||
/// The output of a [`PgCast`](crate::PgCast) from `quote::ToTokens::to_tokens`. | ||
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] | ||
pub enum PgCastEntity { | ||
Default, | ||
Assignment, | ||
Implicit, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.