Skip to content
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions lib/enrichment/src/find_enrichment_table_records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl Expression for FindEnrichmentTableRecordsFn {
.collect::<std::result::Result<Vec<_>, _>>(),
value => Err(value::Error::Expected {
got: value.kind(),
expected: Kind::Array,
expected: Kind::array(Collection::any()),
}),
})
.transpose()?;
Expand Down Expand Up @@ -164,9 +164,7 @@ impl Expression for FindEnrichmentTableRecordsFn {
}

fn type_def(&self, _: &state::Compiler) -> TypeDef {
TypeDef::new()
.fallible()
.array_mapped::<(), Kind>(map! { (): Kind::Object })
TypeDef::array(Collection::from_unknown(Kind::object(Collection::any()))).fallible()
}
}

Expand Down
6 changes: 2 additions & 4 deletions lib/enrichment/src/get_enrichment_table_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl Expression for GetEnrichmentTableRecordFn {
.collect::<std::result::Result<Vec<_>, _>>(),
value => Err(value::Error::Expected {
got: value.kind(),
expected: Kind::Array,
expected: Kind::array(Collection::any()),
}),
})
.transpose()?;
Expand Down Expand Up @@ -156,9 +156,7 @@ impl Expression for GetEnrichmentTableRecordFn {
}

fn type_def(&self, _: &state::Compiler) -> TypeDef {
TypeDef::new()
.fallible()
.add_object::<(), Kind>(map! { (): Kind::all() })
TypeDef::object(Collection::any()).fallible()
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/lookup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
publish = false

[dependencies]
serde = { version = "1.0.136", default-features = false, features = ["derive"] }
serde = { version = "1.0.136", default-features = false, features = ["derive", "alloc"] }
tracing = { version = "0.1", default-features = false, features = ["attributes"] }
snafu = { version = "0.7", default-features = false }
regex = { version = "1.5.4", default-features = false, features = ["std", "perf"] }
Expand Down
9 changes: 9 additions & 0 deletions lib/value/src/kind/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ impl<T: Ord> Collection<T> {
}
}

/// Create a collection kind of which there are no known and no unknown kinds.
#[must_use]
pub fn empty() -> Self {
Self {
known: BTreeMap::default(),
unknown: None,
}
}

/// Create a collection kind of which the encapsulated values can be any kind.
#[must_use]
pub fn any() -> Self {
Expand Down
2 changes: 1 addition & 1 deletion lib/vector-vrl-functions/src/get_metadata_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ impl Expression for GetMetadataFieldFn {
}

fn type_def(&self, _: &state::Compiler) -> TypeDef {
TypeDef::new().infallible().bytes().add_null()
TypeDef::bytes().add_null().infallible()
}
}
2 changes: 1 addition & 1 deletion lib/vector-vrl-functions/src/remove_metadata_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ impl Expression for RemoveMetadataFieldFn {
}

fn type_def(&self, _: &state::Compiler) -> TypeDef {
TypeDef::new().infallible().null()
TypeDef::null().infallible()
}
}
2 changes: 1 addition & 1 deletion lib/vector-vrl-functions/src/set_metadata_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ impl Expression for SetMetadataFieldFn {
}

fn type_def(&self, _: &state::Compiler) -> TypeDef {
TypeDef::new().infallible().null()
TypeDef::null().infallible()
}
}
1 change: 1 addition & 0 deletions lib/vrl/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ diagnostic = { package = "vrl-diagnostic", path = "../diagnostic" }
parser = { package = "vrl-parser", path = "../parser" }
lookup = { path = "../../lookup" }
vector_common = { path = "../../vector-common", default-features = false, features = ["conversion"] }
value = { path = "../../value" }

bitflags = "1"
bytes = "1.1.0"
Expand Down
24 changes: 16 additions & 8 deletions lib/vrl/compiler/benches/kind.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::fmt;

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use vrl_compiler::value::Kind;
use vrl_compiler::value::kind;

struct Parameters {
basis: Kind,
basis: u16,
}

static PARAMETERS: [Parameters; 4] = [
Parameters { basis: Kind::Bytes },
Parameters { basis: Kind::Array },
Parameters { basis: Kind::Regex },
Parameters { basis: Kind::Null },
Parameters { basis: kind::BYTES },
Parameters { basis: kind::ARRAY },
Parameters { basis: kind::REGEX },
Parameters { basis: kind::NULL },
];

