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

Add support of kebab-case enum with Postgres #755

Merged
merged 5 commits into from
Oct 20, 2020
Merged
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
2 changes: 2 additions & 0 deletions sqlx-macros/src/derives/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub enum RenameAll {
SnakeCase,
UpperCase,
ScreamingSnakeCase,
KebabCase,
}

pub struct SqlxContainerAttributes {
Expand Down Expand Up @@ -75,6 +76,7 @@ pub fn parse_container_attributes(input: &[Attribute]) -> syn::Result<SqlxContai
"snake_case" => RenameAll::SnakeCase,
"UPPERCASE" => RenameAll::UpperCase,
"SCREAMING_SNAKE_CASE" => RenameAll::ScreamingSnakeCase,
"kebab-case" => RenameAll::KebabCase,

_ => fail!(meta, "unexpected value for rename_all"),
};
Expand Down
3 changes: 2 additions & 1 deletion sqlx-macros/src/derives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub(crate) use r#type::expand_derive_type;
pub(crate) use row::expand_derive_from_row;

use self::attributes::RenameAll;
use heck::{ShoutySnakeCase, SnakeCase};
use heck::{KebabCase, ShoutySnakeCase, SnakeCase};
use std::iter::FromIterator;
use syn::DeriveInput;

Expand All @@ -34,5 +34,6 @@ pub(crate) fn rename_all(s: &str, pattern: RenameAll) -> String {
RenameAll::SnakeCase => s.to_snake_case(),
RenameAll::UpperCase => s.to_uppercase(),
RenameAll::ScreamingSnakeCase => s.to_shouty_snake_case(),
RenameAll::KebabCase => s.to_kebab_case(),
}
}
22 changes: 22 additions & 0 deletions tests/postgres/derives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ enum ColorScreamingSnake {
BlueBlack,
}

#[derive(PartialEq, Debug, sqlx::Type)]
#[sqlx(rename = "color-kebab-case")]
#[sqlx(rename_all = "kebab-case")]
enum ColorKebabCase {
RedGreen,
BlueBlack,
}

// "Strong" enum can map to a custom type
#[derive(PartialEq, Debug, sqlx::Type)]
#[sqlx(rename = "mood")]
Expand Down Expand Up @@ -133,11 +141,13 @@ DROP TYPE IF EXISTS color_lower CASCADE;
DROP TYPE IF EXISTS color_snake CASCADE;
DROP TYPE IF EXISTS color_upper CASCADE;
DROP TYPE IF EXISTS color_screaming_snake CASCADE;
DROP TYPE IF EXISTS "color-kebab-case" CASCADE;

CREATE TYPE color_lower AS ENUM ( 'red', 'green', 'blue' );
CREATE TYPE color_snake AS ENUM ( 'red_green', 'blue_black' );
CREATE TYPE color_upper AS ENUM ( 'RED', 'GREEN', 'BLUE' );
CREATE TYPE color_screaming_snake AS ENUM ( 'RED_GREEN', 'BLUE_BLACK' );
CREATE TYPE "color-kebab-case" AS ENUM ( 'red-green', 'blue-black' );

CREATE TABLE people (
id serial PRIMARY KEY,
Expand Down Expand Up @@ -264,6 +274,18 @@ SELECT id, mood FROM people WHERE id = $1
assert!(rec.0);
assert_eq!(rec.1, ColorScreamingSnake::RedGreen);

let rec: (bool, ColorKebabCase) = sqlx::query_as(
"
SELECT $1 = 'red-green'::\"color-kebab-case\", $1
",
)
.bind(&ColorKebabCase::RedGreen)
.fetch_one(&mut conn)
.await?;

assert!(rec.0);
assert_eq!(rec.1, ColorKebabCase::RedGreen);

Ok(())
}

Expand Down