Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jianchen2580 opened this issue Jul 20, 2021 · 0 comments
Open

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

jianchen2580 opened this issue Jul 20, 2021 · 0 comments

Comments

@jianchen2580
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant