Skip to content

Commit

Permalink
fix(code): support for unsigned on nullable fields (#67)
Browse files Browse the repository at this point in the history
The unsigned support introduced in
b41e14a#r125954859 was incomplete as it
did not cover the case where the unsigned field also is nullable. This
change updates the code to convert the base type to unsigned in both
cases.

Fixes #65
  • Loading branch information
longsleep authored Aug 31, 2023
1 parent 22b795f commit 5b8cc31
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,16 @@ impl<'a> Struct<'a> {
})
.map(|c| {
let name = c.name.to_string();
let base_type = if c.is_nullable {
format!("Option<{}>", c.ty)
} else if c.is_unsigned {
let base_type = if c.is_unsigned {
c.ty.replace('i', "u")
} else {
c.ty.clone()
};
let base_type = if c.is_nullable {
format!("Option<{}>", base_type)
} else {
base_type
};
let mut is_optional = false;

let is_pk = self
Expand Down
3 changes: 3 additions & 0 deletions test/simple_table/models/todos/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Connection = diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager
pub struct Todo {
pub id: i32,
pub unsigned: u32,
pub unsigned_nullable: Option<u32>,
pub text: String,
pub completed: bool,
pub type_: String,
Expand All @@ -24,6 +25,7 @@ pub struct Todo {
#[diesel(table_name=todos)]
pub struct CreateTodo {
pub unsigned: u32,
pub unsigned_nullable: Option<u32>,
pub text: String,
pub completed: bool,
pub type_: String,
Expand All @@ -33,6 +35,7 @@ pub struct CreateTodo {
#[diesel(table_name=todos)]
pub struct UpdateTodo {
pub unsigned: Option<u32>,
pub unsigned_nullable: Option<Option<u32>>,
pub text: Option<String>,
pub completed: Option<bool>,
pub type_: Option<String>,
Expand Down
1 change: 1 addition & 0 deletions test/simple_table/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ diesel::table! {
todos (id) {
id -> Int4,
unsigned -> Unsigned<Integer>,
unsigned_nullable -> Nullable<Unsigned<Integer>>,
text -> Text,
completed -> Bool,
#[sql_name = "type"]
Expand Down

0 comments on commit 5b8cc31

Please sign in to comment.