Skip to content

Commit 3fa88b4

Browse files
authored
Merge pull request diesel-rs#2060 from NicholasLYang/camel-case-conversion
Camel case conversion
2 parents 3f10458 + 3d57da5 commit 3fa88b4

8 files changed

Lines changed: 42 additions & 12 deletions

File tree

diesel_cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ chrono = "0.4"
2020
clap = "2.27"
2121
diesel = { version = "~1.4.0", default-features = false }
2222
dotenv = ">=0.8, <0.11"
23+
heck = "0.3.1"
2324
migrations_internals = "~1.4.0"
2425
serde = { version = "1.0.0", features = ["derive"] }
2526
tempfile = "3.0.0"

diesel_cli/src/infer_schema_internals/mysql.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use diesel::mysql::Mysql;
22
use diesel::*;
3+
use heck::CamelCase;
34
use std::error::Error;
45

56
use super::data_structures::*;
@@ -84,7 +85,7 @@ pub fn determine_column_type(attr: &ColumnInformation) -> Result<ColumnType, Box
8485
let unsigned = determine_unsigned(&attr.type_name);
8586

8687
Ok(ColumnType {
87-
rust_name: capitalize(tpe.trim()),
88+
rust_name: tpe.trim().to_camel_case(),
8889
is_array: false,
8990
is_nullable: attr.nullable,
9091
is_unsigned: unsigned,
@@ -119,10 +120,6 @@ fn determine_unsigned(sql_type_name: &str) -> bool {
119120
sql_type_name.to_lowercase().contains("unsigned")
120121
}
121122

122-
fn capitalize(name: &str) -> String {
123-
name[..1].to_uppercase() + &name[1..]
124-
}
125-
126123
#[test]
127124
fn values_which_already_map_to_type_are_returned_unchanged() {
128125
assert_eq!("text", determine_type_name("text").unwrap());

diesel_cli/src/infer_schema_internals/pg.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use super::data_structures::*;
2+
use heck::CamelCase;
13
use std::error::Error;
24
use std::io::{stderr, Write};
35

4-
use super::data_structures::*;
5-
66
pub fn determine_column_type(attr: &ColumnInformation) -> Result<ColumnType, Box<Error>> {
77
let is_array = attr.type_name.starts_with('_');
88
let tpe = if is_array {
@@ -28,13 +28,9 @@ pub fn determine_column_type(attr: &ColumnInformation) -> Result<ColumnType, Box
2828
}
2929

3030
Ok(ColumnType {
31-
rust_name: capitalize(tpe),
31+
rust_name: tpe.to_camel_case(),
3232
is_array,
3333
is_nullable: attr.nullable,
3434
is_unsigned: false,
3535
})
3636
}
37-
38-
fn capitalize(name: &str) -> String {
39-
name[..1].to_uppercase() + &name[1..]
40-
}

diesel_cli/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extern crate clap;
2020
#[macro_use]
2121
extern crate diesel;
2222
extern crate dotenv;
23+
extern crate heck;
2324
extern crate migrations_internals;
2425
#[macro_use]
2526
extern crate serde;

diesel_cli/tests/print_schema.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ fn print_schema_column_renaming() {
6969
test_print_schema("print_schema_column_renaming", vec!["--with-docs"]);
7070
}
7171

72+
#[test]
73+
#[cfg(feature = "postgres")]
74+
fn print_schema_type_renaming() {
75+
test_print_schema("print_schema_type_renaming", vec!["--with-docs"]);
76+
}
77+
7278
#[test]
7379
#[cfg(feature = "mysql")]
7480
fn print_schema_unsigned() {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[print_schema]
2+
file = "src/schema.rs"
3+
with_docs = true
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
table! {
2+
/// Representation of the `users` table.
3+
///
4+
/// (Automatically generated by Diesel.)
5+
users (id) {
6+
/// The `id` column of the `users` table.
7+
///
8+
/// Its SQL type is `Int4`.
9+
///
10+
/// (Automatically generated by Diesel.)
11+
id -> Int4,
12+
/// The `job` column of the `users` table.
13+
///
14+
/// Its SQL type is `UserJob`.
15+
///
16+
/// (Automatically generated by Diesel.)
17+
job -> UserJob,
18+
}
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TYPE user_job AS ENUM ('programmer', 'director', 'writer', 'mathematician');
2+
3+
CREATE TABLE users (
4+
id INTEGER PRIMARY KEY,
5+
job user_job NOT NULL
6+
);
7+

0 commit comments

Comments
 (0)