diff --git a/src/templates.rs b/src/templates.rs index 98e55a5..6871842 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -15,14 +15,14 @@ use uuid::Uuid; use crate::error::{Error, Result}; pub const RECORD_TERA: &str = "record.tera"; -pub const RECORD_TEMPLATE: &str = r#" +pub const RECORD_TEMPLATE: &str = r##" {%- if doc %} {%- set doc_lines = doc | split(pat="\n") %} {%- for doc_line in doc_lines %} /// {{ doc_line }} {%- endfor %} {%- endif %} -#[derive(Debug, PartialEq{%- if is_eq_derivable %}, Eq{%- endif %}, Clone, serde::Deserialize, serde::Serialize{%- if derive_builders %}, derive_builder::Builder {%- endif %}{%- if derive_schemas %}, apache_avro::AvroSchema {%- endif %})] +#[derive(Debug, PartialEq{%- if is_eq_derivable %}, Eq{%- endif %}, Clone, serde::Deserialize, serde::Serialize{%- if derive_builders %}, derive_builder::Builder {%- endif %})] {%- if derive_builders %} #[builder(setter(into))] {%- endif %} @@ -49,6 +49,15 @@ pub struct {{ name }} { {%- endfor %} } +{%- if derive_schemas %} +{# #} +impl apache_avro::schema::derive::AvroSchemaComponent for {{ name }} { + fn get_schema_in_ctxt(_: &mut apache_avro::schema::Names, _: &apache_avro::schema::Namespace) -> apache_avro::Schema { + apache_avro::Schema::parse_str(r#"{{ schema_str }}"#).unwrap() + } +} +{%- endif %} + {%- for f in fields %} {%- set type = types[f] %} {%- if nullable and not type is starting_with("Option") %} @@ -84,7 +93,7 @@ impl Default for {{ name }} { } } {%- endif %} -"#; +"##; pub const ENUM_TERA: &str = "enum.tera"; pub const ENUM_TEMPLATE: &str = r#" @@ -418,6 +427,8 @@ impl Templater { ctx.insert("doc", doc); ctx.insert("derive_builders", &self.derive_builders); ctx.insert("derive_schemas", &self.derive_schemas); + let schema_str = serde_json::to_string(&schema).unwrap(); + ctx.insert("schema_str", &schema_str); let mut f = Vec::new(); // field names; let mut t = HashMap::new(); // field name -> field type diff --git a/tests/schemas/simple_with_schemas.rs b/tests/schemas/simple_with_schemas.rs index a929cac..e7d1689 100644 --- a/tests/schemas/simple_with_schemas.rs +++ b/tests/schemas/simple_with_schemas.rs @@ -1,10 +1,16 @@ -#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize, apache_avro::AvroSchema)] +#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize)] pub struct Test { #[serde(default = "default_test_a")] pub a: i64, pub b: String, } +impl apache_avro::schema::derive::AvroSchemaComponent for Test { + fn get_schema_in_ctxt(_: &mut apache_avro::schema::Names, _: &apache_avro::schema::Namespace) -> apache_avro::Schema { + apache_avro::Schema::parse_str(r#"{"type":"record","name":"test","fields":[{"name":"a","type":"long","default":42},{"name":"b","type":"string"}]}"#).unwrap() + } +} + #[inline(always)] fn default_test_a() -> i64 { 42 }