From bdc14a1d20f37af07451422f03ff53b942899379 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Fri, 27 Nov 2020 17:23:46 +0100 Subject: [PATCH] use_cbor_discriminator: now works with imbricated classes. --- src/cbor/decoder.cr | 12 ++++++++++-- src/cbor/lexer.cr | 5 +++-- src/cbor/serializable.cr | 3 ++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/cbor/decoder.cr b/src/cbor/decoder.cr index c64d726..2aa6e8f 100644 --- a/src/cbor/decoder.cr +++ b/src/cbor/decoder.cr @@ -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) diff --git a/src/cbor/lexer.cr b/src/cbor/lexer.cr index cc34491..883263a 100644 --- a/src/cbor/lexer.cr +++ b/src/cbor/lexer.cr @@ -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 diff --git a/src/cbor/serializable.cr b/src/cbor/serializable.cr index b55258a..4a119fb 100644 --- a/src/cbor/serializable.cr +++ b/src/cbor/serializable.cr @@ -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}}]?