From ce9430d1ac277db976858e946872e5332228939d Mon Sep 17 00:00:00 2001 From: Finn Bear Date: Thu, 13 Jul 2023 01:06:12 -0700 Subject: [PATCH] Bugfix #2175 - Allow escape characters in scripts (#2253) --- lib/src/sql/script.rs | 16 ++++++---------- lib/tests/script.rs | 8 +++++--- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/lib/src/sql/script.rs b/lib/src/sql/script.rs index 48e0bff5..59f39619 100644 --- a/lib/src/sql/script.rs +++ b/lib/src/sql/script.rs @@ -2,14 +2,10 @@ use crate::sql::comment::{block, slash}; use crate::sql::error::IResult; use crate::sql::strand::no_nul_bytes; use nom::branch::alt; -use nom::bytes::complete::escaped; -use nom::bytes::complete::is_not; -use nom::bytes::complete::tag; -use nom::character::complete::char; -use nom::character::complete::multispace0; +use nom::bytes::complete::{escaped, is_not, tag}; +use nom::character::complete::{anychar, char, multispace0}; use nom::combinator::recognize; -use nom::multi::many0; -use nom::multi::many1; +use nom::multi::{many0, many1}; use nom::sequence::delimited; use serde::{Deserialize, Serialize}; use std::fmt::{self, Display, Formatter}; @@ -101,19 +97,19 @@ fn script_string(i: &str) -> IResult<&str, &str> { }, |i| { let (i, _) = char(SINGLE)(i)?; - let (i, v) = escaped(is_not(SINGLE_ESC_NUL), '\\', char(SINGLE))(i)?; + let (i, v) = escaped(is_not(SINGLE_ESC_NUL), '\\', anychar)(i)?; let (i, _) = char(SINGLE)(i)?; Ok((i, v)) }, |i| { let (i, _) = char(DOUBLE)(i)?; - let (i, v) = escaped(is_not(DOUBLE_ESC_NUL), '\\', char(DOUBLE))(i)?; + let (i, v) = escaped(is_not(DOUBLE_ESC_NUL), '\\', anychar)(i)?; let (i, _) = char(DOUBLE)(i)?; Ok((i, v)) }, |i| { let (i, _) = char(BACKTICK)(i)?; - let (i, v) = escaped(is_not(BACKTICK_ESC_NUL), '\\', char(BACKTICK))(i)?; + let (i, v) = escaped(is_not(BACKTICK_ESC_NUL), '\\', anychar)(i)?; let (i, _) = char(BACKTICK)(i)?; Ok((i, v)) }, diff --git a/lib/tests/script.rs b/lib/tests/script.rs index be175fe5..cd297df9 100644 --- a/lib/tests/script.rs +++ b/lib/tests/script.rs @@ -39,18 +39,20 @@ async fn script_function_error() -> Result<(), Error> { #[tokio::test] async fn script_function_simple() -> Result<(), Error> { - let sql = " + let sql = r#" CREATE person:test SET scores = function() { return [6.6, 8.4, 7.3].map(v => v * 10); + }, bio = function() { + return "Line 1\nLine 2"; }; - "; + "#; let dbs = Datastore::new("memory").await?; let ses = Session::for_kv().with_ns("test").with_db("test"); let res = &mut dbs.execute(sql, &ses, None).await?; assert_eq!(res.len(), 1); // let tmp = res.remove(0).result?; - let val = Value::parse("[{ id: person:test, scores: [66, 84, 73] }]"); + let val = Value::parse(r#"[{ bio: "Line 1\nLine 2", id: person:test, scores: [66, 84, 73] }]"#); assert_eq!(tmp, val); // Ok(())