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 JsonBinary attribute #1346

Merged
merged 4 commits into from
Jan 18, 2023
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
1 change: 1 addition & 0 deletions sea-orm-codegen/src/entity/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl Column {
ColumnType::Decimal(Some((p, s))) => Some(format!("Decimal(Some(({}, {})))", p, s)),
ColumnType::Money(Some((p, s))) => Some(format!("Money(Some({}, {}))", p, s)),
ColumnType::Text => Some("Text".to_owned()),
ColumnType::JsonBinary => Some("JsonBinary".to_owned()),
ColumnType::Custom(iden) => {
Some(format!("Custom(\"{}\".to_owned())", iden.to_string()))
}
Expand Down
113 changes: 113 additions & 0 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1957,4 +1957,117 @@ mod tests {

Ok(expected.to_string())
}

#[test]
fn test_gen_postgres() -> io::Result<()> {
let entities = vec![
// This tests that the JsonBinary column type is annotated
// correctly in compact entity form. More information can be found
// in this issue:
//
// https://github.com/SeaQL/sea-orm/issues/1344
Entity {
table_name: "task".to_owned(),
columns: vec![
Column {
name: "id".to_owned(),
col_type: ColumnType::Integer,
auto_increment: true,
not_null: true,
unique: false,
},
Column {
name: "payload".to_owned(),
col_type: ColumnType::Json,
auto_increment: false,
not_null: true,
unique: false,
},
Column {
name: "payload_binary".to_owned(),
col_type: ColumnType::JsonBinary,
auto_increment: false,
not_null: true,
unique: false,
},
],
relations: vec![],
conjunct_relations: vec![],
primary_keys: vec![PrimaryKey {
name: "id".to_owned(),
}],
},
];
const ENTITY_FILES: [&str; 1] = [include_str!("../../tests/postgres/binary_json.rs")];

const ENTITY_FILES_EXPANDED: [&str; 1] =
[include_str!("../../tests/postgres/binary_json_expanded.rs")];

assert_eq!(entities.len(), ENTITY_FILES.len());

for (i, entity) in entities.iter().enumerate() {
assert_eq!(
parse_from_file(ENTITY_FILES[i].as_bytes())?.to_string(),
EntityWriter::gen_compact_code_blocks(
entity,
&crate::WithSerde::None,
&crate::DateTimeCrate::Chrono,
&None,
false,
false,
&TokenStream::new(),
&TokenStream::new(),
)
.into_iter()
.skip(1)
.fold(TokenStream::new(), |mut acc, tok| {
acc.extend(tok);
acc
})
.to_string()
);
assert_eq!(
parse_from_file(ENTITY_FILES[i].as_bytes())?.to_string(),
EntityWriter::gen_compact_code_blocks(
entity,
&crate::WithSerde::None,
&crate::DateTimeCrate::Chrono,
&Some("public".to_owned()),
false,
false,
&TokenStream::new(),
&TokenStream::new(),
)
.into_iter()
.skip(1)
.fold(TokenStream::new(), |mut acc, tok| {
acc.extend(tok);
acc
})
.to_string()
);
assert_eq!(
parse_from_file(ENTITY_FILES_EXPANDED[i].as_bytes())?.to_string(),
EntityWriter::gen_expanded_code_blocks(
entity,
&crate::WithSerde::None,
&crate::DateTimeCrate::Chrono,
&Some("schema_name".to_owned()),
false,
false,
&TokenStream::new(),
&TokenStream::new(),
)
.into_iter()
.skip(1)
.fold(TokenStream::new(), |mut acc, tok| {
acc.extend(tok);
acc
})
.to_string()
);
}

Ok(())
}
}
21 changes: 21 additions & 0 deletions sea-orm-codegen/tests/postgres/binary_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0
//!
//! This file tests that the JsonBinary column type is annotated correctly is
//! compact entity form. More information can be found in this issue:
//!
//! https://github.com/SeaQL/sea-orm/issues/1344

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "task")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub payload: Json,
#[sea_orm(column_type = "JsonBinary")]
pub payload_binary: Json,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
65 changes: 65 additions & 0 deletions sea-orm-codegen/tests/postgres/binary_json_expanded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0
//!
//! This file tests that the JsonBinary column type is annotated correctly is
//! expanded entity form. More information can be found in this issue:
//!
//! https://github.com/SeaQL/sea-orm/issues/1344

use sea_orm::entity::prelude::*;

#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
pub struct Entity;
impl EntityName for Entity {
fn schema_name(&self) -> Option< &str > {
Some("schema_name")
}
fn table_name(&self) -> &str {
"task"
}
}

#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Eq)]
pub struct Model {
pub id: i32,
pub payload: Json,
pub payload_binary: Json,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
Id,
Payload,
PayloadBinary,
}

#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
pub enum PrimaryKey {
Id,
}

impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}
}

#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {}
impl ColumnTrait for Column {
type EntityName = Entity;
fn def(&self) -> ColumnDef {
match self {
Self::Id => ColumnType::Integer.def(),
// This is the part that is being tested.
Self::Payload => ColumnType::Json.def(),
Self::PayloadBinary => ColumnType::JsonBinary.def(),
}
}
}
impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
panic!("No RelationDef")
}
}
impl ActiveModelBehavior for ActiveModel {}