Comments (11)
Sure, you'll need to expand B
's schema the first time it's encountered. I think this'll do the trick:
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.
]);
from avsc.
Sure! The two things to know are:
- When
parse
creates a new named type (for example arecord
), it will add it to itsregistry
. - When
parse
encounters a type reference, it will look it up inside itsregistry
(and throw an error if it isn't found).
By default the registry
is just an empty object, but by sharing it between parse
calls we can allow a schema to reference types defined in another schema. Using your example, that would look like:
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).
from avsc.
BTW. I love your module, and you are very responsive (excellent Open Source Committer)
So, I'd call:
obj = avroEncodedMessage (buffer)
firstType.fromBuffer(obj)
//secondType reference held in the registry, so would not error?
from avsc.
Thanks :).
So, I'd call:
obj = avroEncodedMessage; // (buffer) firstType.fromBuffer(obj) //secondType reference held in the registry, so would not error?
Right. If parse
successfully returned, then you're all set.
from avsc.
So simple.
Thanks for the help. Closing issue. Have a great weekend.
from avsc.
Hi,
Though parser/ registry works, adding this comment which I am trying to resolve:
//in the above examples, when parsed, I get this result for one of the k:v.
// registry works on parsing, but returns [Object].
{...,
"moreStuff":{"x10ba.second": [Object]}
}
from avsc.
That's probably because nested objects get truncated when you print them (see util.inspect
).
There are multiple ways around it, for example:
- You can increase the depth:
console.log(util.inspect(obj, {depth: null}))
- You can stringify the object first:
console.log(JSON.stringify(obj, null, 2))
- You can use Avro's JSON encoding:
console.log(type.toString(obj))
from avsc.
yep. worked.
from avsc.
Is it possible to have circular dependencies with this pattern?
For example, record A has a field of type B, and record B has a field of type A.
Would it help if all the schemas were jumbled in one json file?
from avsc.
@krukru - yes, you should put both schemas in the same file if you have a circular dependency.
from avsc.
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
Error: undefined type name: B at Function.Type.forSchema (nodejs/node_modules/avsc/lib/types.js:167:11) at new Field (nodejs/node_modules/avsc/lib/types.js:2722:20) at RecordType.<anonymous> (nodejs/node_modules/avsc/lib/types.js:2086:17) at Array.map (<anonymous>) at new RecordType (nodejs/node_modules/avsc/lib/types.js:2085:45) at nodejs/node_modules/avsc/lib/types.js:210:14 at Function.Type.forSchema (nodejs/node_modules/avsc/lib/types.js:211:7)
from avsc.
Related Issues (20)
- Support schema evolution without the need of previous schemas HOT 1
- float value is inaccurate after serializing and deserializing using avro schema HOT 1
- How to add custom attributes to serialised schema type eg. sqlType HOT 4
- finish event fires too early HOT 3
- Array with null items supported? HOT 1
- Avro union - remove type information in resulting json HOT 1
- Bun support HOT 1
- Update `snappy` examples in wiki for `snappy` 7.x.x (async) HOT 1
- How to convert decoded avro data into JSON? HOT 1
- IDL not exporting types for array of union HOT 3
- Support ?-syntax for optional fields in avdl HOT 2
- can schema support dynamic keys? HOT 1
- Extending a schema causes a "truncated buffer" error when using fromBuffer HOT 5
- Invalid Avro header does not raise error event HOT 1
- "new SlowBuffer" is deprecated since Node v.6 --> cannot use it with VITE5 and VUE3 HOT 2
- Doesn't handle trailing 0s from buffer. HOT 1
- Unwrapping unions when deserialising HOT 4
- Unable to consume messages produced by Java application with AVRO schema HOT 9
- Using type.isValid() with a union of records HOT 3
- long encoding/decoding is not reversible for some large but safe js ints HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from avsc.