Decoding: use_cbor_discriminator

dev
Karchnu 2020-11-11 04:54:19 +01:00
parent 4ac38a7598
commit b8ebc57bb7
4 changed files with 43 additions and 0 deletions

View File

@ -2,6 +2,11 @@ class CBOR::Decoder
@lexer : Lexer
getter current_token : Token::T?
def reset
@lexer.reset
@current_token = @lexer.next_token
end
def initialize(input)
@lexer = Lexer.new(input)
@current_token = @lexer.next_token

View File

@ -3,6 +3,10 @@ def Object.from_cbor(string_or_io)
new(parser)
end
def Object.from_cbor(parser : CBOR::Decoder)
new(parser)
end
def String.new(decoder : CBOR::Decoder)
decoder.read_string
end

View File

@ -8,6 +8,11 @@ class CBOR::Lexer
def initialize(@io : IO)
end
def reset
@io.seek 0
@eof = false
end
def next_token : Token::T?
return nil if @eof

View File

@ -346,6 +346,35 @@ module CBOR
end
end
macro use_cbor_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 %}
# SLOW. Read everything, get the type, read everything again.
def self.new(decoder : ::CBOR::Decoder)
if v = decoder.read_value
decoder.reset
case v
when Hash(CBOR::Type, CBOR::Type)
discriminator_value = v[{{field.id.stringify}}]?
case discriminator_value
{% for key, value in mapping %}
when {{key.id.stringify}}
return {{value.id}}.from_cbor(decoder)
{% end %}
else
raise "Unknown '{{field.id}}' discriminator value: #{discriminator_value.inspect}"
end
else
raise "cannot get cbor discriminator #{ {{ field.id.stringify }} }"
end
else
raise "cannot decode cbor value"
end
end
end
# Tells this class to decode CBOR by using a field as a discriminator.
#
# - *field* must be the field name to use as a discriminator