Skip to content
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
16 changes: 14 additions & 2 deletions src/cuttlefish_datatypes.erl
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,18 @@ to_string(Value, MaybeExtendedDatatype) ->
{error, {type, {Value, MaybeExtendedDatatype}}}
end.

-spec from_string(term(), datatype()) -> term() | cuttlefish_error:error().
-spec from_string(term(), datatype()|'existing_atom') -> term() | cuttlefish_error:error().
from_string(Atom, atom) when is_atom(Atom) -> Atom;
from_string(String, atom) when is_list(String) -> list_to_atom(String);

from_string(String, existing_atom) when is_list(String) ->
try
list_to_existing_atom(String)
catch
error:badarg ->
{error, {conversion, {String, atom}}}
end;

from_string(Value, {enum, Enum}) ->
cuttlefish_enum:parse(Value, {enum, Enum});

Expand Down Expand Up @@ -338,7 +346,11 @@ to_string_unsupported_datatype_test() ->

from_string_atom_test() ->
?assertEqual(split_the, from_string(split_the, atom)),
?assertEqual(split_the, from_string("split_the", atom)).
?assertEqual(split_the, from_string("split_the", atom)),
?assertMatch({error, {conversion, _}}, from_string("unknown_atom_to_explode_table", existing_atom)),
%% Using `assertEqual' as above introduces the atom to be created,
%% so they don't actually test that unknown atoms work properly.
?assert(is_atom(from_string("unknown_atom_to_explode_table", atom))).

from_string_integer_test() ->
?assertEqual(32, from_string(32, integer)),
Expand Down