Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::io::prelude::*;
use apache_avro::schema::RecordField;

use crate::error::{Error, Result};
use crate::protocol::Protocol;
use crate::templates::*;
use crate::Schema;

Expand Down Expand Up @@ -71,8 +72,17 @@ impl Generator {
}
}

let schemas = &raw_schemas.iter().map(|s| s.as_str()).collect::<Vec<_>>();
let schemas = Schema::parse_list(schemas)?;
let schemas = raw_schemas
.iter()
.flat_map(|s| {
match Protocol::get_schemas(s) {
Ok(schemas) => schemas,
Err(_) => [s.to_string()].to_vec(),
}
})
.collect::<Vec<_>>();

let schemas = Schema::parse_list(&schemas.iter().map(String::as_str).collect::<Vec<_>>())?;
self.gen(&Source::Schemas(&schemas), output)?;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod error;
mod gen;
mod templates;
mod protocol;

pub use crate::error::{Error, Result};
pub use crate::gen::{Generator, GeneratorBuilder, Source};
Expand Down
26 changes: 26 additions & 0 deletions src/protocol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use apache_avro::Error;
use serde_json::{to_string, Value};

pub struct Protocol {
// pub name: String,
// pub doc: String,
// pub schema: Schema,
}

#[derive(Debug, Clone)]
struct ExpectedArrayError;

impl Protocol {
pub fn get_schemas(input: &str) -> Result<Vec<String>, Error> {
let schema: Value = serde_json::from_str(input).map_err(Error::ParseSchemaJson)?;
let types = schema["types"].as_array();
match types {
Some(t) => Ok(t
.into_iter()
.flat_map(|json| to_string(json))
.collect()
),
None => Err(Error::GetArrayItemsField),
}
}
}
16 changes: 16 additions & 0 deletions tests/generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,19 @@ fn gen_fixed() {
fn nested_with_float() {
validate_generation("nested_with_float", Generator::new().unwrap());
}

#[test]
fn gen_protocol_simple() {
validate_generation(
"protocol_simple",
Generator::new().unwrap(),
);
}

#[test]
fn gen_protocol_nested() {
validate_generation(
"protocol_nested",
Generator::new().unwrap(),
);
}
2 changes: 2 additions & 0 deletions tests/schemas/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ pub mod simple;
pub mod simple_with_builders;
pub mod simple_with_schemas;
pub mod nested_with_float;
pub mod protocol_simple;
pub mod protocol_nested;
9 changes: 9 additions & 0 deletions tests/schemas/protocol_nested.avdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@namespace("org.test")
protocol TestService {

import idl "protocol_simple.avdl";

record TestC {
TestB testC_probB;
}
}
42 changes: 42 additions & 0 deletions tests/schemas/protocol_nested.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"protocol" : "TestService",
"namespace" : "org.test",
"types" : [ {
"type" : "record",
"name" : "TestA",
"fields" : [ {
"name" : "a",
"type" : "long"
}, {
"name" : "b",
"type" : "string"
}, {
"name" : "c",
"type" : [ "null", "string" ],
"default" : null
} ]
}, {
"type" : "record",
"name" : "TestB",
"fields" : [ ]
}, {
"type" : "record",
"name" : "TestC",
"fields" : [ {
"name" : "testC_probB",
"type" : "TestB"
} ]
} ],
"messages" : {
"fooMethod" : {
"request" : [ {
"name" : "name",
"type" : "string"
}, {
"name" : "description",
"type" : [ "null", "string" ]
} ],
"response" : "TestA"
}
}
}
29 changes: 29 additions & 0 deletions tests/schemas/protocol_nested.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize)]
pub struct TestC {
#[serde(rename = "testC_probB")]
pub test_c_prob_b: TestB,
}

#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct TestB {
}

impl Default for TestB {
fn default() -> TestB {
TestB {
}
}
}

#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize)]
pub struct TestA {
pub a: i64,
pub b: String,
#[serde(default = "default_testa_c")]
pub c: Option<String>,
}

#[inline(always)]
fn default_testa_c() -> Option<String> { None }
14 changes: 14 additions & 0 deletions tests/schemas/protocol_simple.avdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@namespace("org.test")
protocol TestService {

record TestA {
long a;
string b;
union {null, string} c = null;
}

record TestB {
}

TestA fooMethod(string name, union {null, string} description);
}
35 changes: 35 additions & 0 deletions tests/schemas/protocol_simple.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"protocol" : "TestService",
"namespace" : "org.test",
"types" : [ {
"type" : "record",
"name" : "TestA",
"fields" : [ {
"name" : "a",
"type" : "long"
}, {
"name" : "b",
"type" : "string"
}, {
"name" : "c",
"type" : [ "null", "string" ],
"default" : null
} ]
}, {
"type" : "record",
"name" : "TestB",
"fields" : [ ]
} ],
"messages" : {
"fooMethod" : {
"request" : [ {
"name" : "name",
"type" : "string"
}, {
"name" : "description",
"type" : [ "null", "string" ]
} ],
"response" : "TestA"
}
}
}
23 changes: 23 additions & 0 deletions tests/schemas/protocol_simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct TestB {
}

impl Default for TestB {
fn default() -> TestB {
TestB {
}
}
}

#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize)]
pub struct TestA {
pub a: i64,
pub b: String,
#[serde(default = "default_testa_c")]
pub c: Option<String>,
}

#[inline(always)]
fn default_testa_c() -> Option<String> { None }