Skip to content

Commit

Permalink
refactor; type register for better custom type support
Browse files Browse the repository at this point in the history
  • Loading branch information
beanpuppy committed Feb 15, 2025
1 parent 41eb048 commit e3b128e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 45 deletions.
6 changes: 1 addition & 5 deletions benches/execution/diesel_benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,8 @@ pub fn bench_insert(b: &mut Bencher, conn: &mut PgConnection, size: usize) {
1000 => |conn| insert_users::<_, 1000>(conn, hair_color_callback),
_ => unimplemented!(),
};
let insert = &insert;

b.iter(|| {
let insert = insert;
insert(conn)
})
b.iter(|| insert(conn))
}

pub fn loading_associations_sequentially(b: &mut Bencher, conn: &mut PgConnection) {
Expand Down
6 changes: 0 additions & 6 deletions clorinde/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ pub enum TypeMapping {
is_copy: bool,
#[serde(rename = "is-params", default = "default_true")]
is_params: bool,
#[serde(default = "default_kind")]
kind: String,
},
}

Expand Down Expand Up @@ -300,10 +298,6 @@ fn default_destination() -> PathBuf {
PathBuf::from_str("clorinde").unwrap()
}

fn default_kind() -> String {
"simple".to_string()
}

fn default_name() -> String {
"clorinde".to_string()
}
Expand Down
73 changes: 40 additions & 33 deletions clorinde/src/type_registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,15 @@ impl TypeRegistrar {
}
}

pub(crate) fn register(
fn resolve_type(
&mut self,
name: &str,
ty: &Type,
name: &str,
query_name: &Span<String>,
module_info: &ModuleInfo,
default_is_copy: bool,
default_is_params: bool,
) -> Result<&Rc<ClorindeType>, Error> {
self.dependency_analysis.analyse(ty);
fn custom(ty: &Type, is_copy: bool, is_params: bool) -> ClorindeType {
let rust_ty_name = ty.name().to_upper_camel_case();
ClorindeType::Custom {
Expand All @@ -359,34 +360,6 @@ impl TypeRegistrar {
}
}

// check if there's a user-defined mapping first
if let Some(mapping) = self.config.clone().get_type_mapping(ty) {
return Ok(match mapping {
TypeMapping::Simple(name) => self.insert(ty, || ClorindeType::Simple {
pg_ty: ty.clone(),
rust_name: name.to_string(),
is_copy: true,
}),
TypeMapping::Detailed {
rust_type,
is_copy,
is_params: _,
kind,
} => self.insert(ty, move || match kind.as_ref() {
"simple" => ClorindeType::Simple {
pg_ty: ty.clone(),
rust_name: rust_type.clone(),
is_copy: *is_copy,
},
_ => unimplemented!(),
}),
});
}

if let Some(idx) = self.types.get_index_of(&SchemaKey::from(ty)) {
return Ok(&self.types[idx]);
}

Ok(match ty.kind() {
Kind::Enum(_) => self.insert(ty, || custom(ty, true, true)),
Kind::Array(inner_ty) => {
Expand All @@ -404,8 +377,8 @@ impl TypeRegistrar {
self.insert(ty, || domain(ty, inner.clone()))
}
Kind::Composite(composite_fields) => {
let mut is_copy = true;
let mut is_params = true;
let mut is_copy = default_is_copy;
let mut is_params = default_is_params;
for field in composite_fields {
let field_ty = self.register(name, field.type_(), query_name, module_info)?;
is_copy &= field_ty.is_copy();
Expand Down Expand Up @@ -467,6 +440,40 @@ impl TypeRegistrar {
})
}

pub(crate) fn register(
&mut self,
name: &str,
ty: &Type,
query_name: &Span<String>,
module_info: &ModuleInfo,
) -> Result<&Rc<ClorindeType>, Error> {
self.dependency_analysis.analyse(ty);

if let Some(idx) = self.types.get_index_of(&SchemaKey::from(ty)) {
return Ok(&self.types[idx]);
}

// check if there's a user-defined mapping first
if let Some(mapping) = self.config.clone().get_type_mapping(ty) {
return Ok(match mapping {
TypeMapping::Simple(name) => self.insert(ty, || ClorindeType::Simple {
pg_ty: ty.clone(),
rust_name: name.to_string(),
is_copy: true,
}),
TypeMapping::Detailed {
rust_type,
is_copy,
is_params,
} => {
self.resolve_type(ty, rust_type, query_name, module_info, *is_copy, *is_params)?
}
});
}

self.resolve_type(ty, name, query_name, module_info, true, true)
}

pub(crate) fn ref_of(&self, ty: &Type) -> Rc<ClorindeType> {
self.types
.get(&SchemaKey::from(ty))
Expand Down
1 change: 0 additions & 1 deletion examples/custom_types/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,3 @@ CREATE TABLE SpongeBobVoiceActor (

INSERT INTO SpongeBobVoiceActor (voice_actor, character)
VALUES (ROW ('Bill Fagerbakke', 65), 'Patrick');

0 comments on commit e3b128e

Please sign in to comment.