~arestifo/crystal-cbor

crystal-cbor: UUID. v1 SUPERSEDED

~karchnu: 2
 UUID.
 Add CBOR UUID encoding and decoding tests.

 2 files changed, 62 insertions(+), 0 deletions(-)
#530192 .build.yml success
crystal-cbor/patches/.build.yml: SUCCESS in 27s

[UUID.][0] from [~karchnu][1]

[0]: https://lists.sr.ht/~arestifo/crystal-cbor/patches/23422
[1]: mailto:karchnu@karchnu.fr

✓ #530192 SUCCESS crystal-cbor/patches/.build.yml https://builds.sr.ht/~arestifo/job/530192
Thanks for the patch!

Personally, I see no advantages in making this functionality opt-in:
All the types are correctly scoped and will only act if used
with a cobr decoder/encoder.

Can you move this logic to the from_cbor.cr and the to_cbor.cr
files respectively, so it’s always included and follows the rest
of the library pattern for those type extension.

Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~arestifo/crystal-cbor/patches/23422/mbox | git am -3
Learn more about email & git

[PATCH crystal-cbor 1/2] UUID. Export this patch

From: Karchnu <karchnu@karchnu.fr>

---
 src/cbor/uuid.cr | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
 create mode 100644 src/cbor/uuid.cr

diff --git a/src/cbor/uuid.cr b/src/cbor/uuid.cr
new file mode 100644
index 0000000..53c346d
--- /dev/null
+++ b/src/cbor/uuid.cr
@@ -0,0 +1,47 @@
require "../cbor"
require "uuid"

struct UUID
  # Creates UUID from CBOR using `CBOR::Decoder`.
  #
  # NOTE: `require "uuid/cbor"` is required to opt-in to this feature.
  #
  # ```
  # require "cbor"
  # require "uuid"
  # require "uuid/cbor"
  #
  # class Example
  #   include CBOR::Serializable
  #
  #   property id : UUID
  # end
  #
  # hash = {"id" => "ba714f86-cac6-42c7-8956-bcf5105e1b81"}
  # example = Example.from_cbor hash.to_cbor
  # example.id # => UUID(ba714f86-cac6-42c7-8956-bcf5105e1b81)
  # ```
  def self.new(pull : CBOR::Decoder)
    # Either the UUID was encoded as String or bytes (smaller).
    case pull.current_token
    when CBOR::Token::StringT
      new(pull.read_string)
    when CBOR::Token::BytesT
      new(pull.read_bytes)
    else
      raise "trying to get an UUID, but CBOR value isn't a string nor bytes: #{pull.current_token}"
    end
  end

  # Returns UUID as CBOR value.
  #
  # NOTE: `require "uuid/cbor"` is required to opt-in to this feature.
  #
  # ```
  # uuid = UUID.new("87b3042b-9b9a-41b7-8b15-a93d3f17025e")
  # uuid.to_cbor
  # ```
  def to_cbor(cbor : CBOR::Encoder)
    cbor.write(@bytes.to_slice)
  end
end
-- 
2.30.2

[PATCH crystal-cbor 2/2] Add CBOR UUID encoding and decoding tests. Export this patch

From: Karchnu <karchnu@karchnu.fr>

---
 spec/cbor_uuid.cr | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
 create mode 100644 spec/cbor_uuid.cr

diff --git a/spec/cbor_uuid.cr b/spec/cbor_uuid.cr
new file mode 100644
index 0000000..64aaa1b
--- /dev/null
+++ b/spec/cbor_uuid.cr
@@ -0,0 +1,15 @@
require "./spec_helper"

describe CBOR do
  describe "UUID" do
    it "UUID#to_cbor" do
      uuid = UUID.new "fc47eb8e-b13c-481e-863a-8f8c47a550f2"
      uuid.to_cbor.hexstring.should eq "50fc47eb8eb13c481e863a8f8c47a550f2"
    end

    it "UUID#from_cbor" do
      uuid = UUID.from_cbor "50fc47eb8eb13c481e863a8f8c47a550f2".hexbytes
      uuid.to_cbor.hexstring.should eq "50fc47eb8eb13c481e863a8f8c47a550f2"
    end
  end
end
-- 
2.30.2
crystal-cbor/patches/.build.yml: SUCCESS in 27s

[UUID.][0] from [~karchnu][1]

[0]: https://lists.sr.ht/~arestifo/crystal-cbor/patches/23422
[1]: mailto:karchnu@karchnu.fr

✓ #530192 SUCCESS crystal-cbor/patches/.build.yml https://builds.sr.ht/~arestifo/job/530192
Thanks for the patch!

Personally, I see no advantages in making this functionality opt-in:
All the types are correctly scoped and will only act if used
with a cobr decoder/encoder.

Can you move this logic to the from_cbor.cr and the to_cbor.cr
files respectively, so it’s always included and follows the rest
of the library pattern for those type extension.