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

refactor: make DynamicValue polymorphic #2262

Merged
merged 1 commit into from
Jun 23, 2024
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
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
Loading