Comments (4)
There is no conventional way to do this as it's in general dangerous: not all unions can be safely unwrapped.
By default avsc
will automatically unwrap most unions when it is safe to do so, including all optional fields. You would typically only unwrap further if you are using properties of your data, for example in #129 (comment).
If you have a need to tightly control your data's representation (maybe you have some internal APIs which do not work well with single value objects?), I would recommend creating an independent internal representation decoupled from the serialization layer's. You could include the snippet above along with any appropriate validation logic in it, giving you more confidence in the data and the ability to easily extend or swap it out later on.
from avsc.
Hi @ftcvlad. JSON-encoded unions are wrapped but avsc
's (and other libraries') in-memory representation can differ from this. This doesn't mean the data is modified by serialization: the data is simply represented differently, similar to how its buffer encoding is different from the in-memory representation (JSON just happens to be more similar and human-readable).
The best in-memory representation--unwrapped/wrapped unions or a custom logical type--will depend on your requirements. In general I would recommend avoiding the additional complexity of logical types unless the default representations are not sufficient. It's hard to say more without knowing more about your use-case.
from avsc.
Hi @mtth , so my use case is
Service A -> pubsub -> Service B with avsc in it
pubsub validation expects data to be encoded / wrapped. Now in service B i want to get my unencoded, unwrapped, original representation data. Is there a way to achieve it with avsc? I was trying to do smth along
const unwrapped = type.clone(data, {
fieldHook: (field, value, type) => {
const isUnion = Type.isType(field.type, 'union');
if (isUnion) {
const keys = Object.keys(value);
return value[keys[0]];
}
return value;
}
});
But this feels a bit unsafe to mangle with data like this -- there might be extra steps to make it work for primitive types, fieldHook is not called for array items (e.g. if i have an array of union type it stays unwrapped), test etc. Is there a simpler / more conventional way? Or am i doing something unexpected?
from avsc.
@mtth i ended up with
const unwrapped = type.clone(data, {
fieldHook: (field, value) => {
const isArray = Type.isType(field.type, 'array');
if (!isArray) {
if (Type.isType(field.type, 'union:wrapped')) {
const key = Object.keys(value)[0];
return value[key];
}
return value;
}
const arrayItemType = (field.type as any).getItemsType();
if (Type.isType(arrayItemType, 'union:wrapped')) {
const res = [];
for (let i = 0; i < value.length; i++) {
const key = Object.keys(value[i])[0];
res.push(value[i][key]);
}
return res;
}
return value;
}
});
which works for my data currently. But qns from above remain
from avsc.
Related Issues (20)
- 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 3
- Doesn't handle trailing 0s from buffer. HOT 1
- 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
- Convert String Representations into Logical Type HOT 1
- Which version of the Avro specification does the latest version of avsc (5.7.7) implement? HOT 1
- Not being able to use records that use the "bytes" type field HOT 2
- Using seprately declared enum in union in record. HOT 1
- Avro.types.LogicalType's _copy implementation HOT 1
- "Error: trailing data" when using custom Long type in Avro with KafkaJS Confluent Schema Registry 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.