Skip to content

Commit

Permalink
Merge pull request #27 from basho/jd-dialyzer
Browse files Browse the repository at this point in the history
Cuttlefish, now Dialyzing
  • Loading branch information
joedevivo committed Sep 12, 2013
2 parents 2d8fa72 + 48178fa commit 09a1aa9
Show file tree
Hide file tree
Showing 25 changed files with 174 additions and 382 deletions.
28 changes: 25 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
APPS = kernel stdlib sasl erts ssl tools os_mon runtime_tools crypto inets \
public_key mnesia syntax_tools compiler
COMBO_PLT = $(HOME)/.cuttlefish_combo_dialyzer_plt

.PHONY: deps

all: deps compile
Expand All @@ -6,9 +10,6 @@ all: deps compile
deps:
./rebar get-deps

docs:
./rebar skip_deps=true doc

docsclean:
@rm -rf doc/*.png doc/*.html doc/*.css edoc-info

Expand All @@ -20,3 +21,24 @@ clean:

distclean: clean
@rm -rf cuttlefish deps

test: all
./rebar skip_deps=true eunit

docs: deps
./rebar skip_deps=true doc

build_plt: compile
dialyzer --build_plt --output_plt $(COMBO_PLT) --apps $(APPS) \
deps/*/ebin

check_plt: compile
dialyzer --check_plt --plt $(COMBO_PLT) --apps $(APPS) \
deps/*/ebin

dialyzer: compile
@echo
@echo Use "'make check_plt'" to check PLT prior to using this target.
@echo Use "'make build_plt'" to build PLT prior to using this target.
@echo
dialyzer --plt $(COMBO_PLT) ebin
2 changes: 1 addition & 1 deletion src/cuttlefish_advanced.erl
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ overlay_test() ->

ok.

-endif.
-endif.
2 changes: 1 addition & 1 deletion src/cuttlefish_bytesize.erl
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ parse_test() ->
?assertEqual(error, parse("10MB10kb")),
ok.

-endif.
-endif.
23 changes: 15 additions & 8 deletions src/cuttlefish_conf.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
%% -------------------------------------------------------------------
-module(cuttlefish_conf).

-type conf() :: [{ [string()], any() }].
-export_type([conf/0]).

-export([
generate/1,
generate_file/2,
Expand Down Expand Up @@ -53,22 +56,25 @@ file(Filename) ->
remove_duplicates(Conf)
end.

generate(Schema) ->
-spec generate([cuttlefish_mapping:mapping()]) -> [string()].
generate(Mappings) ->
lists:foldl(
fun(SchemaElement, ConfFile) ->
ConfFile ++ generate_element(SchemaElement)
end, [], Schema).
fun(Mapping, ConfFile) ->
ConfFile ++ generate_element(Mapping)
end, [], Mappings).

generate_file(Schema, Filename) ->
ConfFileLines = generate(Schema),
-spec generate_file([cuttlefish_mapping:mapping()], string()) -> ok.
generate_file(Mappings, Filename) ->
ConfFileLines = generate(Mappings),

{ok, S} = file:open(Filename, write),
{ok, S} = file:open(Filename, [write]),
[ begin
io:format(S, "~s~n", [lists:flatten(Line)])
end || Line <- ConfFileLines],
file:close(S),
ok.

-spec generate_element(cuttlefish_mapping:mapping()) -> [string()].
generate_element(MappingRecord) ->
Default = cuttlefish_mapping:default(MappingRecord),
Key = cuttlefish_mapping:variable(MappingRecord),
Expand Down Expand Up @@ -100,6 +106,7 @@ generate_element(basic, _Default, undefined) -> default;
generate_element(basic, _, _Comment) -> commented;
generate_element(_Level, _Default, _Commented) -> no.

-spec generate_comments(cuttlefish_mapping:mapping()) -> [string()].
generate_comments(MappingRecord) ->
Doc = cuttlefish_mapping:doc(MappingRecord),
[ "## " ++ D || D <- Doc].
Expand Down Expand Up @@ -193,4 +200,4 @@ files_one_nonent() ->
?assertEqual("1", proplists:get_value(["a","b","d"], Conf)),
ok.

-endif.
-endif.
4 changes: 2 additions & 2 deletions src/cuttlefish_datatypes.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
-compile(export_all).
-endif.

-opaque datatype() :: integer | string | {enum, [atom()]} | ip | {duration, cuttlefish_duration:time_unit() } | bytesize.
-type datatype() :: integer | string | {enum, [atom()]} | ip | {duration, cuttlefish_duration:time_unit() } | bytesize.
-export_type([datatype/0]).

-export([is_supported/1, from_string/2, to_string/2]).
Expand Down Expand Up @@ -166,4 +166,4 @@ from_string_duration_secs_test() ->
from_string_string_test() ->
?assertEqual("string", from_string("string", string)).

-endif.
-endif.
2 changes: 1 addition & 1 deletion src/cuttlefish_duration.erl
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,4 @@ parse_2_test() ->
?assertEqual(1, parse("1ms", f)),
ok.

-endif.
-endif.
17 changes: 10 additions & 7 deletions src/cuttlefish_generator.erl
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ map({Translations, Mappings, Validators} = Schema, Config) ->
end,
{[], {[],[]}},
Mappings),
lager:info("Applying 1:1 Mappings"),
lager:info("Applied 1:1 Mappings"),

TranslationsToDrop = TranslationsToMaybeDrop -- TranslationsToKeep,
%% The fold handles the translations. After we've build the DirecetMappings,
%% we use that to seed this fold's accumulator. As we go through each translation
%% we write that to the `app.config` that lives in the accumutator.
lists:foldl(
Return = lists:foldl(
fun(TranslationRecord, Acc) ->
Mapping = cuttlefish_translation:mapping(TranslationRecord),
Xlat = cuttlefish_translation:func(TranslationRecord),
Expand All @@ -124,7 +124,9 @@ map({Translations, Mappings, Validators} = Schema, Config) ->
end
end,
DirectMappings,
Translations).
Translations),
lager:info("Applied Translations"),
Return.

%for each token, is it special?
%
Expand Down Expand Up @@ -215,6 +217,7 @@ add_defaults(Conf, Mappings) ->
%% {"multi_backend",
%% ["bitcask_mult","leveldb_mult","leveldb_mult2","memory_mult"}]
%%%%%%%%%%%%%%%%%%%%%%%%
-spec get_possible_values_for_fuzzy_matches(cuttlefish_conf:conf(), [cuttlefish_mapping:mapping()]) -> [{string(), [string()]}].
get_possible_values_for_fuzzy_matches(Conf, Mappings) ->
%% Get a list of all the key definitions from the schema
%% that involve a pattern match
Expand All @@ -229,10 +232,9 @@ get_possible_values_for_fuzzy_matches(Conf, Mappings) ->
cuttlefish_util:fuzzy_variable_match(Variable, VariableDef)
end,
FuzzyVariableDefs),
case length(Fuzz) of
0 -> FuzzyMatches;
_ ->
VD = hd(Fuzz),
case Fuzz of
[] -> FuzzyMatches;
[VD|_] ->
ListOfVars = [ Var || {_, Var } <- cuttlefish_util:matches_for_variable_def(VD, [{Variable, 0}])],
orddict:append_list(VD, ListOfVars, FuzzyMatches)
end
Expand Down Expand Up @@ -287,6 +289,7 @@ get_possible_values_for_fuzzy_matches(Conf, Mappings) ->
PrefixesWithoutDefaults,
DefaultsNeeded).

-spec transform_datatypes(cuttlefish_conf:conf(), [cuttlefish_mapping:mapping()]) -> cuttlefish_conf:conf().
transform_datatypes(Conf, Mappings) ->
lists:foldl(
fun({Variable, Value}, Acc) ->
Expand Down
17 changes: 11 additions & 6 deletions src/cuttlefish_mapping.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
datatype_options :: any(),
level = basic :: basic | intermediate | advanced,
doc = [] :: list(),
include_default::string(),
include_default = undefined :: string() | undefined,
validators = [] :: [string()]
}).

-opaque mapping() :: #mapping{}.
-type mapping() :: #mapping{}.
-export_type([mapping/0]).

-export([
Expand Down Expand Up @@ -91,7 +91,7 @@ parse(X) -> {error, io_lib:format("poorly formatted input to cuttlefish_mapping:
is_mapping(M) ->
is_tuple(M) andalso element(1, M) =:= mapping.

-spec variable(mapping()) -> string().
-spec variable(mapping()) -> [string()].
variable(M) -> M#mapping.variable.

-spec mapping(mapping()) -> string().
Expand All @@ -112,15 +112,20 @@ level(M) -> M#mapping.level.
-spec doc(mapping()) -> [string()].
doc(M) -> M#mapping.doc.

-spec include_default(mapping()) -> string().
-spec include_default(mapping()) -> string() | undefined.
include_default(M) -> M#mapping.include_default.

-spec validators(mapping()) -> [string()].
validators(M) -> M#mapping.validators.

-spec validators(mapping(), [cuttlefish_validator:validator()]) -> [cuttlefish_validator:validator()].
validators(M, Validators) ->
[ lists:keyfind(VName, 2, Validators) || VName <- M#mapping.validators].
lists:foldr(fun(VName, Vs) ->
case lists:keyfind(VName, 2, Validators) of
false -> Vs;
V -> [V|Vs]
end
end, [], M#mapping.validators).

-spec replace(mapping(), [mapping()]) -> [mapping()].
replace(Mapping, ListOfMappings) ->
Expand Down Expand Up @@ -303,4 +308,4 @@ validators_test() ->

ok.

-endif.
-endif.
1 change: 0 additions & 1 deletion src/cuttlefish_rebar_plugin.erl
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,3 @@ should_i_run(ReltoolConfig) ->
false ->
no
end.

18 changes: 9 additions & 9 deletions src/cuttlefish_schema.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
-compile(export_all).
-endif.

-type errorlist() :: {error, [string()]}.
-type errorlist() :: {error, string()|[string()]}.
-type schema() :: {[cuttlefish_translation:translation()], [cuttlefish_mapping:mapping()], [cuttlefish_validator:validator()]}.
-export_type([schema/0]).

files(ListOfSchemaFiles) ->
lists:foldl(
Expand Down Expand Up @@ -62,9 +64,7 @@ files(ListOfSchemaFiles) ->
ValidatorAcc,
Validators),

{NewTranslations, NewMappings, NewValidators};
_ ->
lager:error("Unknown error parsing schema file: ~p", [SchemaFile])
{NewTranslations, NewMappings, NewValidators}
end
end,
{[], [], []},
Expand All @@ -74,7 +74,7 @@ files(ListOfSchemaFiles) ->
[cuttlefish_translation:translation()],
[cuttlefish_mapping:mapping()],
[cuttlefish_validator:validator()]
} | error.
} | errorlist().
file(Filename) ->
{ok, B} = file:read_file(Filename),
%% TODO: Hardcoded utf8
Expand Down Expand Up @@ -145,9 +145,9 @@ parse_schema(Tokens, Comments) ->
-spec parse_schema(
[any()],
[any()],
[cuttlefish_translation:translation() | cuttlefish_mapping:mapping() | cuttlefish_validator:validator()]
[cuttlefish_translation:translation() | cuttlefish_mapping:mapping() | cuttlefish_validator:validator() | errorlist()]
) ->
[cuttlefish_translation:translation() | cuttlefish_mapping:mapping() | cuttlefish_validator:validator()].
[cuttlefish_translation:translation() | cuttlefish_mapping:mapping() | cuttlefish_validator:validator() | errorlist()].
parse_schema([], _LeftoverComments, Acc) ->
lists:reverse(Acc);
parse_schema(ScannedTokens, CommentTokens, Acc) ->
Expand Down Expand Up @@ -187,7 +187,7 @@ parse_schema_tokens(Scanned, Acc=[{dot, LineNo}|_]) ->
parse_schema_tokens([H|Scanned], Acc) ->
parse_schema_tokens(Scanned, [H|Acc]).

-spec parse(list()) -> { mapping | translation, tuple()} | errorlist().
-spec parse(list()) -> { mapping | translation | validator, tuple()} | errorlist().
parse(Scanned) ->
case erl_parse:parse_exprs(Scanned) of
{ok, Parsed} ->
Expand Down Expand Up @@ -342,4 +342,4 @@ files_test() ->

ok.

-endif.
-endif.
4 changes: 2 additions & 2 deletions src/cuttlefish_translation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
mapping::string(),
func::fun()
}).
-opaque translation() :: #translation{}.
-type translation() :: #translation{}.
-export_type([translation/0]).

-export([
Expand Down Expand Up @@ -146,4 +146,4 @@ remove_duplicates_test() ->
?assertEqual(Expected, NewTranslations),
ok.

-endif.
-endif.
4 changes: 2 additions & 2 deletions src/cuttlefish_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ conf_get_value(Variable, ConfigProplist) ->
conf_get_value(tokenize_variable_key(Variable), ConfigProplist).

%% @doc replace the element in a proplist
-spec replace_proplist_value(string(), any(), [{string(), any()}]) -> [{string(), any()}].
-spec replace_proplist_value(atom() | string(), any(), [{string(), any()}]) -> [{string(), any()}].
replace_proplist_value(Key, Value, Proplist) ->
proplists:delete(Key, Proplist) ++ [{Key, Value}].

Expand Down Expand Up @@ -131,7 +131,7 @@ tokenize_variable_key([Char|Rest], Part, Acc) ->

%% @doc given a KeyDef "a.b.$c.d", what are the possible values for $c
%% in the set of Keys in Conf = [{Key, Value}]?
-spec matches_for_variable_def([string()], [{[string()], any()}]) -> [string()].
-spec matches_for_variable_def([string()], [{[string()], any()}]) -> [{string(), any()}].
matches_for_variable_def(VariableDef, Conf) ->
lists:foldl(
fun({Variable, _}, Acc) ->
Expand Down
2 changes: 1 addition & 1 deletion src/cuttlefish_validator.erl
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,4 @@ remove_duplicates_test() ->
?assertEqual(Expected, NewValidators),
ok.

-endif.
-endif.
2 changes: 1 addition & 1 deletion src/cuttlefish_vmargs.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ stringify_test() ->
[ ?assertEqual(E, V) || {E, V} <- lists:zip(Expected, VMArgs)],
ok.

-endif.
-endif.
2 changes: 1 addition & 1 deletion test/advanced.config
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
%% Default is 1 build per hour.
{anti_entropy_build_limit, {1, 3600000}}
]}
].
].
2 changes: 1 addition & 1 deletion test/bad_erlang.schema
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
%% @doc bad setting
{mapping, "setting2, "nested.nesting", [
{datatype, string}
}.
}.
4 changes: 2 additions & 2 deletions test/cuttlefish_integration_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ proplist_equals(Expected, Actual) ->
{false, false} ->
?assertEqual({EKey, ExpectedValue}, {EKey, ActualValue});
_ ->
?assert(false)
?assertEqual({EKey, ExpectedValue}, {EKey, ActualValue})
end
end || EKey <- ExpectedKeys].

Expand All @@ -135,4 +135,4 @@ is_proplist(Proplist) when is_list(Proplist) ->
is_tuple(X) andalso tuple_size(X) =:= 2
end,
Proplist);
is_proplist(_) -> false.
is_proplist(_) -> false.
Loading

0 comments on commit 09a1aa9

Please sign in to comment.