Skip to content

Commit

Permalink
feat: use hash
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-0acf4 committed Jan 10, 2025
1 parent a37cdc6 commit eedf310
Show file tree
Hide file tree
Showing 25 changed files with 578 additions and 605 deletions.
14 changes: 7 additions & 7 deletions examples/typegraphs/metagen/rs/fdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,17 @@ macro_rules! init_mat {
// gen-static-end
use types::*;
pub mod types {
pub type ScalarString1 = String;
pub type ScalarStringDatetime1 = String;
pub type ScalarStringUri1 = String;
pub type Idv3TitleString = String;
pub type Idv3ReleaseTimeStringDatetime = String;
pub type Idv3Mp3UrlStringUri = String;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Idv3 {
pub title: ScalarString1,
pub artist: ScalarString1,
pub title: Idv3TitleString,
pub artist: Idv3TitleString,
#[serde(rename = "releaseTime")]
pub release_time: ScalarStringDatetime1,
pub release_time: Idv3ReleaseTimeStringDatetime,
#[serde(rename = "mp3Url")]
pub mp3_url: ScalarStringUri1,
pub mp3_url: Idv3Mp3UrlStringUri,
}
}
pub mod stubs {
Expand Down
8 changes: 4 additions & 4 deletions src/typegate/src/typegraphs/introspection.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
{
"type": "string",
"title": "scalar_string_1"
"title": "root___type_fn_input_name_string"
},
{
"type": "optional",
Expand Down Expand Up @@ -99,7 +99,7 @@
},
{
"type": "optional",
"title": "type_name_scalar_string_1_optional",
"title": "type_name_root___type_fn_input_name_string_optional",
"item": 3,
"default_value": null
},
Expand Down Expand Up @@ -127,13 +127,13 @@
},
{
"type": "optional",
"title": "type_fields_fn_input_includeDeprecated_scalar_boolean_1_optional",
"title": "type_fields_fn_input_includeDeprecated_type_fields_fn_input_includeDeprecated_boolean_optional",
"item": 11,
"default_value": null
},
{
"type": "boolean",
"title": "scalar_boolean_1"
"title": "type_fields_fn_input_includeDeprecated_boolean"
},
{
"type": "optional",
Expand Down
36 changes: 18 additions & 18 deletions src/typegate/src/typegraphs/typegate.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
},
{
"type": "object",
"title": "scalar_struct_shared_1",
"title": "struct_542d7",
"properties": {},
"id": [],
"required": []
Expand All @@ -109,11 +109,11 @@
},
{
"type": "string",
"title": "scalar_string_1"
"title": "string_5d176"
},
{
"type": "string",
"title": "scalar_string_uri_1",
"title": "Typegraph_url_string_uri",
"format": "uri"
},
{
Expand Down Expand Up @@ -198,7 +198,7 @@
},
{
"type": "string",
"title": "scalar_string_json_1",
"title": "string_json_df54e",
"format": "json"
},
{
Expand Down Expand Up @@ -240,7 +240,7 @@
},
{
"type": "string",
"title": "scalar_string_enum_1",
"title": "root_addTypegraph_fn_output_messages_struct_type_string_enum",
"enum": [
"\"info\"",
"\"warning\"",
Expand Down Expand Up @@ -268,7 +268,7 @@
},
{
"type": "optional",
"title": "root_addTypegraph_fn_output_failure_scalar_string_json_1_optional",
"title": "root_addTypegraph_fn_output_failure_string_json_df54e_optional",
"item": 14,
"default_value": null
},
Expand Down Expand Up @@ -296,12 +296,12 @@
},
{
"type": "list",
"title": "scalar_scalar_string_1_list_shared_1",
"title": "string_5d176_list_691ee",
"items": 5
},
{
"type": "boolean",
"title": "scalar_boolean_1"
"title": "boolean_a56cd"
},
{
"type": "function",
Expand Down Expand Up @@ -333,7 +333,7 @@
},
{
"type": "list",
"title": "root_argInfoByPath_fn_input_argPaths_scalar_scalar_string_1_list_shared_1_list",
"title": "root_argInfoByPath_fn_input_argPaths_string_5d176_list_691ee_list",
"items": 24
},
{
Expand Down Expand Up @@ -369,18 +369,18 @@
},
{
"type": "optional",
"title": "TypeInfo_enum_TypeInfo_enum_scalar_string_json_1_list_optional",
"title": "TypeInfo_enum_TypeInfo_enum_string_json_df54e_list_optional",
"item": 32,
"default_value": null
},
{
"type": "list",
"title": "TypeInfo_enum_scalar_string_json_1_list",
"title": "TypeInfo_enum_string_json_df54e_list",
"items": 14
},
{
"type": "optional",
"title": "TypeInfo_format_scalar_string_1_optional",
"title": "TypeInfo_format_string_5d176_optional",
"item": 5,
"default_value": null
},
Expand Down Expand Up @@ -440,7 +440,7 @@
},
{
"type": "object",
"title": "scalar_struct_shared_2",
"title": "struct_8f8a3",
"properties": {
"typegraph": 5
},
Expand Down Expand Up @@ -477,7 +477,7 @@
},
{
"type": "string",
"title": "scalar_string_enum_2",
"title": "OperationInfo_type_string_enum",
"enum": [
"\"query\"",
"\"mutation\""
Expand Down Expand Up @@ -596,7 +596,7 @@
},
{
"type": "object",
"title": "scalar_struct_shared_3",
"title": "struct_5fa3d",
"properties": {
"typegraph": 5,
"runtime": 5,
Expand Down Expand Up @@ -697,13 +697,13 @@
},
{
"type": "optional",
"title": "PrismaBatchQuery_transaction_struct_isolationLevel_scalar_string_enum_3_optional",
"title": "PrismaBatchQuery_transaction_struct_isolationLevel_PrismaBatchQuery_transaction_struct_isolationLevel_string_enum_optional",
"item": 63,
"default_value": null
},
{
"type": "string",
"title": "scalar_string_enum_3",
"title": "PrismaBatchQuery_transaction_struct_isolationLevel_string_enum",
"enum": [
"\"read uncommitted\"",
"\"readuncommitted\"",
Expand Down Expand Up @@ -777,7 +777,7 @@
},
{
"type": "integer",
"title": "scalar_integer_1"
"title": "integer_e116e"
},
{
"type": "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,28 @@ expression: typegraph.0
},
{
"type": "integer",
"title": "scalar_integer_1"
"title": "root_one_fn_input_one_integer"
},
{
"type": "integer",
"title": "scalar_integer_2",
"title": "integer_f0e37",
"minimum": 12,
"maximum": 44
},
{
"type": "optional",
"title": "root_one_fn_input_three_root_one_fn_input_three_scalar_float_1_list_optional",
"title": "root_one_fn_input_three_root_one_fn_input_three_root_one_fn_input_three_float_list_optional",
"item": 6,
"default_value": null
},
{
"type": "list",
"title": "root_one_fn_input_three_scalar_float_1_list",
"title": "root_one_fn_input_three_root_one_fn_input_three_float_list",
"items": 7
},
{
"type": "float",
"title": "scalar_float_1"
"title": "root_one_fn_input_three_float"
}
],
"materializers": [
Expand Down
41 changes: 18 additions & 23 deletions src/typegraph/core/src/utils/postprocess/naming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use common::typegraph::{
StringFormat, TypeNode, Typegraph,
};
use indexmap::IndexSet;
use sha2::{Digest, Sha256};

use crate::errors::TgError;

Expand Down Expand Up @@ -46,7 +47,6 @@ impl super::PostProcessor for NamingProcessor {
path: vec![],
// ref related
counts: ref_counters.counts,
name_occurences: Default::default(),
};
for (key, &ty_id) in &root_data.properties {
acc.path.push((
Expand Down Expand Up @@ -76,7 +76,6 @@ struct VisitCollector {
path: Vec<(PathSegment, Rc<str>)>,
// ref related
counts: HashMap<u32, IndexSet<u32>>,
name_occurences: HashMap<String, u32>,
}

struct TypeRefCount {
Expand All @@ -103,17 +102,12 @@ impl VisitCollector {
false
}

pub fn next_name(&mut self, name: String) -> String {
// Hopefully more stable than ids on snapshots
format!(
"scalar_{name}_{}",
self.name_occurences
.entry(name.clone())
.and_modify(|counter| {
*counter += 1;
})
.or_insert(1)
)
pub fn next_name(&mut self, name: String, hash_input: String) -> String {
let mut sha256 = Sha256::new();
sha256.update(hash_input.bytes().collect::<Vec<_>>());
let hash = format!("{:x}", sha256.finalize());

format!("{name}_{}", hash.chars().take(5).collect::<String>())
}
}

Expand Down Expand Up @@ -338,16 +332,15 @@ fn gen_name(cx: &VisitContext, acc: &mut VisitCollector, id: u32, ty_name: &str)
let node = &cx.tg.types[id as usize];
let name: Rc<str> = if cx.user_named.contains(&id) {
node.base().title.clone().into()
} else if node.is_scalar() {
acc.next_name(ty_name.to_string()).into()
} else {
macro_rules! use_if_ok {
($default:expr) => {
macro_rules! join_if_ok {
($prefix:expr, $default:expr) => {
if acc.has_more_than_one_referrer(id) {
// Cannot be opinionated on the prefix path if shared (confusing)
acc.next_name(format!("{ty_name}_shared")).into()
let hash_input = $prefix;
acc.next_name(ty_name.to_string(), hash_input)
} else {
$default
format!("{}_{}", $prefix, $default)
}
};
}
Expand All @@ -361,11 +354,13 @@ fn gen_name(cx: &VisitContext, acc: &mut VisitCollector, id: u32, ty_name: &str)
// we don't include optional and list nodes in
// generated names (useless but also, they might be placeholders)
Edge::OptionalItem | Edge::ArrayItem => continue,
Edge::FunctionInput => use_if_ok!(format!("{last_name}_input")),
Edge::FunctionOutput => use_if_ok!(format!("{last_name}_output")),
Edge::ObjectProp(key) => use_if_ok!(format!("{last_name}_{key}_{ty_name}")),
Edge::FunctionInput => join_if_ok!(last_name.to_string(), "input"),
Edge::FunctionOutput => join_if_ok!(last_name.to_string(), "output"),
Edge::ObjectProp(key) => {
join_if_ok!(format!("{last_name}_{key}"), ty_name)
}
Edge::EitherVariant(idx) | Edge::UnionVariant(idx) => {
use_if_ok!(format!("{last_name}_t{idx}_{ty_name}"))
join_if_ok!(format!("{last_name}_t{idx}"), ty_name)
}
};
break;
Expand Down
Loading

0 comments on commit eedf310

Please sign in to comment.