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

feat: json functions #118

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions core/expression/src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,10 @@ impl<'arena, 'bytecode_ref> CompilerInner<'arena, 'bytecode_ref> {
self.compile_argument(kind, arguments, 0)?;
Ok(self.emit(Opcode::TypeConversion(TypeConversionKind::Bool)))
}
BuiltInFunction::Json => {
self.compile_argument(kind, arguments, 0)?;
Ok(self.emit(Opcode::TypeConversion(TypeConversionKind::Json)))
}
BuiltInFunction::IsNumeric => {
self.compile_argument(kind, arguments, 0)?;
Ok(self.emit(Opcode::TypeCheck(TypeCheckKind::Numeric)))
Expand Down
1 change: 1 addition & 0 deletions core/expression/src/compiler/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub enum TypeConversionKind {
Number,
String,
Bool,
Json,
}

/// Metadata for TypeCheck Opcode
Expand Down
4 changes: 2 additions & 2 deletions core/expression/src/parser/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub enum BuiltInFunction {
String,
Number,
Bool,
// Json,
Json,

// Date + time
Date,
Expand Down Expand Up @@ -119,7 +119,7 @@ impl BuiltInFunction {
BuiltInFunction::String => Arity::Single,
BuiltInFunction::Number => Arity::Single,
BuiltInFunction::Bool => Arity::Single,
// BuiltInFunction::Json => Arity::Single,
BuiltInFunction::Json => Arity::Single,
BuiltInFunction::IsNumeric => Arity::Single,

// Closure
Expand Down
1 change: 1 addition & 0 deletions core/expression/src/parser/unary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ impl From<&Node<'_>> for UnaryNodeBehaviour {
BuiltInFunction::String => CompareWithReference(Equal),
BuiltInFunction::Number => CompareWithReference(Equal),
BuiltInFunction::Bool => CompareWithReference(Equal),
BuiltInFunction::Json => CompareWithReference(Equal),
BuiltInFunction::Date => CompareWithReference(Equal),
BuiltInFunction::Time => CompareWithReference(Equal),
BuiltInFunction::Duration => CompareWithReference(Equal),
Expand Down
20 changes: 20 additions & 0 deletions core/expression/src/vm/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use regex_lite::Regex;
use rust_decimal::prelude::ToPrimitive;
use rust_decimal::{Decimal, MathematicalOps};
use rust_decimal_macros::dec;
use serde_json::Value;

use crate::vm::error::VMResult;
use crate::vm::variable::{IntervalObject, Variable};
Expand Down Expand Up @@ -1281,6 +1282,25 @@ impl<'arena, 'parent_ref, 'bytecode_ref> VMInner<'arena, 'parent_ref, 'bytecode_
(TypeConversionKind::Bool, Object(_) | Array(_)) => {
self.bump.alloc(Bool(true))
}
(TypeConversionKind::Json, String(s)) => {
let json_value: Value =
serde_json::from_str(s).map_err(|_| OpcodeErr {
opcode: "TypeConversion".into(),
message: format!("Failed to deserialize JSON value `{}`", s),
})?;

let variable = Variable::from_serde(&json_value, self.bump);
self.bump.alloc(variable)
}
(TypeConversionKind::Json, _) => {
return Err(OpcodeErr {
opcode: "TypeConversion".into(),
message: format!(
"Type {} cannot be converted to json",
var.type_name()
),
})
}
};

self.push_ref(converted_var);
Expand Down
4 changes: 4 additions & 0 deletions core/expression/tests/data/standard.csv
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ number(123.123);;123.123
number(123);;123
number(true);;1
number(false);;0
json('true');;true
json("false");;false
json('{"hello": "world"}');;{hello: 'world'}
json('[1, 2, 3, "test"]');;[1, 2, 3, 'test']

# Type check
isNumeric(123.123);;true
Expand Down
Loading