Comments (7)
Hi @LostKobrakai do you have a specific example of when is it needed? I'm trying to keep the library small but functional, so some features definitely not 1:1 with erlavro
from avrora.
My current case is for permission handling, where a user can have only a single role, but each role has it's own set of additional metadata. I'd like to encode that in a single union of multiple different named records (name == role). That way I'm not encoding the role twice (e.g. in an enum + a separate union of records).
If you don't want to include specific union handling how about a generic way to customize the decoder hook used? This would be even more flexible.
from avrora.
Since I'm not eating my own dog food anymore, may I ask you to share a made-up example of data and schema that I can take a look at and see what is exactly happening? We can work around to see what could be done to achieve that.
from avrora.
That's the test I wrote to figure it out. Consider record_1
and record_2
to be role names, and each record can have role specific fields, which might or might not overlap. Not sure if this is actually a good way to compose hooks though.
test "decode with tagged tuple" do
template = """
{
"type": "record",
"name": "credit_wallet_payment_event",
"namespace": "io.slim",
"fields": [
{
"name": "record_union",
"type": [
{
"type": "record",
"name": "record_1",
"fields": [
{
"name": "start",
"type": "int"
}
]
},
{
"type": "record",
"name": "record_2",
"fields": [
{
"name": "start",
"type": "int"
}
]
}
]
}
]
}
"""
{:ok, schema} = Avrora.Schema.Encoder.from_json(template)
data = %{record_union: {"io.slim.record_2", %{start: 12}}}
{:ok, payload} = Avrora.Codec.Plain.encode(data, schema: schema)
opts =
Map.update!(Avrora.AvroDecoderOptions.options(), :hook, fn avrora_hook ->
tagged_union_hook = :avro_decoder_hooks.tag_unions()
fn type, sub_name_or_index, data, decode_fun ->
tagged_union_hook.(type, sub_name_or_index, data, fn data ->
avrora_hook.(type, sub_name_or_index, data, decode_fun)
end)
end
end)
assert %{"record_union" => {"io.slim.record_2", %{"start" => 12}}} ==
:avro_binary_decoder.decode(payload, schema.full_name, schema.lookup_table, opts)
assert {:ok, %{"record_union" => %{"start" => 12}}} =
Avrora.Codec.Plain.decode(payload, schema: schema)
end
from avrora.
Hey @LostKobrakai sorry for a long silence, have some IRL things, I will take a look on your example this week and try to share my thoughts
from avrora.
Evening @LostKobrakai I think I'm back to my nightlife and I've checked the erlavro and its tag union hook. Initially I have no interest to open access to the hook system (I don't have a good usage example), but with your case I think time comes and it makes sense to reconsider.
But instead of opening way to tag unions, I would rather open access to define custom hook which will be called inside of current Avrora.AvroDecoderOptions.__hook__/4
and result will be amended to handle null
as nil (same as you did)
And any functionality could be injected (I'm also worried about debugging and will check how can I expose erlavro hooks traces. WDYT?
from avrora.
Thanks for your help, I really appreciate it, the new configuration option decoder_hook
was released as a part of v0.24.0 release 🎉
from avrora.
Related Issues (20)
- Empty user-agent in header when calling registry
- schema evolution support HOT 7
- hook to skip certain fields. HOT 2
- Fix warnings introduced in elixir 1.15 HOT 2
- logicalType: date HOT 5
- Telemetry support HOT 4
- Schema storage and reference lookup
- Confluent Schema Registry references
- Warnings on compilation HOT 4
- Maintain a CHANGELOG? HOT 5
- Dialyzer issues with private clients HOT 2
- "Messages encoded with OCF are wrapped in a List" what for? HOT 1
- Allow 3 named types to be used as root schemas
- Get schema name on decode HOT 3
- Add deprecation warning for encode with plain format
- Private clients hardcode schemas path at compile time HOT 6
- Any way to configure the registry_url at startup? HOT 8
- unnamed_type error with primary type schema HOT 11
- Add option to enable SSL verification of registry requests
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 avrora.