impl fmt::Display for Parameters {
Expand All @@ -23,8 +23,16 @@ impl fmt::Display for Parameters {
fn benchmark_kind_display(c: &mut Criterion) {
let mut group = c.benchmark_group("vrl_compiler/value::kind::display");
for param in &PARAMETERS {
group.bench_with_input(BenchmarkId::from_parameter(param), &param, |b, &param| {
b.iter(|| format!("{}", param.basis))
let parameter = vrl_compiler::Parameter {
keyword: "",
kind: param.basis,
required: false,
};

let kind = parameter.kind();

group.bench_with_input(BenchmarkId::from_parameter(param), &kind, |b, kind| {
b.iter(|| format!("{}", kind))
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/vrl/compiler/src/expression/abort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Abort {
})
} else if !type_def.is_bytes() {
Err(Error {
variant: ErrorVariant::NonString(type_def.kind()),
variant: ErrorVariant::NonString(type_def.into()),
expr_span,
})
} else {
Expand Down Expand Up @@ -69,7 +69,7 @@ impl Expression for Abort {
}

fn type_def(&self, _: &State) -> TypeDef {
TypeDef::new().infallible().null()
TypeDef::null().infallible()
}

fn compile_to_vm(&self, vm: &mut crate::vm::Vm) -> Result<(), String> {
Expand Down Expand Up @@ -138,7 +138,7 @@ impl DiagnosticError for Error {
}

fn labels(&self) -> Vec<Label> {
match self.variant {
match &self.variant {
ErrorVariant::FallibleExpr => vec![
Label::primary(
"abort only accepts an infallible expression argument",
Expand Down
46 changes: 27 additions & 19 deletions lib/vrl/compiler/src/expression/array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt, ops::Deref};
use std::{collections::BTreeMap, fmt, ops::Deref};

use crate::{
expression::{Expr, Resolved},
Expand Down Expand Up @@ -53,7 +53,13 @@ impl Expression for Array {
// fallible.
let fallible = type_defs.iter().any(TypeDef::is_fallible);

TypeDef::new().array(type_defs).with_fallibility(fallible)
let collection = type_defs
.into_iter()
.enumerate()
.map(|(index, type_def)| (index.into(), type_def.into()))
.collect::<BTreeMap<_, _>>();

TypeDef::array(collection).with_fallibility(fallible)
}

fn compile_to_vm(&self, vm: &mut crate::vm::Vm) -> Result<(), String> {
Expand Down Expand Up @@ -94,35 +100,37 @@ impl From<Vec<Expr>> for Array {

#[cfg(test)]
mod tests {
use crate::{expr, map, test_type_def, value::Kind, TypeDef};
use super::*;
use crate::{expr, test_type_def, value::Kind, TypeDef};
use value::kind::Collection;

test_type_def![
empty_array {
expr: |_| expr!([]),
want: TypeDef::new().array::<TypeDef>(vec![]),
want: TypeDef::array(Collection::empty()),
}

scalar_array {
expr: |_| expr!([1, "foo", true]),
want: TypeDef::new().array_mapped::<i32, TypeDef>(map! {
0: Kind::Integer,
1: Kind::Bytes,
2: Kind::Boolean,
}),
want: TypeDef::array(BTreeMap::from([
(0.into(), Kind::integer()),
(1.into(), Kind::bytes()),
(2.into(), Kind::boolean()),
])),
}

mixed_array {
expr: |_| expr!([1, [true, "foo"], { "bar": null }]),
want: TypeDef::new().array_mapped::<i32, TypeDef>(map! {
0: Kind::Integer,
1: TypeDef::new().array_mapped::<i32, TypeDef>(map! {
0: Kind::Boolean,
1: Kind::Bytes,
}),
2: TypeDef::new().object::<&str, TypeDef>(map! {
"bar": Kind::Null,
}),
}),
want: TypeDef::array(BTreeMap::from([
(0.into(), Kind::integer()),
(1.into(), Kind::array(BTreeMap::from([
(0.into(), Kind::boolean()),
(1.into(), Kind::bytes()),
]))),
(2.into(), Kind::object(BTreeMap::from([
("bar".into(), Kind::null())
]))),
])),
}
];
}
11 changes: 6 additions & 5 deletions lib/vrl/compiler/src/expression/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
ast::{self, Ident},
Node,
},
value::kind::DefaultValue,
vm::OpCode,
Context, Expression, Span, State, TypeDef, Value,
};
Expand Down Expand Up @@ -108,7 +109,7 @@ impl Assignment {
// "err" target.
let ok = Target::try_from(ok.into_inner())?;
let type_def = type_def.infallible();
let default = type_def.kind().default_value();
let default_value = type_def.default_value();
let value = match &expr {
Expr::Literal(v) => Some(v.to_value()),
_ => None,
Expand All @@ -119,15 +120,15 @@ impl Assignment {
// "err" target is assigned `null` or a string containing the
// error message.
let err = Target::try_from(err.into_inner())?;
let type_def = TypeDef::new().bytes().add_null().infallible();
let type_def = TypeDef::bytes().add_null().infallible();

err.insert_type_def(state, type_def, None);

Variant::Infallible {
ok,
err,
expr: Box::new(expr),
default,
default: default_value,
}
}
};
Expand Down Expand Up @@ -214,7 +215,7 @@ impl Target {
Internal(ident, path) => {
let td = match path {
None => type_def,
Some(path) => type_def.for_path(path.clone()),
Some(path) => type_def.for_path(&path.to_lookup()),
};

let type_def = match state.variable(ident) {
Expand All @@ -230,7 +231,7 @@ impl Target {
External(path) => {
let td = match path {
None => type_def,
Some(path) => type_def.for_path(path.clone()),
Some(path) => type_def.for_path(&path.to_lookup()),
};

let type_def = match state.target() {
Expand Down
2 changes: 1 addition & 1 deletion lib/vrl/compiler/src/expression/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Expression for Block {
let fallible = type_defs.iter().any(TypeDef::is_fallible);

// The last expression determines the resulting value of the block.
let type_def = type_defs.pop().unwrap_or_else(|| TypeDef::new().null());
let type_def = type_defs.pop().unwrap_or_else(TypeDef::null);

type_def.with_fallibility(fallible)
}
Expand Down
Loading