use_cbor_discriminator: now works with imbricated classes.

master
Karchnu 2020-11-27 17:23:46 +01:00
parent a058ab3ccf
commit bdc14a1d20
3 changed files with 15 additions and 5 deletions

View File

@ -2,9 +2,17 @@ class CBOR::Decoder
@lexer : Lexer
getter current_token : Token::T?
def reset
# Decode until a certain point in the history (use_cbor_discriminator helper).
def reset(value : Int32 | Int64 = 0)
@lexer.reset
@current_token = @lexer.next_token
while pos < value
@current_token = @lexer.next_token
end
end
# Give the current position in the decoder (use_cbor_discriminator helper).
def pos
@lexer.io.pos
end
def initialize(input)

View File

@ -1,4 +1,5 @@
class CBOR::Lexer
property io : IO
def self.new(slice : Bytes)
new IO::Memory.new(slice)
end
@ -8,8 +9,8 @@ class CBOR::Lexer
def initialize(@io : IO)
end
def reset
@io.seek 0
def reset(value : Int32 | Int64 = 0)
@io.seek value
@eof = false
end

View File

@ -353,8 +353,9 @@ module CBOR
# SLOW. Read everything, get the type, read everything again.
def self.new(decoder : ::CBOR::Decoder)
current_offset = decoder.pos
if v = decoder.read_value
decoder.reset
decoder.reset current_offset
case v
when Hash(CBOR::Type, CBOR::Type)
discriminator_value = v[{{field.id.stringify}}]?