Skip to content

Commit

Permalink
- upon type change, handle it in all types.
Browse files Browse the repository at this point in the history
  • Loading branch information
laststylebender14 committed Sep 5, 2024
1 parent 36aba8f commit 01fad33
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 48 deletions.
35 changes: 3 additions & 32 deletions src/core/config/transformer/improve_type_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::{BTreeMap, HashSet};

use convert_case::{Case, Casing};

use super::RenameTypes;
use crate::core::config::Config;
use crate::core::transform::Transform;
use crate::core::valid::Valid;
Expand Down Expand Up @@ -113,42 +114,12 @@ impl<'a> CandidateGeneration<'a> {
#[derive(Default)]
pub struct ImproveTypeNames;

impl ImproveTypeNames {
/// Generates type names based on inferred candidates from the provided
/// configuration.
fn generate_type_names(&self, mut config: Config) -> Config {
let finalized_candidates = CandidateGeneration::new(&config).generate().converge();

for (old_type_name, new_type_name) in finalized_candidates {
if let Some(type_) = config.types.remove(old_type_name.as_str()) {
// Add newly generated type.
config.types.insert(new_type_name.to_owned(), type_);

// Replace all the instances of old name in config.
for actual_type in config.types.values_mut() {
for actual_field in actual_type.fields.values_mut() {
if actual_field.type_of.name() == &old_type_name {
// Update the field's type with the new name
actual_field.type_of = actual_field
.type_of
.clone()
.with_name(new_type_name.to_owned());
}
}
}
}
}
config
}
}

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

Valid::succeed(config)
let finalized_candidates = CandidateGeneration::new(&config).generate().converge();
RenameTypes::new(finalized_candidates.iter()).transform(config.clone())
}
}

Expand Down
75 changes: 69 additions & 6 deletions src/core/config/transformer/rename_types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use indexmap::IndexMap;

