difftreelog
fix std.decodeUTF8 should be lossy by default
in: master
6 files changed
crates/jrsonnet-stdlib/src/encoding.rsdiffbeforeafterboth1use base64::{engine::general_purpose::STANDARD, Engine};2use jrsonnet_evaluator::{3 bail,4 function::builtin,5 runtime_error,6 typed::{Either, Either2},7 IBytes, IStr, Result,8};910#[builtin]11pub fn builtin_encode_utf8(str: IStr) -> IBytes {12 str.cast_bytes()13}1415#[builtin]16pub fn builtin_decode_utf8(arr: IBytes, #[default(true)] lossy: bool) -> Result<IStr> {17 match arr.clone().cast_str() {18 Some(s) => Ok(s),19 None if lossy => Ok(String::from_utf8_lossy(arr.as_slice()).into()),20 None => {21 bail!("bad utf8")22 }23 }24}2526#[builtin]27pub fn builtin_base64(input: Either![IStr, IBytes]) -> String {28 use Either2::*;29 match input {30 A(l) => STANDARD.encode(l.as_bytes()),31 B(a) => STANDARD.encode(a.as_slice()),32 }33}3435#[builtin]36pub fn builtin_base64_decode_bytes(str: IStr) -> Result<IBytes> {37 Ok(STANDARD38 .decode(str.as_bytes())39 .map_err(|e| runtime_error!("invalid base64: {e}"))?40 .as_slice()41 .into())42}4344#[builtin]45pub fn builtin_base64_decode(str: IStr) -> Result<String> {46 let bytes = STANDARD47 .decode(str.as_bytes())48 .map_err(|e| runtime_error!("invalid base64: {e}"))?;49 String::from_utf8(bytes).map_err(|_| runtime_error!("bad utf8"))50}tests/golden/issue187.jsonnetdiffbeforeafterboth--- /dev/null
+++ b/tests/golden/issue187.jsonnet
@@ -0,0 +1 @@
+std.decodeUTF8(std.encodeUTF8('foo bar ') + [255] + std.encodeUTF8(' baz'))
tests/golden/issue187.jsonnet.goldendiffbeforeafterboth--- /dev/null
+++ b/tests/golden/issue187.jsonnet.golden
@@ -0,0 +1 @@
+"foo bar � baz"
\ No newline at end of file
tests/golden/issue187.rev.jsonnetdiffbeforeafterboth--- /dev/null
+++ b/tests/golden/issue187.rev.jsonnet
@@ -0,0 +1 @@
+std.decodeUTF8(std.encodeUTF8('foo bar ') + [255] + std.encodeUTF8(' baz'), lossy = false)
tests/golden/issue187.rev.jsonnet.goldendiffbeforeafterboth--- /dev/null
+++ b/tests/golden/issue187.rev.jsonnet.golden
@@ -0,0 +1,2 @@
+runtime error: bad utf8
+ issue187.rev.jsonnet:1:1-92: function <builtin_decode_utf8> call
\ No newline at end of file
tests/suite/std_param_names.jsonnetdiffbeforeafterboth--- a/tests/suite/std_param_names.jsonnet
+++ b/tests/suite/std_param_names.jsonnet
@@ -129,7 +129,7 @@
parseJson: ['str'],
parseYaml: ['str'],
encodeUTF8: ['str'],
- decodeUTF8: ['arr'],
+ decodeUTF8: ['arr', 'lossy'],
sum: ['arr'],
avg: ['arr', 'onEmpty'],