Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Enable nested dict decoding #6

Open
wants to merge 3 commits into from

1 participant

Woody Peterson
Woody Peterson

Currently bert can only decode one-level dicts, this enables nested dict decoding.

Apologies for including 2 commits in this pull request, but I'm using a combination of laziness and judgment and assuming that the dict encoding fix is a given.

woahdae and others added some commits
Woody Peterson woahdae 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.
1ebcfaa
Ryan Burrows Make dictionary encoding/decoding work on nested e6f0ab9
Woody Peterson woahdae Rebarized bert
Moved from rake to rebar. Note that the tests have been changed to
be OTP compliant, specifically following the m_tests.erl convention
so they will be automatically picked up by eunit (see eunit overview,
"Running EUnit")
8d09986
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 29, 2010
  1. Woody Peterson

    fixed dictionary encoding in a version-agnostic way

    woahdae authored
    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.
Commits on Oct 15, 2010
  1. Make dictionary encoding/decoding work on nested

    Ryan Burrows authored
Commits on Dec 1, 2010
  1. Woody Peterson

    Rebarized bert

    woahdae authored
    Moved from rake to rebar. Note that the tests have been changed to
    be OTP compliant, specifically following the m_tests.erl convention
    so they will be automatically picked up by eunit (see eunit overview,
    "Running EUnit")
This page is out of date. Refresh to see the latest.
4 .gitignore
View
@@ -1,2 +1,6 @@
+/doc
+/_test
+/.eunit
+/docs
ebin
*.dump
25 Rakefile
View
@@ -1,25 +0,0 @@
-require 'rubygems'
-require 'rake'
-
-ERLC_FLAGS = "+debug_info -W2 -o ebin"
-
-task :default => :test
-
-task :chdir do
- Dir.chdir(File.join(File.dirname(__FILE__), *%w[.]))
-end
-
-task :build => :chdir do
- sh "mkdir -p ebin"
- sh "erlc #{ERLC_FLAGS} src/*.erl"
-end
-
-task :test => :chdir do
- sh "mkdir -p ebin"
- sh "erlc #{ERLC_FLAGS} -DTEST -I etest src/*.erl"
- sh "test/run.es"
-end
-
-task :console => :chdir do
- sh "erl +Bc +K true -smp enable -pa ebin -sname local_console_#{$$} -kernel start_boot_server true"
-end
BIN  rebar
View
Binary file not shown
7 src/bert.app.src
View
@@ -0,0 +1,7 @@
+{application, bert,
+ [
+ {description, "BERT Serialization Library"},
+ {vsn, "1.1.0"},
+ {modules, [bert]},
+ {applications, [kernel, stdlib]}
+ ]}.
11 src/bert.erl
View
@@ -7,10 +7,6 @@
-export([encode/1, decode/1]).
--ifdef(TEST).
--include("test/bert_test.erl").
--endif.
-
%%---------------------------------------------------------------------------
%% Public API
@@ -34,7 +30,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);
@@ -56,7 +52,8 @@ decode_term(Term) ->
{bert, true} -> true;
{bert, false} -> false;
{bert, dict, Dict} ->
- dict:from_list(Dict);
+ L = lists:map(fun decode_term/1, Dict),
+ dict:from_list(L);
{bert, Other} ->
{bert, Other};
List when is_list(Term) ->
@@ -66,4 +63,4 @@ decode_term(Term) ->
TList2 = lists:map((fun decode_term/1), TList),
list_to_tuple(TList2);
_Else -> Term
- end.
+ end.
23 test/bert_test.erl
View
@@ -1,23 +0,0 @@
--include_lib("eunit/include/eunit.hrl").
-
-%% encode
-
-encode_list_nesting_test() ->
- Bert = term_to_binary([foo, {bert, true}]),
- Bert = encode([foo, true]).
-
-encode_tuple_nesting_test() ->
- Bert = term_to_binary({foo, {bert, true}}),
- Bert = encode({foo, true}).
-
-%% decode
-
-decode_list_nesting_test() ->
- Bert = term_to_binary([foo, {bert, true}]),
- Term = [foo, true],
- Term = decode(Bert).
-
-decode_tuple_nesting_test() ->
- Bert = term_to_binary({foo, {bert, true}}),
- Term = {foo, true},
- Term = decode(Bert).
46 test/bert_tests.erl
View
@@ -0,0 +1,46 @@
+-module(bert_tests).
+-include_lib("eunit/include/eunit.hrl").
+-import(bert, [encode/1, decode/1]).
+
+%% encode
+
+encode_list_nesting_test() ->
+ Bert = term_to_binary([foo, {bert, true}]),
+ Bert = encode([foo, true]).
+
+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() ->
+ Bert = term_to_binary([foo, {bert, true}]),
+ Term = [foo, true],
+ Term = decode(Bert).
+
+decode_tuple_nesting_test() ->
+ Bert = term_to_binary({foo, {bert, true}}),
+ Term = {foo, true},
+ 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).
+
+decode_dict_nesting_test() ->
+ Bert = term_to_binary({ bert, dict,
+ dict:to_list(dict:store(a,
+ { bert, dict,
+ dict:to_list(dict:store(b, "b", dict:new()))
+ },
+ dict:new())) }),
+ Term = dict:store(a, dict:store(b, "b", dict:new()), dict:new()),
+ Term = decode(Bert).
6 test/run.es
View
@@ -1,6 +0,0 @@
-#!/usr/bin/env escript
-%% -*- erlang -*-
-%%! -pa ./ebin -sasl -boot start_sasl -noshell
-
-main(_) ->
- bert:test().
Something went wrong with that request. Please try again.