use crate::core::config::Config;
Expand Down Expand Up @@ -28,12 +30,11 @@ impl Transform for RenameTypes {

// Ensure all types exist in the configuration
Valid::from_iter(self.0.iter(), |(existing_name, suggested_name)| {
if !config.types.contains_key(existing_name) {
Valid::fail(format!(
"Type '{}' not found in configuration.",
existing_name
))
} else {
if config.types.contains_key(existing_name)
|| config.enums.contains_key(existing_name)
|| config.unions.contains_key(existing_name)
{
// handle for the types.
if let Some(type_info) = config.types.remove(existing_name) {
config.types.insert(suggested_name.to_string(), type_info);
lookup.insert(existing_name.clone(), suggested_name.clone());
Expand All @@ -46,7 +47,24 @@ impl Transform for RenameTypes {
}
}

// handle for the enums.
if let Some(type_info) = config.enums.remove(existing_name) {
config.enums.insert(suggested_name.to_string(), type_info);
lookup.insert(existing_name.clone(), suggested_name.clone());
}

// handle for the union.
if let Some(type_info) = config.unions.remove(existing_name) {
config.unions.insert(suggested_name.to_string(), type_info);
lookup.insert(existing_name.clone(), suggested_name.clone());

Check warning on line 59 in src/core/config/transformer/rename_types.rs

View check run for this annotation

Codecov / codecov/patch

src/core/config/transformer/rename_types.rs#L58-L59

Added lines #L58 - L59 were not covered by tests
}

Valid::succeed(())
} else {
Valid::fail(format!(
"Type '{}' not found in configuration.",
existing_name
))
}
})
.map(|_| {
Expand All @@ -65,6 +83,51 @@ impl Transform for RenameTypes {
}
}
}

// replace in interface.
type_.implements = type_
.implements
.iter()
.filter_map(|interface_type_name| {
lookup
.get(interface_type_name)
.cloned()
.or(Some(interface_type_name.clone()))

Check warning on line 95 in src/core/config/transformer/rename_types.rs

View check run for this annotation

Codecov / codecov/patch

src/core/config/transformer/rename_types.rs#L92-L95

Added lines #L92 - L95 were not covered by tests
})
.collect();
}

// replace in the union as well.
for union_type_ in config.unions.values_mut() {
// Collect changes to be made
let mut types_to_remove = HashSet::new();
let mut types_to_add = HashSet::new();

Check warning on line 104 in src/core/config/transformer/rename_types.rs

View check run for this annotation

Codecov / codecov/patch

src/core/config/transformer/rename_types.rs#L103-L104

Added lines #L103 - L104 were not covered by tests

for type_name in union_type_.types.iter() {
if let Some(merge_into_type_name) = lookup.get(type_name) {
types_to_remove.insert(type_name.clone());
types_to_add.insert(merge_into_type_name.clone());
}

Check warning on line 110 in src/core/config/transformer/rename_types.rs

View check run for this annotation

Codecov / codecov/patch

src/core/config/transformer/rename_types.rs#L106-L110

Added lines #L106 - L110 were not covered by tests
}
// Apply changes
for type_name in types_to_remove {
union_type_.types.remove(&type_name);
}

Check warning on line 115 in src/core/config/transformer/rename_types.rs

View check run for this annotation

Codecov / codecov/patch

src/core/config/transformer/rename_types.rs#L113-L115

Added lines #L113 - L115 were not covered by tests

for type_name in types_to_add {
union_type_.types.insert(type_name);
}

Check warning on line 119 in src/core/config/transformer/rename_types.rs

View check run for this annotation

Codecov / codecov/patch

src/core/config/transformer/rename_types.rs#L117-L119

Added lines #L117 - L119 were not covered by tests
}

// replace in union as well.
for union_type_ in config.unions.values_mut() {
union_type_.types = union_type_
.types
.iter()
.filter_map(|type_name| {
lookup.get(type_name).cloned().or(Some(type_name.clone()))
})
.collect();

Check warning on line 130 in src/core/config/transformer/rename_types.rs

View check run for this annotation

Codecov / codecov/patch

src/core/config/transformer/rename_types.rs#L124-L130

Added lines #L124 - L130 were not covered by tests
}

config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ input news__NewsInput {
body: String
id: Int
postImage: String
status: news__Status
status: Status
title: String
}

enum news__Status {
enum Status {
DELETED
DRAFT
PUBLISHED
Expand Down Expand Up @@ -52,7 +52,7 @@ type News {
body: String
id: Int
postImage: String
status: news__Status
status: Status
title: String
}

Expand Down Expand Up @@ -80,11 +80,11 @@ type Post {
type Query {
inCompatibleProperties: InCompatibleProperty @http(path: "/")
news__NewsService__AddNews(news: news__NewsInput!): News @grpc(body: "{{.args.news}}", method: "news.NewsService.AddNews")
news__NewsService__DeleteNews(newsId: news__NewsId!): Empty @grpc(body: "{{.args.newsId}}", method: "news.NewsService.DeleteNews")
news__NewsService__DeleteNews(newsId: Id!): Empty @grpc(body: "{{.args.newsId}}", method: "news.NewsService.DeleteNews")
news__NewsService__EditNews(news: news__NewsInput!): News @grpc(body: "{{.args.news}}", method: "news.NewsService.EditNews")
news__NewsService__GetAllNews: NewsNewsServiceGetMultipleNew @grpc(method: "news.NewsService.GetAllNews")
news__NewsService__GetMultipleNews(multipleNewsId: news__MultipleNewsId!): NewsNewsServiceGetMultipleNew @grpc(body: "{{.args.multipleNewsId}}", method: "news.NewsService.GetMultipleNews")
news__NewsService__GetNews(newsId: news__NewsId!): News @grpc(body: "{{.args.newsId}}", method: "news.NewsService.GetNews")
news__NewsService__GetNews(newsId: Id!): News @grpc(body: "{{.args.newsId}}", method: "news.NewsService.GetNews")
post(id: Int! = 1): Post @http(path: "/posts/{{.args.id}}")
posts: [Post] @http(path: "/posts?_limit=11")
user(id: Int!): User @http(path: "/users/{{.args.id}}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ input news__NewsInput {
body: String
id: Int
postImage: String
status: news__Status
status: Status
title: String
}

enum news__Status {
enum Status {
DELETED
DRAFT
PUBLISHED
Expand Down Expand Up @@ -51,7 +51,7 @@ type News {
body: String
id: Int
postImage: String
status: news__Status
status: Status
title: String
}

Expand All @@ -61,11 +61,11 @@ type NewsNewsServiceGetMultipleNew {

type Query {
news__NewsService__AddNews(news: news__NewsInput!): News @grpc(body: "{{.args.news}}", method: "news.NewsService.AddNews")
news__NewsService__DeleteNews(newsId: news__NewsId!): Empty @grpc(body: "{{.args.newsId}}", method: "news.NewsService.DeleteNews")
news__NewsService__DeleteNews(newsId: Id!): Empty @grpc(body: "{{.args.newsId}}", method: "news.NewsService.DeleteNews")
news__NewsService__EditNews(news: news__NewsInput!): News @grpc(body: "{{.args.news}}", method: "news.NewsService.EditNews")
news__NewsService__GetAllNews: NewsNewsServiceGetMultipleNew @grpc(method: "news.NewsService.GetAllNews")
news__NewsService__GetMultipleNews(multipleNewsId: news__MultipleNewsId!): NewsNewsServiceGetMultipleNew @grpc(body: "{{.args.multipleNewsId}}", method: "news.NewsService.GetMultipleNews")
news__NewsService__GetNews(newsId: news__NewsId!): News @grpc(body: "{{.args.newsId}}", method: "news.NewsService.GetNews")
news__NewsService__GetNews(newsId: Id!): News @grpc(body: "{{.args.newsId}}", method: "news.NewsService.GetNews")
users: [User] @http(path: "/users")
}

Expand Down

0 comments on commit 01fad33

Please sign in to comment.