From b0c35f39df781d8fa34158bf51facf4e10974da9 Mon Sep 17 00:00:00 2001 From: Myers Carpenter Date: Tue, 25 Mar 2014 09:13:12 -0400 Subject: [PATCH 1/2] Build under Elixir 0.13 --- lib/excbor.ex | 6 +++--- mix.exs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/excbor.ex b/lib/excbor.ex index 775168e..ef3b776 100644 --- a/lib/excbor.ex +++ b/lib/excbor.ex @@ -41,12 +41,12 @@ defmodule CBOR do val::[unsigned, size(64)], rest::binary >>), do: {mt, val, rest} def decode_header(<< mt::size(3), 31::size(5), rest::binary >>), do: {mt, :indefinite, rest} def decode_with_hashes(bin), do: decode(bin, @hash_treatment) - def decode(bin, treatment // @default_treatment) do + def decode(bin, treatment \\ @default_treatment) do {term, ""} = decode_with_rest(bin, treatment) term end def decode_with_hashes_with_rest(bin), do: decode_with_rest(bin, @hash_treatment) - def decode_with_rest(bin, treatment // @default_treatment) do + def decode_with_rest(bin, treatment \\ @default_treatment) do {mt, val, rest} = decode_header(bin) case val do :indefinite -> @@ -125,7 +125,7 @@ defmodule CBOR do defp decode_array(len, rest, treatment) do decode_array1(len, treatment, [], rest) end - defp decode_array1(0, treatment, acc, bin) do + defp decode_array1(0, _treatment, acc, bin) do {Enum.reverse(acc), bin} end defp decode_array1(len, treatment, acc, bin) do diff --git a/mix.exs b/mix.exs index b834c18..cfacf5f 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule Excbor.Mixfile do def project do [ app: :excbor, version: "0.0.1", - elixir: "~> 0.12.3-dev", + elixir: "~> 0.12", deps: deps ] end From a600e7e648f62c690f89ec622b43def866533928 Mon Sep 17 00:00:00 2001 From: Myers Carpenter Date: Tue, 25 Mar 2014 11:44:38 -0400 Subject: [PATCH 2/2] Use Maps instead of keyword tuple lists --- lib/excbor.ex | 15 +++++++++++---- test/excbor_test.exs | 16 ++++++++-------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/excbor.ex b/lib/excbor.ex index ef3b776..3846f6e 100644 --- a/lib/excbor.ex +++ b/lib/excbor.ex @@ -14,10 +14,10 @@ defmodule CBOR do def decode_non_finite(0, 0), do: {CBOR.Tag, :float, :inf} def decode_non_finite(1, 0), do: {CBOR.Tag, :float, :"-inf"} def decode_non_finite(_, _), do: {CBOR.Tag, :float, :nan} - def default_newmap(), do: [] - def default_add_to_map(map, k, v), do: [{k,v}|map] - def default_finish_map(map), do: Enum.reverse(map) - def default_empty_map(), do: [{}] + def default_newmap(), do: %{} + def default_add_to_map(map, k, v), do: Dict.put(map, k, v) + def default_finish_map(map), do: map + def default_empty_map(), do: %{} def hash_add_to_map(map, k, v), do: HashDict.put(map, k, v) def hash_empty_map(), do: HashDict.new def identity(x), do: x @@ -255,6 +255,13 @@ defmodule CBOR do fn(v, acc) -> CBOR.Encoder.encode_into(v, acc) end) end end + defimpl Encoder, for: Map do + def encode_into(dict, acc) do + Enum.reduce(dict, CBOR.encode_head(5, Dict.size(dict), acc), + fn({k, v}, acc) -> + CBOR.Encoder.encode_into(v, CBOR.Encoder.encode_into(k, acc)) end) + end + end # defimpl Encoder, for: Tuple do # anything else we can do here? # end end diff --git a/test/excbor_test.exs b/test/excbor_test.exs index 3b7303a..3346104 100644 --- a/test/excbor_test.exs +++ b/test/excbor_test.exs @@ -353,27 +353,27 @@ defmodule(CBORTest) do end test("RFC 7049 Appendix A Example 67") do encoded = <<160>> - assert(d(encoded) == [{}]) + assert(d(encoded) == %{}) assert(CBOR.encode(d(encoded)) == encoded) end test("RFC 7049 Appendix A Example 68") do encoded = <<162, 1, 2, 3, 4>> - assert(d(encoded) == [{1, 2}, {3, 4}]) + assert(d(encoded) == %{1 => 2, 3 => 4}) assert(CBOR.encode(d(encoded)) == encoded) end test("RFC 7049 Appendix A Example 69") do encoded = <<162, 97, 97, 1, 97, 98, 130, 2, 3>> - assert(d(encoded) == [{"a", 1}, {"b", [2, 3]}]) + assert(d(encoded) == %{"a" => 1, "b" => [2, 3]}) assert(CBOR.encode(d(encoded)) == encoded) end test("RFC 7049 Appendix A Example 70") do encoded = <<130, 97, 97, 161, 97, 98, 97, 99>> - assert(d(encoded) == ["a", [{"b", "c"}]]) + assert(d(encoded) == ["a", %{"b" => "c"}]) assert(CBOR.encode(d(encoded)) == encoded) end test("RFC 7049 Appendix A Example 71") do encoded = <<165, 97, 97, 97, 65, 97, 98, 97, 66, 97, 99, 97, 67, 97, 100, 97, 68, 97, 101, 97, 69>> - assert(d(encoded) == [{"a", "A"}, {"b", "B"}, {"c", "C"}, {"d", "D"}, {"e", "E"}]) + assert(d(encoded) == %{"a" => "A", "b" => "B", "c" => "C", "d" => "D", "e" => "E"}) assert(CBOR.encode(d(encoded)) == encoded) end test("RFC 7049 Appendix A Example 72") do @@ -418,17 +418,17 @@ defmodule(CBORTest) do end test("RFC 7049 Appendix A Example 80") do encoded = <<191, 97, 97, 1, 97, 98, 159, 2, 3, 255, 255>> - assert(d(encoded) == [{"a", 1}, {"b", [2, 3]}]) + assert(d(encoded) == %{"a" => 1, "b" => [2, 3]}) # (no roundtrip) end test("RFC 7049 Appendix A Example 81") do encoded = <<130, 97, 97, 191, 97, 98, 97, 99, 255>> - assert(d(encoded) == ["a", [{"b", "c"}]]) + assert(d(encoded) == ["a", %{"b" => "c"}]) # (no roundtrip) end test("RFC 7049 Appendix A Example 82") do encoded = <<191, 99, 70, 117, 110, 245, 99, 65, 109, 116, 33, 255>> - assert(d(encoded) == [{"Fun", true}, {"Amt", -2}]) + assert(d(encoded) == %{"Fun" => true, "Amt" => -2}) # (no roundtrip) end end