From 53e9e37dee3c1bfa030604110cfd3fe39c44cef0 Mon Sep 17 00:00:00 2001 From: Sean Cribbs Date: Fri, 26 Dec 2014 08:36:04 -0600 Subject: [PATCH] Factor out the fold function from apply_mappings --- src/cuttlefish_generator.erl | 50 +++++++++++++++++------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/src/cuttlefish_generator.erl b/src/cuttlefish_generator.erl index 77d95b65..a4ffd351 100644 --- a/src/cuttlefish_generator.erl +++ b/src/cuttlefish_generator.erl @@ -122,44 +122,40 @@ map_validate(Schema, Conf) -> -spec apply_mappings(cuttlefish_schema:schema(), cuttlefish_conf:conf()) -> {[proplists:property()], [string()]}. apply_mappings({Translations, Mappings, _Validators}, Conf) -> - %% This fold handles 1:1 mappings, that have no cooresponding translations + %% This fold handles 1:1 mappings, that have no corresponding translations %% The accumlator is the app.config proplist that we start building from %% these 1:1 mappings, hence the return "DirectMappings". %% It also builds a list of "TranslationsToDrop". It's basically saying that %% if a user didn't actually configure this setting in the .conf file and %% there's no default in the schema, then there won't be enough information %% during the translation phase to succeed, so we'll earmark it to be skipped - {DirectMappings, {TranslationsToMaybeDrop, TranslationsToKeep}} = lists:foldr( - fun(MappingRecord, {ConfAcc, {MaybeDrop, Keep}}) -> - Mapping = cuttlefish_mapping:mapping(MappingRecord), - Default = cuttlefish_mapping:default(MappingRecord), - Variable = cuttlefish_mapping:variable(MappingRecord), - case { - Default =/= undefined orelse cuttlefish_conf:is_variable_defined(Variable, Conf), - lists:any( - fun(T) -> - cuttlefish_translation:mapping(T) =:= Mapping - end, - Translations) - } of - {true, false} -> - Tokens = cuttlefish_variable:tokenize(Mapping), - NewValue = proplists:get_value(Variable, Conf), - {set_value(Tokens, ConfAcc, NewValue), - {MaybeDrop, ordsets:add_element(Mapping,Keep)}}; - {true, true} -> - {ConfAcc, {MaybeDrop, ordsets:add_element(Mapping,Keep)}}; - _ -> - {ConfAcc, {ordsets:add_element(Mapping,MaybeDrop), Keep}} - end - end, - {[], {ordsets:new(),ordsets:new()}}, - Mappings), + {DirectMappings, {TranslationsToMaybeDrop, TranslationsToKeep}} = + lists:foldr(fun(M, A) -> fold_apply_mappings(M, A, Translations, Conf) end, + {[], {ordsets:new(),ordsets:new()}}, + Mappings), lager:debug("Applied 1:1 Mappings"), TranslationsToDrop = TranslationsToMaybeDrop -- TranslationsToKeep, {DirectMappings, TranslationsToDrop}. +fold_apply_mappings(MappingRecord, {ConfAcc, {MaybeDrop, Keep}}, Translations, Conf) -> + Mapping = cuttlefish_mapping:mapping(MappingRecord), + Default = cuttlefish_mapping:default(MappingRecord), + Variable = cuttlefish_mapping:variable(MappingRecord), + IsDefined = (Default =/= undefined orelse cuttlefish_conf:is_variable_defined(Variable, Conf)), + HasTranslation = lists:any(fun(T) -> cuttlefish_translation:mapping(T) =:= Mapping end, Translations), + if + IsDefined andalso not HasTranslation -> + Tokens = cuttlefish_variable:tokenize(Mapping), + NewValue = proplists:get_value(Variable, Conf), + {set_value(Tokens, ConfAcc, NewValue), + {MaybeDrop, ordsets:add_element(Mapping, Keep)}}; + IsDefined andalso HasTranslation -> + {ConfAcc, {MaybeDrop, ordsets:add_element(Mapping,Keep)}}; + true -> + {ConfAcc, {ordsets:add_element(Mapping,MaybeDrop), Keep}} + end. + -spec apply_translations(cuttlefish_schema:schema(), cuttlefish_conf:conf(), [proplists:property()], [string()]) -> [proplists:property()] | {error, atom(), cuttlefish_error:errorlist()}.