Skip to content

Commit

Permalink
Merge pull request basho#13 from basho/sdc-fix-index-marshalling
Browse files Browse the repository at this point in the history
Special-case the encoding of index entries since integers are allowed
  • Loading branch information
seancribbs committed Jul 13, 2012
2 parents 9aa630a + 090396d commit 26b684d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/riak_pb_kv_codec.erl
Expand Up @@ -95,7 +95,7 @@ encode_content_meta(?MD_LASTMOD, {MS,S,US}, PbContent) ->
encode_content_meta(?MD_USERMETA, UserMeta, PbContent) when is_list(UserMeta) ->
PbContent#rpbcontent{usermeta = [encode_pair(E) || E <- UserMeta]};
encode_content_meta(?MD_INDEX, Indexes, PbContent) when is_list(Indexes) ->
PbContent#rpbcontent{indexes = [encode_pair(E) || E <- Indexes]};
PbContent#rpbcontent{indexes = [encode_index_pair(E) || E <- Indexes]};
encode_content_meta(?MD_DELETED, DeletedVal, PbContent) ->
PbContent#rpbcontent{deleted=header_val_to_bool(DeletedVal)};
encode_content_meta(_Key, _Value, PbContent) ->
Expand Down Expand Up @@ -177,6 +177,13 @@ decode_content(PbC) ->

{dict:from_list(MD), PbC#rpbcontent.value}.

%% @doc Convert {K,V} index entries into protocol buffers
-spec encode_index_pair({binary(), integer() | binary()}) -> #rpbpair{}.
encode_index_pair({K,V}) when is_integer(V) ->
encode_pair({K, integer_to_list(V)});
encode_index_pair(E) ->
encode_pair(E).

%% @doc Convert {K,V} tuple to protocol buffers
%% @equiv riak_pb_codec:encode_pair/1
-spec encode_pair({Key::binary(), Value::any()}) -> #rpbpair{}.
Expand Down
13 changes: 13 additions & 0 deletions test/encoding_test.erl
Expand Up @@ -48,6 +48,19 @@ pb_test_() ->
?assertEqual(true, MdSame1),
?assertEqual(true, MdSame2)
end)},
{"indexes encode decode",
?_test(begin
InputMD = dict:from_list([{?MD_INDEX, [{"index_bin", "foo"},
{"index_int", 10}]}]),
ExpectedMD = [{?MD_INDEX, [{<<"index_bin">>, <<"foo">>},
{<<"index_int">>, <<"10">>}]}],
Value = <<"test value">>,
{OutputMD, _} = riak_pb_kv_codec:decode_content(
riak_kv_pb:decode_rpbcontent(
riak_kv_pb:encode_rpbcontent(
riak_pb_kv_codec:encode_content({InputMD, Value})))),
?assertEqual(ExpectedMD, dict:to_list(OutputMD))
end)},
{"empty content encode decode",
?_test(begin
MetaData = dict:new(),
Expand Down

0 comments on commit 26b684d

Please sign in to comment.