Skip to content

Commit

Permalink
Merge pull request sfackler#25 from petrosagg/integrate-upstream
Browse files Browse the repository at this point in the history
integrate upstream
  • Loading branch information
petrosagg committed Aug 21, 2024
2 parents 02336be + 900ed50 commit 37f1114
Show file tree
Hide file tree
Showing 67 changed files with 3,161 additions and 1,306 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,33 @@ jobs:
key: clippy-target-${{ runner.os }}-${{ steps.rust-version.outputs.version }}-${{ hashFiles('Cargo.lock') }}y
- run: cargo clippy --all --all-targets

check-wasm32:
name: check-wasm32
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: sfackler/actions/rustup@master
- run: echo "version=$(rustc --version)" >> $GITHUB_OUTPUT
id: rust-version
- run: rustup target add wasm32-unknown-unknown
- uses: actions/cache@v3
with:
path: ~/.cargo/registry/index
key: index-${{ runner.os }}-${{ github.run_number }}
restore-keys: |
index-${{ runner.os }}-
- run: cargo generate-lockfile
- uses: actions/cache@v3
with:
path: ~/.cargo/registry/cache
key: registry-${{ runner.os }}-${{ steps.rust-version.outputs.version }}-${{ hashFiles('Cargo.lock') }}
- run: cargo fetch
- uses: actions/cache@v3
with:
path: target
key: check-wasm32-target-${{ runner.os }}-${{ steps.rust-version.outputs.version }}-${{ hashFiles('Cargo.lock') }}
- run: cargo check --target wasm32-unknown-unknown --manifest-path tokio-postgres/Cargo.toml --no-default-features --features js

test:
name: test
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ members = [
"postgres-native-tls",
"postgres-openssl",
"postgres-protocol",
"postgres-replication",
"postgres-types",
"tokio-postgres",
]
resolver = "2"

[profile.release]
debug = 2
31 changes: 31 additions & 0 deletions postgres-derive-test/src/compile-fail/invalid-allow-mismatch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use postgres_types::{FromSql, ToSql};

#[derive(ToSql, Debug)]
#[postgres(allow_mismatch)]
struct ToSqlAllowMismatchStruct {
a: i32,
}

#[derive(FromSql, Debug)]
#[postgres(allow_mismatch)]
struct FromSqlAllowMismatchStruct {
a: i32,
}

#[derive(ToSql, Debug)]
#[postgres(allow_mismatch)]
struct ToSqlAllowMismatchTupleStruct(i32, i32);

#[derive(FromSql, Debug)]
#[postgres(allow_mismatch)]
struct FromSqlAllowMismatchTupleStruct(i32, i32);

#[derive(FromSql, Debug)]
#[postgres(transparent, allow_mismatch)]
struct TransparentFromSqlAllowMismatchStruct(i32);

#[derive(FromSql, Debug)]
#[postgres(allow_mismatch, transparent)]
struct AllowMismatchFromSqlTransparentStruct(i32);

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
error: #[postgres(allow_mismatch)] may only be applied to enums
--> src/compile-fail/invalid-allow-mismatch.rs:4:1
|
4 | / #[postgres(allow_mismatch)]
5 | | struct ToSqlAllowMismatchStruct {
6 | | a: i32,
7 | | }
| |_^

error: #[postgres(allow_mismatch)] may only be applied to enums
--> src/compile-fail/invalid-allow-mismatch.rs:10:1
|
10 | / #[postgres(allow_mismatch)]
11 | | struct FromSqlAllowMismatchStruct {
12 | | a: i32,
13 | | }
| |_^

error: #[postgres(allow_mismatch)] may only be applied to enums
--> src/compile-fail/invalid-allow-mismatch.rs:16:1
|
16 | / #[postgres(allow_mismatch)]
17 | | struct ToSqlAllowMismatchTupleStruct(i32, i32);
| |_______________________________________________^

error: #[postgres(allow_mismatch)] may only be applied to enums
--> src/compile-fail/invalid-allow-mismatch.rs:20:1
|
20 | / #[postgres(allow_mismatch)]
21 | | struct FromSqlAllowMismatchTupleStruct(i32, i32);
| |_________________________________________________^

