git.delta.rocks / jrsonnet / refs/commits / 409a660d0753

difftreelog

feat(evaluator) serde_json integration

Lach2020-08-15parent: #d445938.patch.diff
in: master

5 files changed

modifiedCargo.lockdiffbeforeafterboth
before · Cargo.lock
54 packageslockfile v1
after · Cargo.lock
57 packageslockfile v1
modifiedcrates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -12,6 +12,8 @@
 default = ["serialized-stdlib", "faster", "explaining-traces"]
 # Serializes standard library AST instead of parsing them every run
 serialized-stdlib = ["serde", "bincode", "jrsonnet-parser/deserialize"]
+# Allow to convert Val into serde_json::Value and backwards
+serde-json = ["serde", "serde_json"]
 # Same as above, but with generated code instead of serde. Reduces memory usage, but increases binary size and compilation time
 codegenerated-stdlib = []
 # Replace some standard library functions with faster implementations (I.e manifestJsonEx)
@@ -29,19 +31,24 @@
 pathdiff = "0.2.0"
 
 closure = "0.3.0"
-indexmap = "1.5.0"
+indexmap = "1.5.1"
 
 md5 = "0.7.0"
 base64 = "0.12.3"
 
 # Serialized stdlib
 [dependencies.serde]
-version = "1.0.114"
+version = "1.0.115"
 optional = true
 [dependencies.bincode]
 version = "1.3.1"
 optional = true
 
+# Serde json
+[dependencies.serde_json]
+version = "1.0.57"
+optional = true
+
 # Explaining traces
 [dependencies.annotate-snippets]
 version = "0.9.0"
@@ -51,5 +58,5 @@
 jrsonnet-parser = { path = "../jrsonnet-parser", features = ["dump", "serialize", "deserialize"], version = "0.3.0" }
 jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.3.0" }
 structdump = "0.1.2"
-serde = "1.0.114"
+serde = "1.0.115"
 bincode = "1.3.1"
addedcrates/jrsonnet-evaluator/src/integrations/mod.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-evaluator/src/integrations/mod.rs
@@ -0,0 +1,2 @@
+#[cfg(feature = "serde-json")]
+pub mod serde;
addedcrates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -0,0 +1,77 @@
+use crate::{
+	error::{Error::*, LocError, Result},
+	throw, LazyBinding, LazyVal, ObjMember, ObjValue, Val,
+};
+use jrsonnet_parser::Visibility;
+use serde_json::{Map, Number, Value};
+use std::{
+	collections::HashMap,
+	convert::{TryFrom, TryInto},
+	rc::Rc,
+};
+
+impl TryFrom<&Val> for Value {
+	type Error = LocError;
+	fn try_from(v: &Val) -> Result<Self> {
+		Ok(match v {
+			Val::Bool(b) => Value::Bool(*b),
+			Val::Null => Value::Null,
+			Val::Str(s) => Value::String((&s as &str).into()),
+			Val::Num(n) => Value::Number(Number::from_f64(*n).expect("to json number")),
+			Val::Lazy(v) => (&v.evaluate()?).try_into()?,
+			Val::Arr(a) => {
+				let mut out = Vec::with_capacity(a.len());
+				for item in a.iter() {
+					out.push(item.try_into()?);
+				}
+				Value::Array(out)
+			}
+			Val::Obj(o) => {
+				let mut out = Map::new();
+				for key in o.visible_fields() {
+					out.insert(
+						(&key as &str).into(),
+						(&o.get(key)?.expect("field exists")).try_into()?,
+					);
+				}
+				Value::Object(out)
+			}
+			Val::Func(_) | Val::Intristic(_, _) => {
+				throw!(RuntimeError("tried to manifest function".into()))
+			}
+		})
+	}
+}
+
+impl From<&Value> for Val {
+	fn from(v: &Value) -> Self {
+		match v {
+			Value::Null => Val::Null,
+			Value::Bool(v) => Val::Bool(*v),
+			Value::Number(n) => Val::Num(n.as_f64().expect("as f64")),
+			Value::String(s) => Val::Str((s as &str).into()),
+			Value::Array(a) => {
+				let mut out = Vec::with_capacity(a.len());
+				for v in a {
+					out.push(v.into());
+				}
+				Val::Arr(Rc::new(out))
+			}
+			Value::Object(o) => {
+				let mut entries = HashMap::with_capacity(o.len());
+				for (k, v) in o {
+					entries.insert(
+						(k as &str).into(),
+						ObjMember {
+							add: false,
+							visibility: Visibility::Normal,
+							invoke: LazyBinding::Bound(LazyVal::new_resolved(v.into())),
+							location: None,
+						},
+					);
+				}
+				Val::Obj(ObjValue::new(None, Rc::new(entries)))
+			}
+		}
+	}
+}
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -8,6 +8,7 @@
 mod evaluate;
 mod function;
 mod import;
+mod integrations;
 mod map;
 mod obj;
 pub mod trace;