Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(stdlib): remove deprecated ellipsis argument from truncate #1188

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions lib/tests/tests/diagnostics/truncate_deprecated_argument.vrl

This file was deleted.

20 changes: 7 additions & 13 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use crate::compiler::{
},
parser::ast::RootExpr,
program::ProgramInfo,
CompileConfig, DeprecationWarning, Function, Program, TypeDef,
CompileConfig, Function, Program, TypeDef,
};
use crate::diagnostic::{DiagnosticList, DiagnosticMessage, Note};
use crate::diagnostic::{DiagnosticList, DiagnosticMessage};
use crate::parser::ast::{self, Node, QueryTarget};
use crate::path::PathPrefix;
use crate::path::{OwnedTargetPath, OwnedValuePath};
Expand Down Expand Up @@ -621,17 +621,11 @@ impl<'a> Compiler<'a> {
}

#[allow(clippy::unused_self)]
pub(crate) fn check_function_deprecations(&mut self, func: &FunctionCall, args: &ArgumentList) {
if func.ident == "truncate" && args.optional("ellipsis").is_some() {
self.diagnostics.push(Box::new(
DeprecationWarning::new("the `ellipsis` argument", "0.7.0")
.with_span(func.span)
.with_notes(Note::solution(
"the `truncate` function now supports a `suffix` argument.",
vec!["The `suffix` argument can be used for appending arbitrary strings."],
)),
));
}
pub(crate) fn check_function_deprecations(
&mut self,
_func: &FunctionCall,
_args: &ArgumentList,
) {
}

fn compile_function_call(
Expand Down
28 changes: 8 additions & 20 deletions src/stdlib/truncate.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::compiler::prelude::*;

fn truncate(value: Value, limit: Value, ellipsis: Value, suffix: Value) -> Resolved {
fn truncate(value: Value, limit: Value, suffix: Value) -> Resolved {
let mut value = value.try_bytes_utf8_lossy()?.into_owned();
let limit = limit.try_integer()?;
let limit = if limit < 0 { 0 } else { limit as usize };
let ellipsis = ellipsis.try_boolean()?;
let suffix = suffix.try_bytes_utf8_lossy()?.to_string();
let pos = if let Some((pos, chr)) = value.char_indices().take(limit).last() {
// char_indices gives us the starting position of the character at limit,
Expand All @@ -16,9 +15,7 @@ fn truncate(value: Value, limit: Value, ellipsis: Value, suffix: Value) -> Resol
};
if value.len() > pos {
value.truncate(pos);
if ellipsis {
value.push_str("...");
} else if !suffix.is_empty() {
if !suffix.is_empty() {
value.push_str(&suffix);
}
}
Expand All @@ -45,11 +42,6 @@ impl Function for Truncate {
kind: kind::INTEGER,
required: true,
},
Parameter {
keyword: "ellipsis",
kind: kind::BOOLEAN,
required: false,
},
Parameter {
keyword: "suffix",
kind: kind::BYTES,
Expand Down Expand Up @@ -80,19 +72,17 @@ impl Function for Truncate {

fn compile(
&self,
_state: &state::TypeState,
_state: &TypeState,
_ctx: &mut FunctionCompileContext,
arguments: ArgumentList,
) -> Compiled {
let value = arguments.required("value");
let limit = arguments.required("limit");
let ellipsis = arguments.optional("ellipsis").unwrap_or(expr!(false));
let suffix = arguments.optional("suffix").unwrap_or(expr!(""));

Ok(TruncateFn {
value,
limit,
ellipsis,
suffix,
}
.as_expr())
Expand All @@ -103,18 +93,16 @@ impl Function for Truncate {
struct TruncateFn {
value: Box<dyn Expression>,
limit: Box<dyn Expression>,
ellipsis: Box<dyn Expression>,
suffix: Box<dyn Expression>,
}

impl FunctionExpression for TruncateFn {
fn resolve(&self, ctx: &mut Context) -> Resolved {
let value = self.value.resolve(ctx)?;
let limit = self.limit.resolve(ctx)?;
let ellipsis = self.ellipsis.resolve(ctx)?;
let suffix = self.suffix.resolve(ctx)?;

truncate(value, limit, ellipsis, suffix)
truncate(value, limit, suffix)
}

fn type_def(&self, _: &state::TypeState) -> TypeDef {
Expand All @@ -140,7 +128,7 @@ mod tests {
ellipsis {
args: func_args![value: "Super",
limit: 0,
ellipsis: true
suffix: "..."
],
want: Ok("..."),
tdef: TypeDef::bytes().infallible(),
Expand All @@ -157,7 +145,7 @@ mod tests {
exact {
args: func_args![value: "Super",
limit: 5,
ellipsis: true
suffix: "."
],
want: Ok("Super"),
tdef: TypeDef::bytes().infallible(),
Expand All @@ -174,7 +162,7 @@ mod tests {
big_ellipsis {
args: func_args![value: "Supercalifragilisticexpialidocious",
limit: 5,
ellipsis: true,
suffix: "..."
],
want: Ok("Super..."),
tdef: TypeDef::bytes().infallible(),
Expand All @@ -183,7 +171,7 @@ mod tests {
unicode {
args: func_args![value: "♔♕♖♗♘♙♚♛♜♝♞♟",
limit: 6,
ellipsis: true
suffix: "..."
],
want: Ok("♔♕♖♗♘♙..."),
tdef: TypeDef::bytes().infallible(),
Expand Down
Loading