Skip to content

Commit

Permalink
Merge pull request #66 from lerouxrgd/apache-avro-0.17
Browse files Browse the repository at this point in the history
Apache Avro 0.17
  • Loading branch information
lerouxrgd committed Aug 9, 2024
2 parents 0e44996 + 11b13e0 commit 852987b
Show file tree
Hide file tree
Showing 23 changed files with 776 additions and 407 deletions.
538 changes: 257 additions & 281 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@ license = "MIT"
readme = "README.md"

[dependencies]
apache-avro = { version = "0.16", features = ["derive"] }
apache-avro = { version = "0.17", features = ["derive"] }
clap = { version = "4", features = ["derive"], optional = true }
glob = "0.3"
heck = "0.4"
heck = "0.5"
lazy_static = "1"
serde = { version = "1", features = ["serde_derive"] }
serde_json = "1"
tempfile = "3"
tera = { version = "1", default-features = false }
thiserror = "1"
uuid = { version = "1", features = ["serde", "v4"] }

[dev-dependencies]
chrono = { version = "0.4", default-features = false, features = ["serde"] }
derive_builder = "0.12"
derive_builder = "0.20"
pretty_assertions = "1"
serde_bytes = "0.11"
tempfile = "3"

[profile.release]
Expand Down
6 changes: 0 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ impl From<tera::Error> for Error {
}
}

impl From<uuid::Error> for Error {
fn from(source: uuid::Error) -> Self {
Error::Template(source.to_string())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
35 changes: 25 additions & 10 deletions src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::{HashMap, VecDeque};
use std::fs;
use std::io::prelude::*;

use apache_avro::schema::{DecimalSchema, RecordField, RecordSchema};
use apache_avro::schema::{ArraySchema, DecimalSchema, MapSchema, RecordField, RecordSchema};

use crate::error::{Error, Result};
use crate::templates::*;
Expand Down Expand Up @@ -107,11 +107,15 @@ impl Generator {
}

// Register inner type for it to be used as a nested type later
Schema::Array(ref inner) => {
Schema::Array(ArraySchema {
items: ref inner, ..
}) => {
let type_str = array_type(inner, &gs)?;
gs.put_type(&s, type_str)
}
Schema::Map(ref inner) => {
Schema::Map(MapSchema {
types: ref inner, ..
}) => {
let type_str = map_type(inner, &gs)?;
gs.put_type(&s, type_str)
}
Expand Down Expand Up @@ -161,7 +165,7 @@ fn deps_stack(schema: &Schema, mut deps: Vec<Schema>) -> Vec<Schema> {
Schema::Enum { .. } => push_unique(&mut deps, s.clone()),
Schema::Fixed { .. } => push_unique(&mut deps, s.clone()),
Schema::Decimal(DecimalSchema { inner, .. })
if matches!(**inner, Schema::Fixed { .. }) =>
if matches!(inner.as_ref(), Schema::Fixed { .. }) =>
{
push_unique(&mut deps, s.clone())
}
Expand All @@ -185,13 +189,17 @@ fn deps_stack(schema: &Schema, mut deps: Vec<Schema>) -> Vec<Schema> {
Schema::Record { .. } => q.push_back(sr),

// Push to the exploration queue, depending on the inner schema format
Schema::Map(sc) | Schema::Array(sc) => match sc.as_ref() {
Schema::Map(MapSchema { types: sc, .. })
| Schema::Array(ArraySchema { items: sc, .. }) => match sc.as_ref() {
Schema::Fixed { .. }
| Schema::Enum { .. }
| Schema::Record { .. }
| Schema::Map(..)
| Schema::Array(..)
| Schema::Union(..) => q.push_back(sc),
| Schema::Union(..) => {
q.push_back(sc);
push_unique(&mut deps, s.clone());
}
_ => (),
},
Schema::Union(union) => {
Expand Down Expand Up @@ -222,14 +230,18 @@ fn deps_stack(schema: &Schema, mut deps: Vec<Schema>) -> Vec<Schema> {
}

// Depending on the inner schema type ...
Schema::Map(sc) | Schema::Array(sc) => match &**sc {
Schema::Map(MapSchema { types: sc, .. })
| Schema::Array(ArraySchema { items: sc, .. }) => match sc.as_ref() {
// ... Needs further checks, push to the exploration queue
Schema::Fixed { .. }
| Schema::Enum { .. }
| Schema::Record { .. }
| Schema::Map(..)
| Schema::Array(..)
| Schema::Union(..) => q.push_back(&**sc),
| Schema::Union(..) => {
q.push_back(sc.as_ref());
push_unique(&mut deps, s.clone());
}
// ... Not nested, can be pushed to the result stack
_ => push_unique(&mut deps, s.clone()),
},
Expand All @@ -248,7 +260,10 @@ fn deps_stack(schema: &Schema, mut deps: Vec<Schema>) -> Vec<Schema> {
| Schema::Record { .. }
| Schema::Map(..)
| Schema::Array(..)
| Schema::Union(..) => q.push_back(sc),
| Schema::Union(..) => {
q.push_back(sc);
push_unique(&mut deps, s.clone());
}
// ... Not nested, can be pushed to the result stack
_ => push_unique(&mut deps, s.clone()),
});
Expand Down Expand Up @@ -352,6 +367,7 @@ impl GeneratorBuilder {
#[cfg(test)]
mod tests {
use apache_avro::schema::{EnumSchema, Name};
use pretty_assertions::assert_eq;

use super::*;

Expand Down Expand Up @@ -447,7 +463,6 @@ pub struct A {
let mut buf = vec![];
g.gen(&source, &mut buf)?;
let res = String::from_utf8(buf)?;
println!("{}", res);

assert_eq!(expected, res);

Expand Down
Loading

0 comments on commit 852987b

Please sign in to comment.