Skip to content

Commit

Permalink
Change the doc transforn fun from fun/2 to fun/4 – regarding with PR …
Browse files Browse the repository at this point in the history
…comment.
  • Loading branch information
cabol committed Aug 5, 2016
1 parent 7663a65 commit 116f142
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
16 changes: 8 additions & 8 deletions src/adapters/sumo_store_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -349,27 +349,27 @@ validate_date({FieldType, _, FieldValue}) ->

%% @private
sleep(Doc) ->
sumo_utils:doc_transform(fun sleep_fun/2, Doc).
sumo_utils:doc_transform(fun sleep_fun/4, Doc).

%% @private
sleep_fun({_, _, undefined}, _) ->
sleep_fun(_, _, undefined, _) ->
undefined;
sleep_fun({string, _, FieldValue}, _) ->
sleep_fun(string, _, FieldValue, _) ->
sumo_utils:to_bin(FieldValue);
sleep_fun({date, _, FieldValue}, _) ->
sleep_fun(date, _, FieldValue, _) ->
case sumo_utils:is_datetime(FieldValue) of
true -> {FieldValue, {0, 0, 0}};
_ -> FieldValue
end;
sleep_fun({_, _, FieldValue}, _) ->
sleep_fun(_, _, FieldValue, _) ->
FieldValue.

%% @private
wakeup(Doc) ->
sumo_utils:doc_transform(fun wakeup_fun/2, Doc).
sumo_utils:doc_transform(fun wakeup_fun/4, Doc).

%% @private
wakeup_fun({date, _, {Date, _} = _FieldValue}, _) ->
wakeup_fun(date, _, {Date, _} = _FieldValue, _) ->
Date;
wakeup_fun({_, _, FieldValue}, _) ->
wakeup_fun(_, _, FieldValue, _) ->
FieldValue.
15 changes: 9 additions & 6 deletions src/utils/sumo_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
%%%=============================================================================

-spec doc_transform(Fun, Doc1) -> Doc2 when
Fun :: fun((DocSum, Attrs) -> sumo:field_value()),
DocSum :: {sumo:field_type(), sumo:field_name(), sumo:field_value()},
Attrs :: sumo:field_attrs(),
Doc1 :: sumo_internal:doc(),
Doc2 :: sumo_internal:doc().
Fun :: fun((FieldType, FieldName, Value1, FieldAttrs) -> Value2),
FieldType :: sumo:field_type(),
FieldName :: sumo:field_name(),
Value1 :: sumo:field_value(),
FieldAttrs :: sumo:field_attrs(),
Value2 :: sumo:field_value(),
Doc1 :: sumo_internal:doc(),
Doc2 :: sumo_internal:doc().
doc_transform(Fun, Doc) ->
DocName = sumo_internal:doc_name(Doc),
Schema = sumo_internal:get_schema(DocName),
Expand All @@ -35,7 +38,7 @@ doc_transform(Fun, Doc) ->
FieldName = sumo_internal:field_name(Field),
FieldValue = sumo_internal:get_field(FieldName, Doc),
FieldAttrs = sumo_internal:field_attrs(Field),
NewValue = Fun({FieldType, FieldName, FieldValue}, FieldAttrs),
NewValue = Fun(FieldType, FieldName, FieldValue, FieldAttrs),
sumo_internal:set_field(FieldName, NewValue, Acc)
end, Doc, SchemaFields).

Expand Down

0 comments on commit 116f142

Please sign in to comment.