mirror of
https://github.com/gorules/zen.git
synced 2026-10-05 00:02:26 +00:00
feat: json functions
This commit is contained in:
@@ -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)))
|
||||
|
||||
@@ -76,6 +76,7 @@ pub enum TypeConversionKind {
|
||||
Number,
|
||||
String,
|
||||
Bool,
|
||||
Json,
|
||||
}
|
||||
|
||||
/// Metadata for TypeCheck Opcode
|
||||
|
||||
@@ -41,7 +41,7 @@ pub enum BuiltInFunction {
|
||||
String,
|
||||
Number,
|
||||
Bool,
|
||||
// Json,
|
||||
Json,
|
||||
|
||||
// Date + time
|
||||
Date,
|
||||
@@ -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
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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};
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
|
Can't render this file because it contains an unexpected character in line 14 and column 13.
|
Reference in New Issue
Block a user