error: #[postgres(transparent)] is not allowed with #[postgres(allow_mismatch)]
--> src/compile-fail/invalid-allow-mismatch.rs:24:25
|
24 | #[postgres(transparent, allow_mismatch)]
| ^^^^^^^^^^^^^^

error: #[postgres(allow_mismatch)] is not allowed with #[postgres(transparent)]
--> src/compile-fail/invalid-allow-mismatch.rs:28:28
|
28 | #[postgres(allow_mismatch, transparent)]
| ^^^^^^^^^^^
43 changes: 43 additions & 0 deletions postgres-derive-test/src/composites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,49 @@ fn name_overrides() {
);
}

#[test]
fn rename_all_overrides() {
#[derive(FromSql, ToSql, Debug, PartialEq)]
#[postgres(name = "inventory_item", rename_all = "SCREAMING_SNAKE_CASE")]
struct InventoryItem {
name: String,
supplier_id: i32,
#[postgres(name = "Price")]
price: Option<f64>,
}

let mut conn = Client::connect("user=postgres host=localhost port=5433", NoTls).unwrap();
conn.batch_execute(
"CREATE TYPE pg_temp.inventory_item AS (
\"NAME\" TEXT,
\"SUPPLIER_ID\" INT,
\"Price\" DOUBLE PRECISION
);",
)
.unwrap();

let item = InventoryItem {
name: "foobar".to_owned(),
supplier_id: 100,
price: Some(15.50),
};

let item_null = InventoryItem {
name: "foobar".to_owned(),
supplier_id: 100,
price: None,
};

test_type(
&mut conn,
"inventory_item",
&[
(item, "ROW('foobar', 100, 15.50)"),
(item_null, "ROW('foobar', 100, NULL)"),
],
);
}

#[test]
fn wrong_name() {
#[derive(FromSql, ToSql, Debug, PartialEq)]
Expand Down
101 changes: 100 additions & 1 deletion postgres-derive-test/src/enums.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::test_type;
use postgres::{Client, NoTls};
use postgres::{error::DbError, Client, NoTls};
use postgres_types::{FromSql, ToSql, WrongType};
use std::error::Error;

Expand Down Expand Up @@ -53,6 +53,35 @@ fn name_overrides() {
);
}

#[test]
fn rename_all_overrides() {
#[derive(Debug, ToSql, FromSql, PartialEq)]
#[postgres(name = "mood", rename_all = "snake_case")]
enum Mood {
VerySad,
#[postgres(name = "okay")]
Ok,
VeryHappy,
}

let mut conn = Client::connect("user=postgres host=localhost port=5433", NoTls).unwrap();
conn.execute(
"CREATE TYPE pg_temp.mood AS ENUM ('very_sad', 'okay', 'very_happy')",
&[],
)
.unwrap();

test_type(
&mut conn,
"mood",
&[
(Mood::VerySad, "'very_sad'"),
(Mood::Ok, "'okay'"),
(Mood::VeryHappy, "'very_happy'"),
],
);
}

