-
Notifications
You must be signed in to change notification settings - Fork 148
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
Parsing (registry) multiple schemas #32
Comments
Sure! The two things to know are:
By default the var registry = {};
var secondType = avro.parse('./second.avsc', {registry: registry});
// At this point `registry` contains the definition of `x10ba.second`.
var firstType = avro.parse('./first.avsc', {registry: registry}); Note that the order in which we parse the schemas is important (it should be chosen such that references can be resolved). |
BTW. I love your module, and you are very responsive (excellent Open Source Committer) So, I'd call: |
Thanks :).
Right. If |
So simple. |
Hi, //in the above examples, when parsed, I get this result for one of the k:v. // registry works on parsing, but returns [Object]. |
That's probably because nested objects get truncated when you print them (see There are multiple ways around it, for example:
|
yep. worked. |
Is it possible to have circular dependencies with this pattern? Would it help if all the schemas were jumbled in one json file? |
@krukru - yes, you should put both schemas in the same file if you have a circular dependency. |
Hey @mtth, could you then help me out, I tried the following but did not work as expected. it("Should parse circular dependencies", function() {
const type = avro.Type.forSchema([
{
"type": "record",
"name": "A",
"fields": [
{
"name": "fieldB",
"type": "B"
},
]
},
{
"type": "record",
"name": "B",
"fields": [
{
"name": "fieldA",
"type": "A"
},
]
}
]);
const objA: A = new A();
const buffer = type.toBuffer(objA);
}); This fails with exception |
Sure, you'll need to expand const type = avro.Type.forSchema([
{
"type": "record",
"name": "A",
"fields": [
{
"name": "fieldB",
"type": {
// Put B's schema here since it's the first time we encounter it.
"type": "record",
"name": "B",
"fields": [{"name": "fieldA", "type": "A"}]
}
}
]
},
"B" // Now we can just reference it by name.
]); |
Hi,
I am trying to parse schemas that are split in multiple files (my understanding is I need to register these during the parse). However, the documentation is a bit sparse and I am having difficulty using the registry, can you give an example on how to use?
Thanks,
x10ba
pseudo code nodejs
var first_type = avsc.parse(frist.avsc); // how do I use registry?
var second_type = avsc.parse(second.avsc);
// parse avro encoded message fromBuffer
console.log(type.fromBuffer());
two schema files
// first.avsc
{
"namespace": "x10ba",
"type": "record",
"name": "first",
"fields": [
{ "name": "stuff", "type": [ "long", "null" ] },
{ "name": "moreStuff", "type": [
"x10ba.second", "null" ] }
]
}
// second.avsc
{
"namespace": "x10ba",
"type": "record",
"name": "second",
"fields": [
{ "name": "someStuff", "type": [ "string", "null" ] },
]
}
The text was updated successfully, but these errors were encountered: