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

New data types: "plain" binary, tagged binary #52

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
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
35 changes: 34 additions & 1 deletion src/cuttlefish_datatypes.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
{list, datatype()}.
-type extended() :: { integer, integer() } |
{ string, string() } |
{ binary, binary() } |
{ file, file:filename() } |
{ directory, file:filename() } |
{ atom, atom() } |
Expand All @@ -56,7 +57,8 @@
{ {percent, integer}, integer() } |
{ {percent, float}, float() } |
{ float, float() } |
{ tagged_string, string() }.
{ tagged_string, string() } |
{ tagged_binary, string() }.
-type datatype_list() :: [ datatype() | extended() ].

-export_type([datatype/0, extended/0, datatype_list/0]).
Expand All @@ -73,6 +75,7 @@
-spec is_supported(any()) -> boolean().
is_supported(integer) -> true;
is_supported(string) -> true;
is_supported(binary) -> true;
is_supported(file) -> true;
is_supported(directory) -> true;
is_supported(flag) -> true;
Expand All @@ -95,6 +98,7 @@ is_supported({percent, integer}) -> true;
is_supported({percent, float}) -> true;
is_supported(float) -> true;
is_supported(tagged_string) -> true;
is_supported(tagged_binary) -> true;
is_supported({list, {list, _}}) ->
% lists of lists are not supported
false;
Expand All @@ -105,7 +109,9 @@ is_supported(_) -> false.
-spec is_extended(any()) -> boolean().
is_extended({integer, I}) when is_integer(I) -> true;
is_extended({string, S}) when is_list(S) -> true;
is_extended({binary, B}) when is_list(B) -> true;
is_extended({tagged_string, S}) when is_list(S) -> true;
is_extended({tagged_binary, B}) when is_list(B) -> true;
is_extended({atom, A}) when is_atom(A) -> true;
is_extended({file, F}) when is_list(F) -> true;
is_extended({directory, D}) when is_list(D) -> true;
Expand All @@ -131,7 +137,9 @@ is_extended(_) -> false.
-spec extended_from(extended()) -> datatype().
extended_from({integer, _}) -> integer;
extended_from({string, _}) -> string;
extended_from({binary, _}) -> binary;
extended_from({tagged_string, _}) -> tagged_string;
extended_from({tagged_binary, _}) -> tagged_binary;
extended_from({atom, _}) -> atom;
extended_from({file, _}) -> file;
extended_from({directory, _}) -> directory;
Expand Down Expand Up @@ -188,8 +196,12 @@ to_string(Bytesize, bytesize) when is_list(Bytesize) -> Bytesize;
to_string(Bytesize, bytesize) when is_integer(Bytesize) -> cuttlefish_bytesize:to_string(Bytesize);

to_string(String, string) when is_list(String) -> String;
to_string(Bin, binary) when is_list(Bin) -> Bin;
to_string(Bin, binary) when is_binary(Bin) -> binary_to_list(Bin);

to_string({Tag, String}, tagged_string) when is_list(Tag), is_list(String) -> Tag ++ ":" ++ String;
to_string({Tag, String}, tagged_binary) when is_list(Tag), is_list(String) -> Tag ++ ":" ++ String;
to_string({Tag, Bin}, tagged_binary) when is_list(Tag), is_binary(Bin) -> Tag ++ ":" ++ binary_to_list(Bin);

to_string(File, file) when is_list(File) -> File;

Expand Down Expand Up @@ -227,6 +239,8 @@ to_string(Value, MaybeExtendedDatatype) ->
from_string(Atom, atom) when is_atom(Atom) -> Atom;
from_string(String, atom) when is_list(String) -> list_to_atom(String);

from_string(String, binary) when is_list(String) -> list_to_binary(String);

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

Expand All @@ -249,6 +263,9 @@ from_string(String, fqdn) when is_list(String) ->
from_string(String, tagged_string) when is_list(String) ->
from_string_to_tagged_string(String, lists:split(string:rchr(String, $:), String));

from_string(String, tagged_binary) when is_list(String) ->
from_string_to_tagged_binary(String, lists:split(string:rchr(String, $:), String));

from_string({local, UDS, Port}, domain_socket) when is_list(UDS), is_integer(Port) -> {local, UDS, Port};
from_string(String, domain_socket) when is_list(String) ->
from_string_to_uds(String, lists:split(string:rchr(String, $:), String));
Expand Down Expand Up @@ -381,6 +398,15 @@ from_string_to_tagged_string(_String, {TagPlusColon, TaggedValue}) ->
Tag = droplast(TagPlusColon),
{list_to_atom(Tag), TaggedValue}.

from_string_to_tagged_binary(String, {[], String}) ->
%% does not follow the tag:value format convention
{error, {conversion, {String, "tagged binary"}}};

from_string_to_tagged_binary(_String, {TagPlusColon, TaggedValue}) ->
%% Drop the trailing colon from the tag
Tag = droplast(TagPlusColon),
{list_to_atom(Tag), list_to_binary(TaggedValue)}.

from_string_to_uds(String, {[], String}) ->
{error, {conversion, {String, 'UDS'}}};
from_string_to_uds(String, {UDSPlusColon, PortString}) ->
Expand Down Expand Up @@ -645,6 +671,7 @@ from_string_unsupported_datatype_test() ->
is_supported_test() ->
?assert(is_supported(integer)),
?assert(is_supported(string)),
?assert(is_supported(binary)),
?assert(is_supported(atom)),
?assert(is_supported(file)),
?assert(is_supported(directory)),
Expand All @@ -661,6 +688,7 @@ is_supported_test() ->
?assert(is_supported(bytesize)),
?assert(is_supported(domain_socket)),
?assert(is_supported(tagged_string)),
?assert(is_supported(tagged_binary)),
?assert(is_supported({list, string})),
?assert(not(is_supported({list, {list, string}}))),
?assert(not(is_supported(some_unsupported_type))),
Expand All @@ -675,6 +703,10 @@ is_extended_test() ->
?assertEqual(false, is_extended({string, string})),
?assertEqual(false, is_extended({string, 10})),

?assertEqual(true, is_extended({binary, "string"})),
?assertEqual(false, is_extended({binary, string})),
?assertEqual(false, is_extended({binary, 10})),

?assertEqual(true, is_extended({atom, atom})),
?assertEqual(false, is_extended({atom, "atom"})),
?assertEqual(false, is_extended({atom, 10})),
Expand Down Expand Up @@ -712,6 +744,7 @@ is_extended_test() ->
?assertEqual(true, is_extended({float, 0.1})),

?assertEqual(true, is_extended({tagged_string, "tag:value"})),
?assertEqual(true, is_extended({tagged_binary, "tag:value"})),

ok.

Expand Down
3 changes: 3 additions & 0 deletions test/binary.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{mapping, "a.b.binary", "cuttlefish.binary", [
{datatype, [binary]}
]}.
20 changes: 17 additions & 3 deletions test/cuttlefish_integration_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,26 @@ duration_test() ->
ErrConfig = cuttlefish_generator:map(Schema, Conf2),
?assertMatch({error, transform_datatypes, _}, ErrConfig).

binary_datatype_test() ->
Schema = cuttlefish_schema:files(["test/binary.schema"]),

Conf = conf_parse:parse(<<"a.b.binary = 96c5381e396dcc1aa8056709c42891473619c277\n">>),
NewConfig = cuttlefish_generator:map(Schema, Conf),
?assertEqual(<<"96c5381e396dcc1aa8056709c42891473619c277">>, proplists:get_value(binary, proplists:get_value(cuttlefish, NewConfig))).

tagged_string_test() ->
Schema = cuttlefish_schema:files(["test/tagged_string.schema"]),
Schema = cuttlefish_schema:files(["test/tagged_values.schema"]),

Conf = conf_parse:parse(<<"tagged_string = tagged:e614d97599dab483f\n">>),
NewConfig = cuttlefish_generator:map(Schema, Conf),
?assertEqual({tagged, "e614d97599dab483f"}, proplists:get_value(tagged_string, proplists:get_value(cuttlefish, NewConfig))).

tagged_binary_test() ->
Schema = cuttlefish_schema:files(["test/tagged_values.schema"]),

Conf = conf_parse:parse(<<"tagged_key = tagged:e614d97599dab483f\n">>),
Conf = conf_parse:parse(<<"tagged_binary = tagged:b614d97599dab483f\n">>),
NewConfig = cuttlefish_generator:map(Schema, Conf),
?assertEqual({tagged, "e614d97599dab483f"}, proplists:get_value(tagged_key, proplists:get_value(cuttlefish, NewConfig))).
?assertEqual({tagged, <<"b614d97599dab483f">>}, proplists:get_value(tagged_binary, proplists:get_value(cuttlefish, NewConfig))).

escaped_value_case1_test() ->
Schema = cuttlefish_schema:files(["test/escaped_values.schema"]),
Expand Down
3 changes: 0 additions & 3 deletions test/tagged_string.schema

This file was deleted.

7 changes: 7 additions & 0 deletions test/tagged_values.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{mapping, "tagged_string", "cuttlefish.tagged_string", [
{datatype, [tagged_string]}
]}.

{mapping, "tagged_binary", "cuttlefish.tagged_binary", [
{datatype, [tagged_binary]}
]}.
Loading