#[test]
fn wrong_name() {
#[derive(Debug, ToSql, FromSql, PartialEq)]
Expand Down Expand Up @@ -102,3 +131,73 @@ fn missing_variant() {
let err = conn.execute("SELECT $1::foo", &[&Foo::Bar]).unwrap_err();
assert!(err.source().unwrap().is::<WrongType>());
}

#[test]
fn allow_mismatch_enums() {
#[derive(Debug, ToSql, FromSql, PartialEq)]
#[postgres(allow_mismatch)]
enum Foo {
Bar,
}

let mut conn = Client::connect("user=postgres host=localhost port=5433", NoTls).unwrap();
conn.execute("CREATE TYPE pg_temp.\"Foo\" AS ENUM ('Bar', 'Baz')", &[])
.unwrap();

let row = conn.query_one("SELECT $1::\"Foo\"", &[&Foo::Bar]).unwrap();
assert_eq!(row.get::<_, Foo>(0), Foo::Bar);
}

#[test]
fn missing_enum_variant() {
#[derive(Debug, ToSql, FromSql, PartialEq)]
#[postgres(allow_mismatch)]
enum Foo {
Bar,
Buz,
}

let mut conn = Client::connect("user=postgres host=localhost port=5433", NoTls).unwrap();
conn.execute("CREATE TYPE pg_temp.\"Foo\" AS ENUM ('Bar', 'Baz')", &[])
.unwrap();

let err = conn
.query_one("SELECT $1::\"Foo\"", &[&Foo::Buz])
.unwrap_err();
assert!(err.source().unwrap().is::<DbError>());
}

#[test]
fn allow_mismatch_and_renaming() {
#[derive(Debug, ToSql, FromSql, PartialEq)]
#[postgres(name = "foo", allow_mismatch)]
enum Foo {
#[postgres(name = "bar")]
Bar,
#[postgres(name = "buz")]
Buz,
}

let mut conn = Client::connect("user=postgres host=localhost port=5433", NoTls).unwrap();
conn.execute("CREATE TYPE pg_temp.foo AS ENUM ('bar', 'baz', 'buz')", &[])
.unwrap();

let row = conn.query_one("SELECT $1::foo", &[&Foo::Buz]).unwrap();
assert_eq!(row.get::<_, Foo>(0), Foo::Buz);
}

#[test]
fn wrong_name_and_allow_mismatch() {
#[derive(Debug, ToSql, FromSql, PartialEq)]
#[postgres(allow_mismatch)]
enum Foo {
Bar,
}

let mut conn = Client::connect("user=postgres host=localhost port=5433", NoTls).unwrap();
conn.execute("CREATE TYPE pg_temp.foo AS ENUM ('Bar', 'Baz')", &[])
.unwrap();

let err = conn.query_one("SELECT $1::foo", &[&Foo::Bar]).unwrap_err();
assert!(err.source().unwrap().is::<WrongType>());
}
7 changes: 7 additions & 0 deletions postgres-derive/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

## v0.4.5 - 2023-08-19

### Added

* Added a `rename_all` option for enum and struct derives.
* Added an `allow_mismatch` option to disable strict enum variant checks against the Postgres type.

## v0.4.4 - 2023-03-27

### Changed
Expand Down
5 changes: 3 additions & 2 deletions postgres-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "postgres-derive"
version = "0.4.4"
version = "0.4.5"
authors = ["Steven Fackler <sfackler@palantir.com>"]
license = "MIT/Apache-2.0"
license = "MIT OR Apache-2.0"
edition = "2018"
description = "An internal crate used by postgres-types"
repository = "https://github.com/sfackler/rust-postgres"
Expand All @@ -15,3 +15,4 @@ test = false
syn = "2.0"
proc-macro2 = "1.0"
quote = "1.0"
heck = "0.5"
42 changes: 24 additions & 18 deletions postgres-derive/src/accepts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,37 @@ pub fn domain_body(name: &str, field: &syn::Field) -> TokenStream {
}
}

pub fn enum_body(name: &str, variants: &[Variant]) -> TokenStream {
pub fn enum_body(name: &str, variants: &[Variant], allow_mismatch: bool) -> TokenStream {
let num_variants = variants.len();
let variant_names = variants.iter().map(|v| &v.name);

quote! {
if type_.name() != #name {
return false;
if allow_mismatch {
quote! {
type_.name() == #name
}
} else {
quote! {
if type_.name() != #name {
return false;
}

match *type_.kind() {
::postgres_types::Kind::Enum(ref variants) => {
if variants.len() != #num_variants {
return false;
}

variants.iter().all(|v| {
match &**v {
#(
#variant_names => true,
)*
_ => false,
match *type_.kind() {
::postgres_types::Kind::Enum(ref variants) => {
if variants.len() != #num_variants {
return false;
}
})

variants.iter().all(|v| {
match &**v {
#(
#variant_names => true,
)*
_ => false,
}
})
}
_ => false,
}
_ => false,
}
}
}
Expand Down
Loading

0 comments on commit 37f1114

Please sign in to comment.