Code Monkey home page Code Monkey logo

Comments (2)

kostya avatar kostya commented on August 15, 2024

code can look like this:

    macro use_msgpack_discriminator(field, mapping)
      {% unless mapping.is_a?(HashLiteral) || mapping.is_a?(NamedTupleLiteral) %}
        {% mapping.raise "mapping argument must be a HashLiteral or a NamedTupleLiteral, not #{mapping.class_name.id}" %}
      {% end %}

      def self.new(pull : ::MessagePack::Unpacker)
        node = pull.read_node
        pull2 = MessagePack::NodeUnpacker.new(node)
        discriminator_value = nil
        pull2.consume_table do |key|
          if key == {{field.id.stringify}}
          case token = pull2.read_token
          when MessagePack::Token::IntT, MessagePack::Token::StringT, MessagePack::Token::BoolT
            discriminator_value = token.value
            break
          else
            # nothing more to do
            raise ::MessagePack::TypeCastError.new("Msgpack discriminator field '{{field.id}}' has an invalid value type of #{MessagePack::Token.to_s(token)}", token.byte_number)
          end
          else
            pull2.skip_value
          end
        end

        print(discriminator_value)

        unless discriminator_value
          raise ::MessagePack::UnpackError.new("Missing Msgpack discriminator field '{{field.id}}'", 0)
        end

        case discriminator_value
        {% for key, value in mapping %}
          {% if mapping.is_a?(NamedTupleLiteral) %}
            when {{key.id.stringify}}
          {% else %}
            {% if key.is_a?(StringLiteral) %}
              when {{key}}
            {% elsif key.is_a?(NumberLiteral) || key.is_a?(BoolLiteral) %}
              when {{key.id}}
            {% elsif key.is_a?(Path) %}
              when {{key.resolve}}
            {% else %}
              {% key.raise "mapping keys must be one of StringLiteral, NumberLiteral, BoolLiteral, or Path, not #{key.class_name.id}" %}
            {% end %}
          {% end %}
          {{value.id}}.new(MessagePack::NodeUnpacker.new(node))
        {% end %}
        else
          raise ::MessagePack::UnpackError.new("Unknown '{{field.id}}' discriminator value: #{discriminator_value.inspect}", 0)
        end
      end
    end

but somehow it not work, and crystal produce infinite exception, not have time to debug it.

from msgpack-crystal.

jgaskins avatar jgaskins commented on August 15, 2024

@kostya Nice! The bug was that {{value.id}}.new(MessagePack::NodeUnpacker.new(node)) is the same method signature as the method the discriminator macro defines. It works by adding __pull_for_msgpack_serializable: in front of the NodeUnpacker. I'll put together a PR for this.

from msgpack-crystal.

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.