git.delta.rocks / jrsonnet / refs/commits / 6e67554e1625

difftreelog

fix std.decodeUTF8 should be lossy by default

uuxvswqwYaroslav Bolyukin2026-02-08parent: #5f96572.patch.diff
in: master

6 files changed

modifiedcrates/jrsonnet-stdlib/src/encoding.rsdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/encoding.rs
1use base64::{engine::general_purpose::STANDARD, Engine};2use jrsonnet_evaluator::{3	function::builtin,4	runtime_error,5	typed::{Either, Either2},6	IBytes, IStr, Result,7};89#[builtin]10pub fn builtin_encode_utf8(str: IStr) -> IBytes {11	str.cast_bytes()12}1314#[builtin]15pub fn builtin_decode_utf8(arr: IBytes) -> Result<IStr> {16	arr.cast_str().ok_or_else(|| runtime_error!("bad utf8"))17}1819#[builtin]20pub fn builtin_base64(input: Either![IStr, IBytes]) -> String {21	use Either2::*;22	match input {23		A(l) => STANDARD.encode(l.as_bytes()),24		B(a) => STANDARD.encode(a.as_slice()),25	}26}2728#[builtin]29pub fn builtin_base64_decode_bytes(str: IStr) -> Result<IBytes> {30	Ok(STANDARD31		.decode(str.as_bytes())32		.map_err(|e| runtime_error!("invalid base64: {e}"))?33		.as_slice()34		.into())35}3637#[builtin]38pub fn builtin_base64_decode(str: IStr) -> Result<String> {39	let bytes = STANDARD40		.decode(str.as_bytes())41		.map_err(|e| runtime_error!("invalid base64: {e}"))?;42	String::from_utf8(bytes).map_err(|_| runtime_error!("bad utf8"))43}
addedtests/golden/issue187.jsonnetdiffbeforeafterboth
--- /dev/null
+++ b/tests/golden/issue187.jsonnet
@@ -0,0 +1 @@
+std.decodeUTF8(std.encodeUTF8('foo bar ') + [255] + std.encodeUTF8(' baz'))
addedtests/golden/issue187.jsonnet.goldendiffbeforeafterboth
--- /dev/null
+++ b/tests/golden/issue187.jsonnet.golden
@@ -0,0 +1 @@
+"foo bar � baz"
\ No newline at end of file
addedtests/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)
addedtests/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
modifiedtests/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'],