Skip to content

Commit

Permalink
refactor: Move transform out of config (#2214)
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath authored Jun 16, 2024
1 parent d5c0a22 commit cd95265
Show file tree
Hide file tree
Showing 19 changed files with 131 additions and 110 deletions.
6 changes: 5 additions & 1 deletion src/core/blueprint/blueprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use derive_setters::Setters;
use serde_json::Value;

use super::telemetry::Telemetry;
use super::GlobalTimeout;
use super::{GlobalTimeout, Index};
use crate::core::blueprint::{Server, Upstream};
use crate::core::ir::model::IR;
use crate::core::schema_extension::SchemaExtension;
Expand Down Expand Up @@ -286,4 +286,8 @@ impl Blueprint {
// generation of schema cannot fail.
schema.finish().unwrap()
}

pub fn index(&self) -> Index {
Index::from(self)
}
}
40 changes: 21 additions & 19 deletions src/core/ir/jit/blueprint_index.rs → src/core/blueprint/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::core::blueprint::{
/// A read optimized index of all the fields in the Blueprint. Provide O(1)
/// access to getting any field information.
#[allow(unused)]
pub struct BlueprintIndex {
pub struct Index {
map: HashMap<String, (Definition, HashMap<String, QueryField>)>,
schema: SchemaDefinition,
}
Expand All @@ -30,8 +30,26 @@ impl QueryField {
}
}

impl BlueprintIndex {
pub fn init(blueprint: &Blueprint) -> Self {
impl Index {
#[allow(unused)]
pub fn get_field(&self, type_name: &str, field_name: &str) -> Option<&QueryField> {
self.map
.get(type_name)
.and_then(|(_, fields_map)| fields_map.get(field_name))
}

pub fn get_query(&self) -> &String {
&self.schema.query
}

#[allow(unused)]
pub fn get_mutation(&self) -> Option<&str> {
self.schema.mutation.as_deref()
}
}

impl From<&Blueprint> for Index {
fn from(blueprint: &Blueprint) -> Self {
let mut map = HashMap::new();

for definition in blueprint.definitions.iter() {
Expand Down Expand Up @@ -121,20 +139,4 @@ impl BlueprintIndex {

Self { map, schema: blueprint.schema.to_owned() }
}

#[allow(unused)]
pub fn get_field(&self, type_name: &str, field_name: &str) -> Option<&QueryField> {
self.map
.get(type_name)
.and_then(|(_, fields_map)| fields_map.get(field_name))
}

pub fn get_query(&self) -> &String {
&self.schema.query
}

#[allow(unused)]
pub fn get_mutation(&self) -> Option<&str> {
self.schema.mutation.as_deref()
}
}
2 changes: 2 additions & 0 deletions src/core/blueprint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod cors;
mod definitions;
mod dynamic_value;
mod from_config;
mod index;
mod into_schema;
mod links;
mod mustache;
Expand All @@ -21,6 +22,7 @@ pub use cors::*;
pub use definitions::*;
pub use dynamic_value::*;
pub use from_config::*;
pub use index::*;
pub use links::*;
pub use operators::*;
pub use schema::*;
Expand Down
7 changes: 5 additions & 2 deletions src/core/config/config_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use jsonwebtoken::jwk::JwkSet;
use prost_reflect::prost_types::{FileDescriptorProto, FileDescriptorSet};
use rustls_pki_types::{CertificateDer, PrivateKeyDer};

use super::transformer::Transform;
use crate::core::config::Config;
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::transform::Transform;
use crate::core::valid::{Valid, Validator};

/// A wrapper on top of Config that contains all the resolved extensions and
Expand Down Expand Up @@ -113,7 +113,10 @@ impl From<Config> for ConfigModule {
}

impl ConfigModule {
pub fn transform<T: Transform>(self, transformer: T) -> Valid<Self, String> {
pub fn transform<T: Transform<Value = Config, Error = String>>(
self,
transformer: T,
) -> Valid<Self, String> {
transformer.transform(self.config).map(ConfigModule::from)
}
}
6 changes: 4 additions & 2 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::Config;
use crate::core::transform::Transform;
use crate::core::valid::{Valid, Validator};

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

impl Transform for AmbiguousType {
fn transform(&self, mut config: Config) -> Valid<Config, String> {
type Value = Config;
type Error = String;
fn transform(&self, mut config: Self::Value) -> Valid<Self::Value, Self::Error> {
let mut input_types = config.input_types();
let mut output_types = config.output_types();
Valid::from_iter(input_types.intersection(&output_types), |current_name| {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::HashSet;

use super::max_value_map::MaxValueMap;
use crate::core::config::transformer::Transform;
use crate::core::config::Config;
use crate::core::transform::Transform;
use crate::core::valid::Valid;

struct UrlTypeMapping {
Expand Down Expand Up @@ -94,6 +94,8 @@ impl ConsolidateURL {
}

impl Transform for ConsolidateURL {
type Value = Config;
type Error = String;
fn transform(&self, config: Config) -> Valid<Config, String> {
let config = self.generate_base_url(config);
Valid::succeed(config)
Expand All @@ -107,8 +109,8 @@ mod test {
use tailcall_fixtures::configs;

use super::*;
use crate::core::config::transformer::Transform;
use crate::core::config::Config;
use crate::core::transform::Transform;
use crate::core::valid::Validator;

fn read_fixture(path: &str) -> String {
Expand Down
56 changes: 0 additions & 56 deletions src/core/config/transformer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,3 @@ pub use consolidate_url::ConsolidateURL;
pub use remove_unused::RemoveUnused;
pub use type_merger::TypeMerger;
pub use type_name_generator::TypeNameGenerator;

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: Config) -> Valid<Config, String>;
}

/// A suite of common operators that are available for all transformers.
pub trait TransformerOps: Sized + Transform {
fn pipe<B: Transform>(self, other: B) -> Pipe<Self, B>;
}

impl<A> TransformerOps for A
where
A: Transform,
{
fn pipe<B: Transform>(self, other: B) -> Pipe<A, B> {
Pipe(self, other)
}
}

/// Represents a composition of two transformers.
pub struct Pipe<A, B>(A, B);

impl<A: Transform, B: Transform> Transform for Pipe<A, B> {
fn transform(&self, value: Config) -> Valid<Config, String> {
self.0.transform(value).and_then(|v| self.1.transform(v))
}
}

/// Represents an empty transformer.
pub struct Empty;

impl Transform for Empty {
fn transform(&self, value: Config) -> Valid<Config, String> {
Valid::succeed(value)
}
}

/// A helper struct that allows us to easily create and compose transformers.
pub struct Transformer;
impl Transformer {
/// Creates an empty transformer
pub fn empty() -> Empty {
Empty
}

/// Combine two transformers into a single transformer.
pub fn pipe<A: Transform, B: Transform>(a: A, b: B) -> Pipe<A, B> {
Pipe(a, b)
}
}
6 changes: 4 additions & 2 deletions src/core/config/transformer/remove_unused.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::Transform;
use crate::core::config::Config;
use crate::core::transform::Transform;
use crate::core::valid::Valid;

/// `RemoveUnused` is responsible for removing unused types from a
Expand All @@ -10,7 +10,9 @@ use crate::core::valid::Valid;
pub struct RemoveUnused;

impl Transform for RemoveUnused {
fn transform(&self, mut config: Config) -> Valid<Config, String> {
type Value = Config;
type Error = String;
fn transform(&self, mut config: Self::Value) -> Valid<Self::Value, Self::Error> {
let unused_types = config.unused_types();
config = config.remove_types(unused_types);
Valid::succeed(config)
Expand Down
6 changes: 4 additions & 2 deletions src/core/config/transformer/type_merger/type_merger.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::{HashMap, HashSet};

use super::similarity::Similarity;
use crate::core::config::transformer::Transform;
use crate::core::config::{Config, Type};
use crate::core::transform::Transform;
use crate::core::valid::Valid;

pub struct TypeMerger {
Expand Down Expand Up @@ -134,6 +134,8 @@ fn merge_type(type_: &Type, mut merge_into: Type) -> Type {
}

impl Transform for TypeMerger {
type Value = Config;
type Error = String;
fn transform(&self, config: Config) -> Valid<Config, String> {
let config = self.merger(1, config);
Valid::succeed(config)
Expand All @@ -143,8 +145,8 @@ impl Transform for TypeMerger {
#[cfg(test)]
mod test {
use super::TypeMerger;
use crate::core::config::transformer::Transform;
use crate::core::config::{Config, Field, Type};
use crate::core::transform::Transform;
use crate::core::valid::Validator;

#[test]
Expand Down
6 changes: 4 additions & 2 deletions src/core/config/transformer/type_name_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::collections::{BTreeMap, HashSet};

use inflector::Inflector;

use crate::core::config::transformer::Transform;
use crate::core::config::Config;
use crate::core::transform::Transform;
use crate::core::valid::Valid;

#[derive(Debug, Default)]
Expand Down Expand Up @@ -136,6 +136,8 @@ impl TypeNameGenerator {
}

impl Transform for TypeNameGenerator {
type Value = Config;
type Error = String;
fn transform(&self, config: Config) -> Valid<Config, String> {
let config = self.generate_type_names(config);

Expand All @@ -151,8 +153,8 @@ mod test {
use tailcall_fixtures::configs;

use super::TypeNameGenerator;
use crate::core::config::transformer::Transform;
use crate::core::config::Config;
use crate::core::transform::Transform;
use crate::core::valid::Validator;

fn read_fixture(path: &str) -> String {
Expand Down
3 changes: 2 additions & 1 deletion src/core/generator/from_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use super::json::{
FieldBaseUrlGenerator, NameGenerator, QueryGenerator, SchemaGenerator, TypesGenerator,
};
use crate::core::config::transformer::{
ConsolidateURL, RemoveUnused, Transform, TransformerOps, TypeMerger, TypeNameGenerator,
ConsolidateURL, RemoveUnused, TypeMerger, TypeNameGenerator,
};
use crate::core::config::Config;
use crate::core::transform::{Transform, TransformerOps};
use crate::core::valid::Validator;

pub struct ConfigGenerationRequest {
Expand Down
3 changes: 2 additions & 1 deletion src/core/generator/from_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,9 @@ mod test {
use prost_reflect::prost_types::FileDescriptorSet;
use tailcall_fixtures::protobuf;

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

fn from_proto_resolved(files: &[FileDescriptorSet], query: &str) -> Result<Config> {
Expand Down
8 changes: 5 additions & 3 deletions src/core/generator/json/field_base_url_generator.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use url::Url;

use super::url_utils::extract_base_url;
use crate::core::config::transformer::Transform;
use crate::core::config::Config;
use crate::core::transform::Transform;
use crate::core::valid::Valid;

pub struct FieldBaseUrlGenerator<'a> {
Expand All @@ -17,7 +17,9 @@ impl<'a> FieldBaseUrlGenerator<'a> {
}

impl Transform for FieldBaseUrlGenerator<'_> {
fn transform(&self, mut config: Config) -> Valid<Config, String> {
type Value = Config;
type Error = String;
fn transform(&self, mut config: Self::Value) -> Valid<Self::Value, Self::Error> {
let base_url = match extract_base_url(self.url) {
Some(base_url) => base_url,
None => {
Expand Down Expand Up @@ -49,8 +51,8 @@ mod test {
use url::Url;

use super::FieldBaseUrlGenerator;
use crate::core::config::transformer::Transform;
use crate::core::config::{Config, Field, Http, Type};
use crate::core::transform::Transform;
use crate::core::valid::Validator;

#[test]
Expand Down
8 changes: 5 additions & 3 deletions src/core/generator/json/schema_generator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::core::config::transformer::Transform;
use crate::core::config::Config;
use crate::core::transform::Transform;
use crate::core::valid::Valid;

pub struct SchemaGenerator {
Expand All @@ -18,7 +18,9 @@ impl SchemaGenerator {
}

impl Transform for SchemaGenerator {
fn transform(&self, mut config: Config) -> Valid<Config, String> {
type Value = Config;
type Error = String;
fn transform(&self, mut config: Self::Value) -> Valid<Self::Value, Self::Error> {
self.generate_schema(&mut config);
Valid::succeed(config)
}
Expand All @@ -29,7 +31,7 @@ mod test {
use anyhow::Ok;

use super::SchemaGenerator;
use crate::core::config::transformer::Transform;
use crate::core::transform::Transform;
use crate::core::valid::Validator;

#[test]
Expand Down
Loading

0 comments on commit cd95265

Please sign in to comment.