Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convert_value_to_type for atom type #268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/boss_record_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
-spec database_table(atom() | tuple()) -> any().
-spec belongs_to_types(atom()) -> any().
-spec ensure_loaded(atom()) -> boolean().
-type target_types() :: 'binary' | 'boolean' | 'date' | 'datetime' | 'float' | 'integer' | 'string' | 'timestamp' | 'undefined'.
-type target_types() :: 'atom' | 'binary' | 'boolean' | 'date' | 'datetime' | 'float' | 'integer' | 'string' | 'timestamp' | 'undefined'.
-spec convert_value_to_type(_,target_types()) -> any().

run_before_hooks(_OldRecord, Record, true) ->
Expand Down Expand Up @@ -150,6 +150,12 @@ convert_value_to_type(Val, binary) when is_list(Val) ->
list_to_binary(Val);
convert_value_to_type(Val, binary) when is_binary(Val) ->
Val;
convert_value_to_type(Val, atom) when is_atom(Val) ->
Val;
convert_value_to_type(Val, atom) when is_binary(Val) ->
binary_to_atom(Val, utf8);
convert_value_to_type(Val, atom) when is_list(Val) ->
list_to_atom(Val);
convert_value_to_type({{D1, D2, D3}, {T1, T2, T3}}, Type) when is_integer(D1), is_integer(D2), is_integer(D3),
is_integer(T1), is_integer(T2), is_float(T3) ->
convert_value_to_type({{D1, D2, D3}, {T1, T2, round(T3)}}, Type);
Expand Down