- bbmustache v1.8.0
- refer: soranoba/bbmustache#41
This patch is provided with the same license as bbmustache.
If you patch bbmustache, you don't need to worry about the license.
This patch is provided with the same license as bbmustache.
If you patch bbmustache, you don't need to worry about the license.
diff --git a/src/bbmustache.erl b/src/bbmustache.erl | |
index 0d43d74..965c8f7 100644 | |
--- a/src/bbmustache.erl | |
+++ b/src/bbmustache.erl | |
@@ -628,13 +628,17 @@ get_data_recursive_impl([], Data, _) -> | |
get_data_recursive_impl([<<".">>], Data, _) -> | |
{ok, Data}; | |
get_data_recursive_impl([Key | RestKey] = Keys, Data, #?MODULE{context_stack = Stack} = State) -> | |
- case is_recursive_data(Data) andalso find_data(convert_keytype(Key, State), Data) of | |
- {ok, ChildData} -> | |
- get_data_recursive_impl(RestKey, ChildData, State#?MODULE{context_stack = []}); | |
- _ when Stack =:= [] -> | |
- error; | |
- _ -> | |
- get_data_recursive_impl(Keys, hd(Stack), State#?MODULE{context_stack = tl(Stack)}) | |
+ case is_list(Data) andalso find_index_data_from_lists(Key, Data) of | |
+ {ok, ChildData} -> {ok, ChildData}; | |
+ _ -> | |
+ case is_recursive_data(Data) andalso find_data(convert_keytype(Key, State), Data) of | |
+ {ok, ChildData} -> | |
+ get_data_recursive_impl(RestKey, ChildData, State#?MODULE{context_stack = []}); | |
+ _ when Stack =:= [] -> | |
+ error; | |
+ _ -> | |
+ get_data_recursive_impl(Keys, hd(Stack), State#?MODULE{context_stack = tl(Stack)}) | |
+ end | |
end. | |
%% @doc find the value of the specified key from {@link recursive_data/0} | |
@@ -669,3 +673,25 @@ is_recursive_data(_) -> false. | |
is_recursive_data([Tuple | _]) when is_tuple(Tuple) -> true; | |
is_recursive_data(_) -> false. | |
-endif. | |
+ | |
+%% @doc When the value can convert integer, it returns the integer. Otherwise it returns error. | |
+-spec safe_binary_to_integer(binary()) -> integer() | error. | |
+safe_binary_to_integer(Bin) -> | |
+ try | |
+ binary_to_integer(Bin) | |
+ catch _:_ -> | |
+ error | |
+ end. | |
+ | |
+-spec find_index_data_from_lists(binary(), list()) -> {ok, term()} | error. | |
+find_index_data_from_lists(IndexBin, List) -> | |
+ case safe_binary_to_integer(IndexBin) of | |
+ Index when is_integer(Index), Index >= 0 -> | |
+ try | |
+ {ok, lists:nth(Index + 1, List)} | |
+ catch _:_ -> | |
+ error | |
+ end; | |
+ _ -> | |
+ error | |
+ end. |