Browse Source

Merge branch 'dcy-master'

Andreas Stenius 10 years ago
parent
commit
91a5d1cc72
3 changed files with 39 additions and 2 deletions
  1. 1 0
      .travis.yml
  2. 26 2
      src/erlydtl_runtime.erl
  3. 12 0
      test/erlydtl_test_defs.erl

+ 1 - 0
.travis.yml

@@ -2,6 +2,7 @@ language: erlang
 otp_release:
 otp_release:
 # Test on all supported releases accepted by the `require_otp_vsn` in rebar.config
 # Test on all supported releases accepted by the `require_otp_vsn` in rebar.config
 #  - 17.0 not yet available on travis!
 #  - 17.0 not yet available on travis!
+  - 17.3
   - R16B03-1
   - R16B03-1
 #  - R16B03 this version is broken!
 #  - R16B03 this version is broken!
   - R16B02
   - R16B02

+ 26 - 2
src/erlydtl_runtime.erl

@@ -108,8 +108,32 @@ find_value(Key, Tuple) when is_tuple(Tuple) ->
                     undefined
                     undefined
             end
             end
     end;
     end;
-find_value(_, _) ->
-    undefined.
+find_value(Key, Map) ->
+    case erlang:is_builtin(erlang, is_map, 1) andalso erlang:is_map(Map) of
+        true  -> find_map_value(Key, Map);
+        false -> undefined
+    end.
+
+find_map_value(Key, Map) when is_atom(Key) ->
+    case maps:find(Key, Map) of
+        error           -> find_map_value(atom_to_list(Key), Map);
+        {ok, Value}     -> Value
+    end;
+find_map_value(Key, Map) when is_list(Key) ->
+    case maps:find(Key, Map) of
+        error           -> find_map_value(list_to_binary(Key), Map);
+        {ok, Value}     -> Value
+    end;
+find_map_value(Key, Map) when is_binary(Key) ->
+    case maps:find(Key, Map) of
+        error           -> undefined;
+        {ok, Value}     -> Value
+    end;
+find_map_value(Key, Map) ->
+    case maps:find(Key, Map) of
+        error           -> undefined;
+        {ok, Value}     -> Value
+    end.
 
 
 fetch_value(Key, Data, Options) ->
 fetch_value(Key, Data, Options) ->
     fetch_value(Key, Data, Options, []).
     fetch_value(Key, Data, Options, []).

+ 12 - 0
test/erlydtl_test_defs.erl

@@ -37,6 +37,18 @@ all_test_defs() ->
           output = <<"test">>
           output = <<"test">>
          }
          }
       ]},
       ]},
+     {"maps",
+      case erlang:is_builtin(erlang, is_map, 1) of
+          false -> [];
+          true ->
+              [#test{
+                  title = "simple test",
+                  source = <<"{{ msg.hello }}">>,
+                  render_vars = [{msg, maps:put(hello, "world", maps:new())}],
+                  output = <<"world">>
+                 }
+              ]
+      end},
      {"comment",
      {"comment",
       [{"comment block is excised",
       [{"comment block is excised",
         <<"bob {% comment %}(moron){% endcomment %} loblaw">>,
         <<"bob {% comment %}(moron){% endcomment %} loblaw">>,