Skip to content

Commit

Permalink
refactor: make DynamicValue polymorphic (#2262)
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Jun 23, 2024
1 parent e38aa3d commit f1d8feb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
18 changes: 9 additions & 9 deletions src/core/blueprint/dynamic_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ use serde_json::Value;
use crate::core::mustache::Mustache;

#[derive(Debug, Clone)]
pub enum DynamicValue {
Value(ConstValue),
pub enum DynamicValue<A> {
Value(A),
Mustache(Mustache),
Object(IndexMap<Name, DynamicValue>),
Array(Vec<DynamicValue>),
Object(IndexMap<Name, DynamicValue<A>>),
Array(Vec<DynamicValue<A>>),
}

impl TryFrom<&DynamicValue> for ConstValue {
impl TryFrom<&DynamicValue<ConstValue>> for ConstValue {
type Error = anyhow::Error;

fn try_from(value: &DynamicValue) -> Result<Self, Self::Error> {
fn try_from(value: &DynamicValue<ConstValue>) -> Result<Self, Self::Error> {
match value {
DynamicValue::Value(v) => Ok(v.to_owned()),
DynamicValue::Mustache(_) => Err(anyhow::anyhow!(
Expand All @@ -37,7 +37,7 @@ impl TryFrom<&DynamicValue> for ConstValue {
}
}

impl DynamicValue {
impl<A> DynamicValue<A> {
// Helper method to determine if the value is constant (non-mustache).
pub fn is_const(&self) -> bool {
match self {
Expand All @@ -49,7 +49,7 @@ impl DynamicValue {
}
}

impl TryFrom<&Value> for DynamicValue {
impl TryFrom<&Value> for DynamicValue<ConstValue> {
type Error = anyhow::Error;

fn try_from(value: &Value) -> Result<Self, Self::Error> {
Expand All @@ -63,7 +63,7 @@ impl TryFrom<&Value> for DynamicValue {
Ok(DynamicValue::Object(out))
}
Value::Array(arr) => {
let out: Result<Vec<DynamicValue>, Self::Error> =
let out: Result<Vec<DynamicValue<ConstValue>>, Self::Error> =
arr.iter().map(DynamicValue::try_from).collect();
Ok(DynamicValue::Array(out?))
}
Expand Down
1 change: 0 additions & 1 deletion src/core/ir/jit/synth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ mod tests {

#[test]
fn test_json_placeholder() {
// FIXME: doesn't work when userId is queried
let synth = JsonPlaceholder::init("{ posts { id title userId user { id name } } }");
let val = synth.synthesize();
insta::assert_snapshot!(serde_json::to_string_pretty(&val).unwrap())
Expand Down
3 changes: 2 additions & 1 deletion src/core/ir/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::fmt::Debug;
use std::num::NonZeroU64;

use async_graphql::Value;
use strum_macros::Display;

use super::{EvalContext, ResolverContextLike};
Expand All @@ -14,7 +15,7 @@ use crate::core::{grpc, http};
#[derive(Clone, Debug, Display)]
pub enum IR {
Context(Context),
Dynamic(DynamicValue),
Dynamic(DynamicValue<Value>),
#[strum(to_string = "{0}")]
IO(IO),
Cache(Cache),
Expand Down
2 changes: 1 addition & 1 deletion src/core/serde_value_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub trait ValueExt {
fn render_value(&self, ctx: &impl PathString) -> GraphQLValue;
}

impl ValueExt for DynamicValue {
impl ValueExt for DynamicValue<async_graphql::Value> {
fn render_value<'a>(&self, ctx: &'a impl PathString) -> GraphQLValue {
match self {
DynamicValue::Value(value) => value.to_owned(),
Expand Down

1 comment on commit f1d8feb

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 6.53ms 3.24ms 95.96ms 76.19%
Req/Sec 3.89k 373.03 14.15k 95.67%

465108 requests in 30.10s, 2.33GB read

Requests/sec: 15452.00

Transfer/sec: 79.31MB

Please sign in to comment.