flavray / avro-rs

Avro client library implementation in Rust
MIT License
169 stars 95 forks source link

Unions got Error: Validation when call writer.append(record)? #191

Open jianchen2580 opened 3 years ago

jianchen2580 commented 3 years ago

code:

use avro_rs::{
    types::Record, Codec, Reader, Schema,
    Writer, Error,
};

fn main() -> Result<(), Error> {

    let raw_schema = r#"
    {
        "type": "record",
        "name": "test",
        "fields": [
            {"name": "a", "type": "long", "default": 42},
            {"name": "b", "type": "string"},
            {"name": "c", "type": ["string", "null"], "default": null}
        ]
    }
"#;

    let schema = Schema::parse_str(raw_schema)?;

    println!("{:?}", schema);

    let mut writer = Writer::with_codec(&schema, Vec::new(), Codec::Deflate);

    let mut record = Record::new(writer.schema()).unwrap();
    record.put("a", 27i64);
    record.put("b", "bar");
    record.put("c", "foo");
    writer.append(record)?;
    let input = writer.into_inner()?;
    let reader = Reader::with_schema(&schema, &input[..])?;
    for record in reader {
        println!("{:?}", record?);
    }
    Ok(())
}
$ cargo run 
   Compiling aa v0.1.0 (/private/tmp/aa)
    Finished dev [unoptimized + debuginfo] target(s) in 2.03s
     Running `target/debug/aa`
Record { name: Name { name: "test", namespace: None, aliases: None }, doc: None, fields: [RecordField { name: "a", doc: None, default: Some(Number(42)), schema: Long, order: Ascending, position: 0 }, RecordField { name: "b", doc: None, default: None, schema: String, order: Ascending, position: 1 }, RecordField { name: "c", doc: None, default: Some(Null), schema: Union(UnionSchema { schemas: [String, Null], variant_index: {Null: 1, String: 0} }), order: Ascending, position: 2 }], lookup: {"a": 0, "b": 1, "c": 2} }
Error: Validation