Skip to content

Commit

Permalink
Support for parsing a list of schemas which may have cross dependenci…
Browse files Browse the repository at this point in the history
…es (#173)

* Added ability to parse a list of schemas, which may have cross dependencies. Added tests

* Fixing the formatting

* A bit more formatting

* Added a comma to please clippy

* Switched an ok_or to an ok_or_else at the behest of clippy

* Following fixes: Fixed some docstrings, a new error for name collisions when using parse_list, guarantees that input order matches output order

* Removed an unused import

* Small formatting fixes

* Added another test involving fullnames

* Added test that ensures when a recursive type is put in, the algorithm does blow the top off the stack but returns an error

* removed unused variable

* Updated readme and one tiny fix
  • Loading branch information
batconjurer authored Jan 29, 2021
1 parent 8c02574 commit eef23fc
Show file tree
Hide file tree
Showing 5 changed files with 613 additions and 52 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,38 @@ let schema = Schema::parse_str(raw_schema).unwrap();
println!("{:?}", schema);
```

Additionally, a list of of definitions (which may depend on each other) can be given and all of
them will be parsed into the corresponding schemas.

```rust
use avro_rs::Schema;

let raw_schema_1 = r#"{
"name": "A",
"type": "record",
"fields": [
{"name": "field_one", "type": "float"}
]
}"#;

// This definition depends on the definition of A above
let raw_schema_2 = r#"{
"name": "B",
"type": "record",
"fields": [
{"name": "field_one", "type": "A"}
]
}"#;

// if the schemas are not valid, this function will return an error
let schemas = Schema::parse_list(&[raw_schema_1, raw_schema_2]).unwrap();

// schemas can be printed for debugging
println!("{:?}", schemas);
```
*N.B.* It is important to note that the composition of schema definitions requires schemas with names.
For this reason, only schemas of type Record, Enum, and Fixed should be input into this function.

The library provides also a programmatic interface to define schemas without encoding them in
JSON (for advanced use), but we highly recommend the JSON interface. Please read the API
reference in case you are interested.
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub enum Error {
#[error("Not a string value, required for uuid: {0:?}")]
GetUuidFromStringValue(ValueKind),

#[error("Two schemas with the same fullname were given: {0:?}")]
NameCollision(String),

#[error("Not a fixed or bytes type, required for decimal schema, got: {0:?}")]
ResolveDecimalSchema(SchemaKind),

Expand Down
32 changes: 32 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,38 @@
//! println!("{:?}", schema);
//! ```
//!
//! Additionally, a list of of definitions (which may depend on each other) can be given and all of
//! them will be parsed into the corresponding schemas.
//!
//! ```
//! use avro_rs::Schema;
//!
//! let raw_schema_1 = r#"{
//! "name": "A",
//! "type": "record",
//! "fields": [
//! {"name": "field_one", "type": "float"}
//! ]
//! }"#;
//!
//! // This definition depends on the definition of A above
//! let raw_schema_2 = r#"{
//! "name": "B",
//! "type": "record",
//! "fields": [
//! {"name": "field_one", "type": "A"}
//! ]
//! }"#;
//!
//! // if the schemas are not valid, this function will return an error
//! let schemas = Schema::parse_list(&[raw_schema_1, raw_schema_2]).unwrap();
//!
//! // schemas can be printed for debugging
//! println!("{:?}", schemas);
//! ```
//! *N.B.* It is important to note that the composition of schema definitions requires schemas with names.
//! For this reason, only schemas of type Record, Enum, and Fixed should be input into this function.
//!
//! The library provides also a programmatic interface to define schemas without encoding them in
//! JSON (for advanced use), but we highly recommend the JSON interface. Please read the API
//! reference in case you are interested.
Expand Down
Loading

0 comments on commit eef23fc

Please # to comment.