Skip to content

Commit

Permalink
WIP Better Avro Schema exposed on struct #44
Browse files Browse the repository at this point in the history
  • Loading branch information
lerouxrgd committed Jan 5, 2023
1 parent 9e3eb0b commit c290deb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand All @@ -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") %}
Expand Down Expand Up @@ -84,7 +93,7 @@ impl Default for {{ name }} {
}
}
{%- endif %}
"#;
"##;

pub const ENUM_TERA: &str = "enum.tera";
pub const ENUM_TEMPLATE: &str = r#"
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion tests/schemas/simple_with_schemas.rs
Original file line number Diff line number Diff line change
@@ -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 }

0 comments on commit c290deb

Please # to comment.