Update project with failing tests

This commit is contained in:
Anthony Miyaguchi 2019-01-16 14:35:27 -08:00
Родитель 4b73a93840
Коммит 506b604f64
6 изменённых файлов: 62 добавлений и 51 удалений

12
Cargo.lock сгенерированный
Просмотреть файл

@ -5,7 +5,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "proc-macro2"
version = "0.4.24"
version = "0.4.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -16,7 +16,7 @@ name = "quote"
version = "0.6.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -34,7 +34,7 @@ name = "serde_derive"
version = "1.0.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -54,13 +54,13 @@ name = "syn"
version = "0.15.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "telemetry-schema-transpiler"
name = "telemetry_schema_transpiler"
version = "0.1.0"
dependencies = [
"serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)",
@ -75,7 +75,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b"
"checksum proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "77619697826f31a02ae974457af0b29b723e5619e113e9397b8b82c6bd253f09"
"checksum proc-macro2 0.4.25 (registry+https://github.com/rust-lang/crates.io-index)" = "d3797b7142c9aa74954e351fc089bbee7958cebbff6bf2815e7ffff0b19f547d"
"checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c"
"checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7"
"checksum serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)" = "0e732ed5a5592c17d961555e3b552985baf98d50ce418b7b655f31f6ba7eb1b7"

Просмотреть файл

@ -1,11 +1,16 @@
[package]
name = "telemetry-schema-transpiler"
name = "telemetry_schema_transpiler"
version = "0.1.0"
authors = ["Anthony Miyaguchi <amiyaguchi@mozilla.com>"]
edition = "2018"
[lib]
name = "converter"
[dependencies]
serde = "*"
serde_json = "*"
serde_derive = "*"
[build-dependencies]
serde = "*"

Просмотреть файл

@ -4,7 +4,7 @@ extern crate serde;
extern crate serde_json;
use serde::Deserialize;
use serde_json::{json, Result, Value};
use serde_json::{json, Map, Result, Value};
use std::fs::{self, File};
use std::io::{BufReader, Write};
use std::path::{Path, PathBuf};
@ -35,10 +35,16 @@ fn generate_tests(input: PathBuf, output: &Path) {
let destination = output.join(format!("{}.rs", suite.name));
let mut outfile = File::create(&destination).unwrap();
write!(outfile, "use converter::convert_avro_direct;");
write!(
outfile,
r#"
use converter::convert_avro_direct;
use serde_json::Value;
"#
);
for case in suite.tests {
let formatted = format!(
r##"
r##"
#[test]
fn {name}() {{
let input_data = r#"
@ -47,9 +53,9 @@ fn {name}() {{
let expected_data = r#"
{expected}
"#;
let input = serde_json::from_str(input_data).unwrap();
let expected = serde_json::from_str(expected_data).unwrap();
assert_eq!(expected, convert_avro_direct(input, "root".to_string()));
let input: Value = serde_json::from_str(input_data).unwrap();
let expected: Value = serde_json::from_str(expected_data).unwrap();
assert_eq!(expected, convert_avro_direct(&input, "root".to_string()));
}}
"##,
name = case.name,

24
src/lib.rs Normal file
Просмотреть файл

@ -0,0 +1,24 @@
use serde_json::{json, Value};
// This uses the Value interface for converting values, which is not strongly typed.
pub fn convert_avro_direct(input: &Value, name: String) -> Value {
let element: Value = match input["type"].as_str().unwrap() {
"object" => {
let mut fields = Vec::new();
for (key, value) in input["properties"].as_object().unwrap().iter() {
fields.push(convert_avro_direct(value, key.to_string()));
}
fields.sort_by_key(|obj| obj["name"].as_str().unwrap().to_string());
json!({
"type": "record",
"name": name,
"fields": fields,
})
}
"integer" => json!({"name": name, "type": "int"}),
"string" => json!({"name": name, "type": "string"}),
"boolean" => json!({"name": name, "type": "boolean"}),
_ => json!(null),
};
json!(element)
}

Просмотреть файл

@ -1,6 +1,3 @@
use serde_json::{json, Result, Value};
use std::collections::HashMap;
// Documentation references:
// https://docs.serde.rs/serde_json/value/index.html
// https://doc.rust-lang.org/1.30.0/book/second-edition/ch08-03-hash-maps.html
@ -21,33 +18,9 @@ use std::collections::HashMap;
// name: String,
// fields: Option<List<Box<AvroField>>>
// }
pub mod converter {
// This uses the Value interface for converting values, which is not strongly typed.
pub fn convert_avro_direct(input: &Value, name: String) -> Value {
let element: Value = match input["type"].as_str().unwrap() {
"object" => {
let mut fields = Vec::new();
for (key, value) in input["properties"].as_object().unwrap().iter() {
fields.push(convert_avro_direct(value, key.to_string()));
}
fields.sort_by_key(|obj| obj["name"].as_str().unwrap().to_string());
json!({
"type": "record",
"name": name,
"fields": fields,
})
}
"integer" => json!({"name": name, "type": "int"}),
"string" => json!({"name": name, "type": "string"}),
"boolean" => json!({"name": name, "type": "boolean"}),
_ => json!(null),
};
json!(element)
}
}
extern crate converter;
use converter::convert_avro_direct;
use serde_json::Value;
fn main() {
println!("hello world!");

Просмотреть файл

@ -1,4 +1,7 @@
use converter::convert_avro_direct;
use serde_json::Value;
#[test]
fn test_atomic() {
let input_data = r#"
@ -12,9 +15,9 @@ fn test_atomic() {
"type": "int"
}
"#;
let input = serde_json::from_str(input_data).unwrap();
let expected = serde_json::from_str(expected_data).unwrap();
assert_eq!(expected, convert_avro_direct(input, "root".to_string()));
let input: Value = serde_json::from_str(input_data).unwrap();
let expected: Value = serde_json::from_str(expected_data).unwrap();
assert_eq!(expected, convert_avro_direct(&input, "root".to_string()));
}
#[test]
@ -36,9 +39,9 @@ fn test_atomic_with_null() {
]
}
"#;
let input = serde_json::from_str(input_data).unwrap();
let expected = serde_json::from_str(expected_data).unwrap();
assert_eq!(expected, convert_avro_direct(input, "root".to_string()));
let input: Value = serde_json::from_str(input_data).unwrap();
let expected: Value = serde_json::from_str(expected_data).unwrap();
assert_eq!(expected, convert_avro_direct(&input, "root".to_string()));
}
#[test]
@ -56,7 +59,7 @@ fn test_incompatible_atomic_multitype() {
"type": "string"
}
"#;
let input = serde_json::from_str(input_data).unwrap();
let expected = serde_json::from_str(expected_data).unwrap();
assert_eq!(expected, convert_avro_direct(input, "root".to_string()));
let input: Value = serde_json::from_str(input_data).unwrap();
let expected: Value = serde_json::from_str(expected_data).unwrap();
assert_eq!(expected, convert_avro_direct(&input, "root".to_string()));
}