Skip to content

Commit

Permalink
Merge pull request #72 from klarna/respect-text-encoding
Browse files Browse the repository at this point in the history
Respect text encoding in !env tag
  • Loading branch information
jesperes authored Jul 24, 2023
2 parents 07e9faa + 6649519 commit f6ec1a2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [unreleased]

### Fixed

- `!env` tag now respects the `str_node_as_binary` option, so injected vars won't crash Mustache templates.

## [1.5.0] - 2023-07-05

### Added
Expand Down Expand Up @@ -40,5 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Open sourcing of an existing internal project

<!-- Markdown link dfn's -->
[unreleased]: https://github.com/klarna-incubator/bec/compare/v1.0.0...HEAD
[unreleased]: https://github.com/klarna-incubator/bec/compare/v1.5.0...HEAD
[1.5.0]: https://github.com/klarna-incubator/bec/releases/tag/1.5.0
[1.3.0]: https://github.com/klarna-incubator/bec/releases/tag/1.3.0
[1.0.0]: https://github.com/klarna-incubator/bec/releases/tag/1.0.0
14 changes: 11 additions & 3 deletions src/bec_node_env_variable.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

tags() -> [?TAG, "!env"].

construct_token(#yamerl_constr{detailed_constr = Detailed},
construct_token(#yamerl_constr{detailed_constr = Detailed,
ext_options = Options},
undefined,
#yamerl_scalar{text = Text} = Token) ->
case os:getenv(Text) of
Expand All @@ -28,11 +29,11 @@ construct_token(#yamerl_constr{detailed_constr = Detailed},
Node = #yamerl_str{ module = ?MODULE
, tag = ?TAG
, pres = Pres
, text = Value
, text = encode_text(Value, Options)
},
{finished, Node};
Value ->
{finished, Value}
{finished, encode_text(Value, Options)}
end;
construct_token(_, _, Token) ->
parsing_error(Token, "Invalid OS environment variable").
Expand All @@ -49,3 +50,10 @@ parsing_error(Token, Text) ->
column = ?TOKEN_COLUMN(Token)
},
throw(Error).

encode_text(Text, Options) ->
case proplists:get_value(str_node_as_binary, Options, false) of
false -> Text;
true -> unicode:characters_to_binary(Text);
Encoding -> unicode:characters_to_binary(Text, unicode, Encoding)
end.
28 changes: 26 additions & 2 deletions test/bec_yml_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
%% Testcases
-export([ environment_variable_substitution/1
, missing_environment_variable/1
, environment_variable_as_string/1
, environment_variable_as_binary/1
]).

%%==============================================================================
Expand Down Expand Up @@ -65,9 +67,9 @@ environment_variable_substitution(_Config) ->
Yml = <<"myval: !env TEST_VAL\n">>,
try
os:putenv("TEST_VAL", "abc"),
?assertEqual([#{<<"myval">> => "abc"}], bec_yml:decode(Yml)),
?assertEqual([#{<<"myval">> => <<"abc">>}], bec_yml:decode(Yml)),
os:putenv("TEST_VAL", "xyz"),
?assertEqual([#{<<"myval">> => "xyz"}], bec_yml:decode(Yml))
?assertEqual([#{<<"myval">> => <<"xyz">>}], bec_yml:decode(Yml))
after
os:unsetenv("TEST_VAL")
end.
Expand All @@ -77,3 +79,25 @@ missing_environment_variable(_Config) ->
Yml = <<"myval: !env TEST_VAL\n">>,
os:unsetenv("TEST_VAL"),
?assertThrow({yamerl_exception, _}, bec_yml:decode(Yml)).

-spec environment_variable_as_string(config()) -> ok.
environment_variable_as_string(_Config) ->
Yml = <<"myval: !env TEST_VAL\n">>,
Opts = [{map_node_format, map}],
try
os:putenv("TEST_VAL", "abc"),
?assertEqual([#{"myval" => "abc"}], yamerl:decode(Yml, Opts))
after
os:unsetenv("TEST_VAL")
end.

-spec environment_variable_as_binary(config()) -> ok.
environment_variable_as_binary(_Config) ->
Yml = <<"myval: !env TEST_VAL\n">>,
Opts = [str_node_as_binary, {map_node_format, map}],
try
os:putenv("TEST_VAL", "abc"),
?assertEqual([#{<<"myval">> => <<"abc">>}], yamerl:decode(Yml, Opts))
after
os:unsetenv("TEST_VAL")
end.

0 comments on commit f6ec1a2

Please sign in to comment.