Comments (2)
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.
@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)
- Packing array of hashes HOT 11
- Failed giving version HOT 1
- not compiled with crystal 0.17
- Parsing an unknown data structure. HOT 1
- not correct handle union type in mapping
- Support for nested Hash HOT 2
- Crash in mapping HOT 3
- Error while building with Crystal 0.21 HOT 1
- Add support for type extension HOT 4
- Support tarantool serializable HOT 3
- Support for missing or optional HOT 1
- Bad process on nil HOT 2
- The fix of #49 doesn't cover type restriction. HOT 1
- Broken stream reading, when type mismatch
- Typo
- Can't infer the type of instance variable '@var_present' HOT 5
- Shall not include Time conversion by default HOT 11
- Missing Timestamp extension type HOT 2
- have Serializable override inspect? HOT 2
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 msgpack-crystal.