Giter Club home page Giter Club logo

Comments (7)

Strech avatar Strech commented on July 21, 2024

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.

LostKobrakai avatar LostKobrakai commented on July 21, 2024

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.

Strech avatar Strech commented on July 21, 2024

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.

LostKobrakai avatar LostKobrakai commented on July 21, 2024

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.

Strech avatar Strech commented on July 21, 2024

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.

Strech avatar Strech commented on July 21, 2024

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.

Strech avatar Strech commented on July 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.