~arestifo/crystal-cbor#2: 
Support for UUID tag

Hi ~arestifo.

Do you have any plans for adding UUID encoder / decoder?

Here i write simple prototype.

--- src/cbor/tag.cr.orig	2021-01-12 12:56:44.055674968 +0000
+++ src/cbor/tag.cr	2021-01-12 12:56:51.425792703 +0000
@@ -20,7 +20,7 @@
   Base64
   RegularExpression
   MimeMessage
-  UUID
+  UUID              = 37
   Language
   Identifier
struct UUID
  def to_cbor(encoder : CBOR::Encoder)
    encoder.write(CBOR::Tag::UUID)
    bytes.to_slice.to_cbor(encoder)
  end
end

def UUID.new(decoder : CBOR::Decoder)
  case tag = decoder.read_tag
  when CBOR::Tag::UUID
    UUID.new(decoder.read_bytes)
  else
    raise CBOR::ParseError.new("Expected tag to have value 37, got #{tag.value}")
  end
end

data = UUID.new("4dc37995-edcf-4fcd-9510-7c73edad5f1f").to_cbor
puts UUID.from_cbor(data)

It's not ideal and missing specs for UUID, but should work well.

Status
REPORTED
Submitter
~sorcus
Assigned to
No-one
Submitted
5 months ago
Updated
5 months ago
Labels
No labels applied.

~arestifo 5 months ago

I don't think I'll have time to integrate it this week, but feel free to send a pat

  • Alberto
Register here or Log in to comment, or comment via email.