Skip to content

Commit

Permalink
Adapt code to apache-avro 0.16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lerouxrgd committed Jan 10, 2024
1 parent 92f6a77 commit 52ffe5d
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 229 deletions.
429 changes: 231 additions & 198 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rsgen-avro"
version = "0.13.0"
version = "0.14.0"
authors = ["Romain Leroux <[email protected]>"]
edition = "2021"
description = "Command line and library for generating Rust types from Avro schemas"
Expand Down
46 changes: 34 additions & 12 deletions src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,9 @@ impl Templater {
}
}

Schema::TimeMillis | Schema::TimestampMillis if self.use_chrono_dates => {
Schema::TimeMillis | Schema::TimestampMillis | Schema::LocalTimestampMillis
if self.use_chrono_dates =>
{
f.push(name_std.clone());
t.insert(name_std.clone(), "chrono::NaiveDateTime".to_string());
w.insert(name_std.clone(), "chrono::naive::serde::ts_milliseconds");
Expand All @@ -511,7 +513,9 @@ impl Templater {
}
}

Schema::TimeMicros | Schema::TimestampMicros if self.use_chrono_dates => {
Schema::TimeMicros | Schema::TimestampMicros | Schema::LocalTimestampMicros
if self.use_chrono_dates =>
{
f.push(name_std.clone());
t.insert(name_std.clone(), "chrono::NaiveDateTime".to_string());
w.insert(name_std.clone(), "chrono::naive::serde::ts_microseconds");
Expand All @@ -533,7 +537,9 @@ impl Templater {
Schema::Long
| Schema::TimeMicros
| Schema::TimestampMillis
| Schema::TimestampMicros => {
| Schema::LocalTimestampMillis
| Schema::TimestampMicros
| Schema::LocalTimestampMicros => {
f.push(name_std.clone());
t.insert(name_std.clone(), "i64".to_string());
if let Some(default) = default {
Expand Down Expand Up @@ -786,6 +792,8 @@ impl Templater {
| Schema::TimeMicros
| Schema::TimestampMillis
| Schema::TimestampMicros
| Schema::LocalTimestampMillis
| Schema::LocalTimestampMicros
if self.use_chrono_dates =>
{
"NaiveDateTime(chrono::NaiveDateTime)".into()
Expand All @@ -795,6 +803,8 @@ impl Templater {
Schema::TimeMicros => "TimeMicros(i64)".into(),
Schema::TimestampMillis => "TimestampMillis(i64)".into(),
Schema::TimestampMicros => "TimestampMicros(i64)".into(),
Schema::LocalTimestampMillis => "LocalTimestampMillis(i64)".into(),
Schema::LocalTimestampMicros => "LocalTimestampMicros(i64)".into(),
Schema::Duration => "Duration(apache_avro::Duration)".into(),
Schema::Null => err!(
"Invalid Schema::Null not in first position on an UnionSchema variants"
Expand Down Expand Up @@ -883,7 +893,9 @@ impl Templater {
_ => err!("Invalid default: {:?}", default)?,
},

Schema::TimeMillis | Schema::TimestampMillis if self.use_chrono_dates => {
Schema::TimeMillis | Schema::TimestampMillis | Schema::LocalTimestampMillis
if self.use_chrono_dates =>
{
match default {
Value::Number(n) if n.is_i64() => format!(
"chrono::NaiveDateTime::from_timestamp_millis({}).unwrap()",
Expand All @@ -893,7 +905,9 @@ impl Templater {
}
}

Schema::TimeMicros | Schema::TimestampMicros if self.use_chrono_dates => {
Schema::TimeMicros | Schema::TimestampMicros | Schema::LocalTimestampMicros
if self.use_chrono_dates =>
{
match default {
Value::Number(n) if n.is_i64() => format!(
"chrono::NaiveDateTime::from_timestamp_micros({}).unwrap()",
Expand All @@ -911,7 +925,9 @@ impl Templater {
Schema::Long
| Schema::TimeMicros
| Schema::TimestampMillis
| Schema::TimestampMicros => match default {
| Schema::TimestampMicros
| Schema::LocalTimestampMillis
| Schema::LocalTimestampMicros => match default {
Value::Number(n) if n.is_i64() => n.to_string(),
_ => err!("Invalid default: {:?}", default)?,
},
Expand Down Expand Up @@ -1189,8 +1205,8 @@ pub(crate) fn array_type(inner: &Schema, gen_state: &GenState) -> Result<String>
Schema::Date => "Vec<i32>".into(),
Schema::TimeMillis => "Vec<i32>".into(),
Schema::TimeMicros => "Vec<i64>".into(),
Schema::TimestampMillis => "Vec<i64>".into(),
Schema::TimestampMicros => "Vec<i64>".into(),
Schema::TimestampMillis | Schema::LocalTimestampMillis => "Vec<i64>".into(),
Schema::TimestampMicros | Schema::LocalTimestampMicros => "Vec<i64>".into(),

Schema::Uuid => "Vec<uuid::Uuid>".into(),
Schema::Decimal { .. } => "Vec<apache_avro::Decimal>".into(),
Expand Down Expand Up @@ -1252,6 +1268,8 @@ pub(crate) fn map_type(inner: &Schema, gen_state: &GenState) -> Result<String> {
| Schema::TimeMicros
| Schema::TimestampMillis
| Schema::TimestampMicros
| Schema::LocalTimestampMillis
| Schema::LocalTimestampMicros
if gen_state.use_chrono_dates =>
{
map_of("chrono::NaiveDateTime")
Expand All @@ -1260,8 +1278,8 @@ pub(crate) fn map_type(inner: &Schema, gen_state: &GenState) -> Result<String> {
Schema::Date => map_of("i32"),
Schema::TimeMillis => map_of("i32"),
Schema::TimeMicros => map_of("i64"),
Schema::TimestampMillis => map_of("i64"),
Schema::TimestampMicros => map_of("i64"),
Schema::TimestampMillis | Schema::LocalTimestampMillis => map_of("i64"),
Schema::TimestampMicros | Schema::LocalTimestampMicros => map_of("i64"),

Schema::Uuid => map_of("uuid::Uuid"),
Schema::Decimal { .. } => map_of("apache_avro::Decimal"),
Expand Down Expand Up @@ -1337,6 +1355,8 @@ fn union_enum_variant(schema: &Schema, gen_state: &GenState) -> Result<String> {
Schema::TimeMicros => "TimeMicros".into(),
Schema::TimestampMillis => "TimestampMillis".into(),
Schema::TimestampMicros => "TimestampMicros".into(),
Schema::LocalTimestampMillis => "LocalTimestampMillis".into(),
Schema::LocalTimestampMicros => "LocalTimestampMicros".into(),
Schema::Duration => "Duration".into(),
Schema::Null => {
err!("Invalid Schema::Null not in first position on an UnionSchema variants")?
Expand Down Expand Up @@ -1401,15 +1421,17 @@ pub(crate) fn option_type(inner: &Schema, gen_state: &GenState) -> Result<String
| Schema::TimeMicros
| Schema::TimestampMillis
| Schema::TimestampMicros
| Schema::LocalTimestampMillis
| Schema::LocalTimestampMicros
if gen_state.use_chrono_dates =>
{
"Option<chrono::NaiveDateTime>".into()
}
Schema::Date => "Option<i32>".into(),
Schema::TimeMillis => "Option<i32>".into(),
Schema::TimeMicros => "Option<i64>".into(),
Schema::TimestampMillis => "Option<i64>".into(),
Schema::TimestampMicros => "Option<i64>".into(),
Schema::TimestampMillis | Schema::LocalTimestampMillis => "Option<i64>".into(),
Schema::TimestampMicros | Schema::LocalTimestampMicros => "Option<i64>".into(),

Schema::Uuid => "Option<uuid::Uuid>".into(),
Schema::Decimal { .. } => "Option<apache_avro::Decimal>".into(),
Expand Down
2 changes: 1 addition & 1 deletion tests/schemas/complex.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{"name": "favorite_number", "type": "int", "default": 7},
{"name": "likes_pizza", "type": "boolean", "default": false},
{"name": "oye", "type": "float", "default": 1.1},
{"name": "aa-i32",
{"name": "aa_i32",
"type": {"type": "array", "items": {"type": "array", "items": "int"}},
"default": [[0], [12, -1]]}
]
Expand Down
1 change: 0 additions & 1 deletion tests/schemas/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub struct User {
pub favorite_number: i32,
pub likes_pizza: bool,
pub oye: f32,
#[serde(rename = "aa-i32")]
pub aa_i32: Vec<Vec<i32>>,
}

Expand Down
2 changes: 1 addition & 1 deletion tests/schemas/map_default.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"type": "record",
"name": "User",
"fields": [
{"name": "m-f64",
{"name": "m_f64",
"type": {"type": "map", "values": "double"},
"default": {"a": 12.0, "b": 42.1}}
]
Expand Down
1 change: 0 additions & 1 deletion tests/schemas/map_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#[derive(Debug, PartialEq, Clone, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct User {
#[serde(rename = "m-f64")]
pub m_f64: ::std::collections::HashMap<String, f64>,
}

Expand Down
2 changes: 1 addition & 1 deletion tests/schemas/nested_record_default.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"type": "record",
"name": "User",
"fields": [ {
"name": "m-f64",
"name": "m_f64",
"type": {
"type": "record",
"name": "Inner",
Expand Down
1 change: 0 additions & 1 deletion tests/schemas/nested_record_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub struct Inner {
#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct User {
#[serde(rename = "m-f64")]
pub m_f64: Inner,
}

Expand Down
2 changes: 1 addition & 1 deletion tests/schemas/nullable.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "test",
"fields": [
{"name": "a", "type": "long", "default": 42},
{"name": "b-b", "type": "string", "default": "na"},
{"name": "b_b", "type": "string", "default": "na"},
{"name": "c", "type": ["null", "int"], "default": null}
]
}
1 change: 0 additions & 1 deletion tests/schemas/nullable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
pub struct Test {
#[serde(deserialize_with = "nullable_test_a")]
pub a: i64,
#[serde(rename = "b-b")]
#[serde(deserialize_with = "nullable_test_b_b")]
pub b_b: String,
pub c: Option<i32>,
Expand Down
6 changes: 3 additions & 3 deletions tests/schemas/record.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
{"name": "likes_pizza", "type": "boolean", "default": false},
{"name": "b", "type": "bytes", "default": "\u00FF"},
{"name": "union_b", "type": ["null", "bytes"], "default": null},
{"name": "a-bool", "type": {"type": "array", "items": "boolean"}, "default": [true, false]},
{"name": "a-i32", "type": {"type": "array", "items": "int"}, "default": [12, -1]},
{"name": "m-f64", "type": {"type": "map", "values": "double"}}
{"name": "A_Bool", "type": {"type": "array", "items": "boolean"}, "default": [true, false]},
{"name": "SomeInteger", "type": {"type": "array", "items": "int"}, "default": [12, -1]},
{"name": "map_of_f64", "type": {"type": "map", "values": "double"}}
]
}
13 changes: 6 additions & 7 deletions tests/schemas/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ pub struct User {
#[serde(with = "serde_bytes")]
#[serde(default = "default_user_union_b")]
pub union_b: Option<Vec<u8>>,
#[serde(rename = "a-bool")]
#[serde(rename = "A_Bool")]
#[serde(default = "default_user_a_bool")]
pub a_bool: Vec<bool>,
#[serde(rename = "a-i32")]
#[serde(default = "default_user_a_i32")]
pub a_i32: Vec<i32>,
#[serde(rename = "m-f64")]
pub m_f64: ::std::collections::HashMap<String, f64>,
#[serde(rename = "SomeInteger")]
#[serde(default = "default_user_some_integer")]
pub some_integer: Vec<i32>,
pub map_of_f64: ::std::collections::HashMap<String, f64>,
}

#[inline(always)]
Expand All @@ -39,4 +38,4 @@ fn default_user_union_b() -> Option<Vec<u8>> { None }
fn default_user_a_bool() -> Vec<bool> { vec![true, false] }

#[inline(always)]
fn default_user_a_i32() -> Vec<i32> { vec![12, -1] }
fn default_user_some_integer() -> Vec<i32> { vec![12, -1] }

0 comments on commit 52ffe5d

Please sign in to comment.