From 1137103147daacf59e5d6205c25b5e32a0e66215 Mon Sep 17 00:00:00 2001 From: soranoba Date: Thu, 11 Nov 2021 12:16:48 +0900 Subject: [PATCH] Support utf8-binary and utf8-atom (OTP20+) --- ct/bbmustache_SUITE.erl | 28 ++++++++++++++++++++++++++-- rebar.config | 1 + src/bbmustache.erl | 2 +- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/ct/bbmustache_SUITE.erl b/ct/bbmustache_SUITE.erl index 9dae254..c0e6e6a 100644 --- a/ct/bbmustache_SUITE.erl +++ b/ct/bbmustache_SUITE.erl @@ -206,15 +206,39 @@ partial_custom_reader_ct(Config) -> ?assertEqual(File, bbmustache:compile(Template, ?debug((?config(data_conv, Config))([])), ?config2(options, Config, []) ++ [{partial_file_reader, fun(_, Key) -> Key end}])). +-ifdef(unicode_supported). unicode_render_ct(Config) -> Template = bbmustache:parse_file(filename:join([?config(data_dir, Config), <<"unicode.mustache">>])), {ok, File} = file:read_file(filename:join([?config(data_dir, Config), <<"unicode.result">>])), - Data = [ + Data1 = [ {"whoami", "猫"}, {"name", "まだない"} ], - ?assertEqual(File, bbmustache:compile(Template, ?debug((?config(data_conv, Config))(Data)), ?config2(options, Config, []))). + ?assertEqual(File, bbmustache:compile(Template, ?debug((?config(data_conv, Config))(Data1)), ?config2(options, Config, []))), + + Data2 = [ + {"whoami", <<"猫"/utf8>>}, + {"name", <<"まだない"/utf8>>} + ], + ?assertEqual(File, bbmustache:compile(Template, ?debug((?config(data_conv, Config))(Data2)), ?config2(options, Config, []))), + + Data3 = [ + {"whoami", '猫'}, + {"name", 'まだない'} + ], + ?assertEqual(File, bbmustache:compile(Template, ?debug((?config(data_conv, Config))(Data3)), ?config2(options, Config, []))). +-else. +unicode_render_ct(Config) -> + Template = bbmustache:parse_file(filename:join([?config(data_dir, Config), <<"unicode.mustache">>])), + {ok, File} = file:read_file(filename:join([?config(data_dir, Config), <<"unicode.result">>])), + + Data1 = [ + {"whoami", "猫"}, + {"name", "まだない"} + ], + ?assertEqual(File, bbmustache:compile(Template, ?debug((?config(data_conv, Config))(Data1)), ?config2(options, Config, []))). +-endif. %%---------------------------------------------------------------------------------------------------------------------- %% Internal Functions diff --git a/rebar.config b/rebar.config index 7d67e98..a14b258 100644 --- a/rebar.config +++ b/rebar.config @@ -2,6 +2,7 @@ {erl_opts, [ {platform_define, "^[0-9]+", namespaced_types}, + {platform_define, "^20", unicode_supported}, warnings_as_errors, warn_export_all, warn_untyped_record diff --git a/src/bbmustache.erl b/src/bbmustache.erl index 22ea09b..23c6f6a 100644 --- a/src/bbmustache.erl +++ b/src/bbmustache.erl @@ -252,7 +252,7 @@ default_value_serializer(X) when is_map(X); is_tuple(X) -> default_value_serializer(X) when X =:= null; X =:= nil -> []; default_value_serializer(X) when is_atom(X) -> - list_to_binary(atom_to_list(X)); + unicode:characters_to_binary(atom_to_list(X)); default_value_serializer(X) -> X.