Skip to content

Commit

Permalink
Use modern data format types for transcoding
Browse files Browse the repository at this point in the history
  • Loading branch information
aoeking committed Sep 29, 2016
1 parent 6bf03e0 commit 96ee0c3
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/cberl_transcoder.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
-include("cberl.hrl").

-define('CBE_NONE', 0).
-define('CBE_JSON', 16#0002).
-define('CBE_RAW', 16#0004).
-define('CBE_STR', 16#0008).
-define('CBE_LEGACY', 16#01).
-define('CBE_JSON', (16#02 bsl 24)).
-define('CBE_RAW', (16#03 bsl 24)).
-define('CBE_STR', (16#04 bsl 24)).
-define('FLAG_MASK', (16#01 bor (16#02 bsl 24) bor 16#02 bor (16#03 bsl 24) bor 16#04 bor (16#04 bsl 24) bor 16#08)).

-define(STANDARD_FLAG, json).

Expand All @@ -17,22 +19,24 @@ encode_value(Encoders, Value) ->
encode_value1(flag(Encoders), Value).

-spec encode_value1(integer(), value()) -> value().
encode_value1(Flag, Value) when Flag band ?'CBE_STR' == ?'CBE_STR' ->
encode_value1(Flag, Value) when (Flag band ?'FLAG_MASK') == ?'CBE_STR' ->
encode_value1(Flag bxor ?'CBE_STR', list_to_binary(Value));
encode_value1(Flag, Value) when Flag band ?'CBE_JSON' == ?'CBE_JSON' ->
encode_value1(Flag, Value) when (Flag band ?'FLAG_MASK') == ?'CBE_JSON' ->
encode_value1(Flag bxor ?'CBE_JSON', jiffy:encode(Value));
encode_value1(Flag, Value) when Flag band ?'CBE_RAW' == ?'CBE_RAW' ->
encode_value1(Flag, Value) when (Flag band ?'FLAG_MASK') == ?'CBE_RAW' ->
encode_value1(Flag bxor ?'CBE_RAW', term_to_binary(Value));
encode_value1(_, Value) ->
Value.

-spec decode_value(integer(), value()) -> value().
decode_value(Flag, Value) when ?'CBE_RAW' band Flag == ?'CBE_RAW' ->
decode_value(Flag, Value) when (?'FLAG_MASK' band Flag) == ?'CBE_RAW' ->
decode_value(Flag bxor ?'CBE_RAW', binary_to_term(Value));
decode_value(Flag, Value) when ?'CBE_JSON' band Flag == ?'CBE_JSON' ->
decode_value(Flag, Value) when (?'FLAG_MASK' band Flag) == ?'CBE_JSON' ->
decode_value(Flag bxor ?'CBE_JSON', jiffy:decode(Value));
decode_value(Flag, Value) when ?'CBE_STR' band Flag == ?'CBE_STR' ->
decode_value(Flag, Value) when (?'FLAG_MASK' band Flag) == ?'CBE_STR' ->
decode_value(Flag bxor ?'CBE_STR', binary_to_list(Value));
decode_value(Flag, Value) when (?'FLAG_MASK' band Flag) == ?'CBE_LEGACY' ->
decode_value(Flag bxor ?'CBE_LEGACY', jiffy:decode(Value));
decode_value(_, Value) ->
Value.

Expand Down

0 comments on commit 96ee0c3

Please sign in to comment.