Просмотр исходного кода

Bug fix in saving template containing custom_tags_modules feature

* Bug fix: when compiling templates with custom tags with a given
custom_tags_modules option, the arguments are saved as atoms in the
AST as opposed to strings. This causes a failure when decompiling
the beam and printing its source by erl_prettypr.

In order to illustrate the issue compile the template test.dtl containing:

    {% tr xxx="ABC" %}

with options:

    [
        , {doc_root, "src"}
        , {out_dir,   "ebin"}
        , {compiler_options, [verbose, debug_info, report, return]}
        , {custom_tags_modules, [custom_tags]}
        , {force_recompile, true}
    ].

where custom_tags module is:

    -module(custom_tags).

    -export([tr/2]).

    tr(Vars, Context) ->
        io:format("Vars: ~p\n  Context: ~p\n", [Vars, Context]),
        [].

Observe the return code of erlydtl_compiler:compile/3, and also try to
decompile the emitted beam with:
[http://erlang.org/pipermail/erlang-questions/2006-January/018813.html]
serge 12 лет назад
Родитель
Сommit
15a46cdd0a
1 измененных файлов с 8 добавлено и 3 удалено
  1. 8 3
      src/erlydtl_compiler.erl

+ 8 - 3
src/erlydtl_compiler.erl

@@ -1237,17 +1237,22 @@ full_path(File, DocRoot) ->
 %% Custom tags
 %%-------------------------------------------------------------------
 
+key_to_string(Key) when is_atom(Key) ->
+    erl_syntax:string(atom_to_list(Key));
+key_to_string(Key) when is_list(Key) ->
+    erl_syntax:string(Key).
+
 tag_ast(Name, Args, Context, TreeWalker) ->
     {InterpretedArgs, AstInfo} = lists:mapfoldl(fun
             ({{identifier, _, Key}, {string_literal, _, Value}}, AstInfoAcc) ->
                 {{StringAst, StringAstInfo}, _} = string_ast(unescape_string_literal(Value), Context, TreeWalker),
-                {erl_syntax:tuple([erl_syntax:string(Key), StringAst]), merge_info(StringAstInfo, AstInfoAcc)};
+                {erl_syntax:tuple([key_to_string(Key), StringAst]), merge_info(StringAstInfo, AstInfoAcc)};
             ({{identifier, _, Key}, {trans, StringLiteral}}, AstInfoAcc) ->
                 {{TransAst, TransAstInfo}, _} = translated_ast(StringLiteral, Context, TreeWalker),
-                {erl_syntax:tuple([erl_syntax:string(Key), TransAst]), merge_info(TransAstInfo, AstInfoAcc)};
+                {erl_syntax:tuple([key_to_string(Key), TransAst]), merge_info(TransAstInfo, AstInfoAcc)};
             ({{identifier, _, Key}, Value}, AstInfoAcc) ->
                 {AST, VarName} = resolve_variable_ast(Value, Context),
-                {erl_syntax:tuple([erl_syntax:string(Key), format(AST,Context, TreeWalker)]), merge_info(#ast_info{var_names=[VarName]}, AstInfoAcc)}
+                {erl_syntax:tuple([key_to_string(Key), format(AST,Context, TreeWalker)]), merge_info(#ast_info{var_names=[VarName]}, AstInfoAcc)}
         end, #ast_info{}, Args),
 
     {RenderAst, RenderInfo} = custom_tags_modules_ast(Name, InterpretedArgs, Context),