Skip to content

Commit

Permalink
chore: use config instead of config_module in transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed May 31, 2024
1 parent 27ea39a commit 3363240
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 61 deletions.
4 changes: 2 additions & 2 deletions src/core/config/config_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::core::macros::MergeRight;
use crate::core::merge_right::MergeRight;
use crate::core::proto_reader::ProtoMetadata;
use crate::core::rest::{EndpointSet, Unchecked};
use crate::core::valid::Valid;
use crate::core::valid::{Valid, Validator};

/// A wrapper on top of Config that contains all the resolved extensions and
/// computed values.
Expand Down Expand Up @@ -114,6 +114,6 @@ impl From<Config> for ConfigModule {

impl ConfigModule {
pub fn transform<T: Transform>(self, transformer: T) -> Valid<Self, String> {
transformer.transform(self)
transformer.transform(self.config).map(ConfigModule::from)
}
}
95 changes: 47 additions & 48 deletions src/core/config/transformer/ambiguous_type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use super::Transform;
use crate::core::config::ConfigModule;
use crate::core::config::Config;
use crate::core::valid::{Valid, Validator};

/// Resolves the ambiguous types by renaming the input and
Expand Down Expand Up @@ -56,76 +56,75 @@ fn insert_resolution(
}

impl Transform for AmbiguousType {
fn transform(&self, mut c: ConfigModule) -> Valid<ConfigModule, String> {
Valid::from_iter(
c.input_types.intersection(&c.output_types),
|current_name| {
// Iterate over intersection of input and output types
let resolution = (self.resolver)(current_name);

if !resolution.is_unique() {
Valid::fail(format!(
"Unable to auto resolve Input: {} and Output: {} are same",
resolution.input, resolution.output,
))
.trace(current_name)
} else {
let mut resolution_map = HashMap::new();
resolution_map = insert_resolution(resolution_map, current_name, resolution);
if let Some(ty) = c.config.types.get(current_name) {
for field in ty.fields.values() {
for args in field.args.values() {
// if arg is of output type then it should be changed to that of
// newly created input type.
if c.output_types.contains(&args.type_of)
&& !resolution_map.contains_key(&args.type_of)
{
let resolution = (self.resolver)(args.type_of.as_str());
resolution_map = insert_resolution(
resolution_map,
args.type_of.as_str(),
resolution,
);
}
fn transform(&self, mut config: Config) -> Valid<Config, String> {
let mut input_types = config.input_types();
let mut output_types = config.output_types();
Valid::from_iter(input_types.intersection(&output_types), |current_name| {
// Iterate over intersection of input and output types
let resolution = (self.resolver)(current_name);

if !resolution.is_unique() {
Valid::fail(format!(
"Unable to auto resolve Input: {} and Output: {} are same",
resolution.input, resolution.output,
))
.trace(current_name)
} else {
let mut resolution_map = HashMap::new();
resolution_map = insert_resolution(resolution_map, current_name, resolution);
if let Some(ty) = config.types.get(current_name) {
for field in ty.fields.values() {
for args in field.args.values() {
// if arg is of output type then it should be changed to that of
// newly created input type.
if output_types.contains(&args.type_of)
&& !resolution_map.contains_key(&args.type_of)
{
let resolution = (self.resolver)(args.type_of.as_str());
resolution_map = insert_resolution(
resolution_map,
args.type_of.as_str(),
resolution,
);
}
}
}
Valid::succeed(resolution_map)
}
},
)
Valid::succeed(resolution_map)
}
})
.map(|v| v.into_iter().flatten().collect::<HashMap<_, _>>())
.map(|resolution_map| {
// insert newly created types to the config.
for (current_name, resolution) in &resolution_map {
let input_name = &resolution.input;
let output_name = &resolution.output;

let og_ty = c.config.types.get(current_name).cloned();
let og_ty = config.types.get(current_name).cloned();

// remove old types
c.config.types.remove(current_name);
c.input_types.remove(current_name);
c.output_types.remove(current_name);
config.types.remove(current_name);
input_types.remove(current_name);
output_types.remove(current_name);

// add new types
if let Some(og_ty) = og_ty {
c.config.types.insert(input_name.clone(), og_ty.clone());
c.input_types.insert(input_name.clone());
config.types.insert(input_name.clone(), og_ty.clone());
input_types.insert(input_name.clone());

c.config.types.insert(output_name.clone(), og_ty);
c.output_types.insert(output_name.clone());
config.types.insert(output_name.clone(), og_ty);
output_types.insert(output_name.clone());
}
}
let keys = c.config.types.keys().cloned().collect::<Vec<_>>();
let keys = config.types.keys().cloned().collect::<Vec<_>>();

for k in keys {
if let Some(ty) = c.config.types.get_mut(&k) {
if let Some(ty) = config.types.get_mut(&k) {
for field in ty.fields.values_mut() {
if let Some(resolution) = resolution_map.get(&field.type_of) {
if c.output_types.contains(&k) {
if output_types.contains(&k) {
field.type_of.clone_from(&resolution.output);
} else if c.input_types.contains(&k) {
} else if input_types.contains(&k) {
field.type_of.clone_from(&resolution.input);
}
}
Expand All @@ -137,7 +136,7 @@ impl Transform for AmbiguousType {
}
}
}
c
config
})
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/config/transformer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
mod ambiguous_type;
pub use ambiguous_type::{AmbiguousType, Resolution};

use super::ConfigModule;
use super::Config;
use crate::core::valid::{Valid, Validator};

/// A configuration transformer that allows us to perform various
/// transformations on the configuration before it's further processed for
/// blueprint creation.
pub trait Transform {
fn transform(&self, value: ConfigModule) -> Valid<ConfigModule, String>;
fn transform(&self, value: Config) -> Valid<Config, String>;
}

/// A suite of common operators that are available for all transformers.
Expand All @@ -29,7 +29,7 @@ where
pub struct Pipe<A, B>(A, B);

impl<A: Transform, B: Transform> Transform for Pipe<A, B> {
fn transform(&self, value: ConfigModule) -> Valid<ConfigModule, String> {
fn transform(&self, value: Config) -> Valid<Config, String> {
self.0.transform(value).and_then(|v| self.1.transform(v))
}
}
Expand All @@ -38,7 +38,7 @@ impl<A: Transform, B: Transform> Transform for Pipe<A, B> {
pub struct Empty;

impl Transform for Empty {
fn transform(&self, value: ConfigModule) -> Valid<ConfigModule, String> {
fn transform(&self, value: Config) -> Valid<Config, String> {
Valid::succeed(value)
}
}
Expand Down
11 changes: 4 additions & 7 deletions src/core/generator/from_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,13 @@ mod test {
use tailcall_fixtures::protobuf;

use crate::core::config::transformer::{AmbiguousType, Transform};
use crate::core::config::{Config, ConfigModule};
use crate::core::config::Config;
use crate::core::valid::Validator;

fn from_proto_resolved(files: &[FileDescriptorSet], query: &str) -> Result<Config> {
let config = AmbiguousType::default()
.transform(ConfigModule::from(super::from_proto(files, query)?))
.to_result()?
.config;
.transform(super::from_proto(files, query)?)
.to_result()?;
Ok(config)
}

Expand Down Expand Up @@ -375,9 +374,7 @@ mod test {
fn test_movies() -> Result<()> {
let set = compile_protobuf(&[protobuf::MOVIES])?;
let config = from_proto_resolved(&[set], "Query")?;
let config_module = AmbiguousType::default()
.transform(ConfigModule::from(config))
.to_result()?;
let config_module = AmbiguousType::default().transform(config).to_result()?;
let config = config_module.to_sdl();
insta::assert_snapshot!(config);

Expand Down

0 comments on commit 3363240

Please sign in to comment.