From 1ebcfaa5c71df8cf7a71ba21dfd5c6026ea5dd73 Mon Sep 17 00:00:00 2001 From: Woody Peterson Date: Thu, 29 Jul 2010 10:49:33 -0700 Subject: [PATCH] fixed dictionary encoding in a version-agnostic way Most forks of this project change is_record(Term, dict, 8) to is_record(Term, dict, 9) to reflect the fact that apparently dict records are of size 9 in versions at least greater than R13B03 (seems to be the earliest complaint about the issue). I haven't verified that size 8 works prior to R13B03, but I'm guessing it did at one point. Assuming dicts will always be represented as tuples where the first element is the dict atom, this change would be version agnostic. Note that the tests are a bit coupled to the bert implementation, but with that you gain some documentation as to what it's actually encoding/decoding. --- src/bert.erl | 2 +- test/bert_test.erl | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/bert.erl b/src/bert.erl index 0faf7fb..61f4ee7 100644 --- a/src/bert.erl +++ b/src/bert.erl @@ -34,7 +34,7 @@ encode_term(Term) -> [] -> {bert, nil}; true -> {bert, true}; false -> {bert, false}; - Dict when is_record(Term, dict, 8) -> + Dict when is_tuple(Term) andalso element(1, Term) =:= dict -> {bert, dict, dict:to_list(Dict)}; List when is_list(Term) -> lists:map((fun encode_term/1), List); diff --git a/test/bert_test.erl b/test/bert_test.erl index 56dc1ba..8845c91 100644 --- a/test/bert_test.erl +++ b/test/bert_test.erl @@ -10,6 +10,11 @@ encode_tuple_nesting_test() -> Bert = term_to_binary({foo, {bert, true}}), Bert = encode({foo, true}). +encode_dict_test() -> + Bert = term_to_binary({ bert, dict, + dict:to_list(dict:store(foo, true, dict:new())) }), + Bert = encode(dict:store(foo, true, dict:new())). + %% decode decode_list_nesting_test() -> @@ -20,4 +25,11 @@ decode_list_nesting_test() -> decode_tuple_nesting_test() -> Bert = term_to_binary({foo, {bert, true}}), Term = {foo, true}, - Term = decode(Bert). \ No newline at end of file + Term = decode(Bert). + +decode_dict_test() -> + Bert = term_to_binary({ bert, dict, + dict:to_list(dict:store(foo, true, dict:new())) }), + Term = dict:store(foo, true, dict:new()), + Term = decode(Bert). +