Skip to content

Commit

Permalink
Revert "Workaround for type name collisions (#601)" (#654)
Browse files Browse the repository at this point in the history
This reverts commit ed9a65a.
  • Loading branch information
ahl authored Aug 12, 2024
1 parent c965c6d commit fabc51d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 314 deletions.
20 changes: 9 additions & 11 deletions typify-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,15 +562,14 @@ impl TypeSpace {
} else {
Name::Unknown
};
self.convert_ref_type(type_name, schema, type_id)?;
self.convert_ref_type(type_name, schema, type_id)?
}

Some(replace_type) => {
let type_entry = TypeEntry::new_native(
replace_type.replace_type.clone(),
&replace_type.impls.clone(),
);

self.id_to_entry.insert(type_id, type_entry);
}
}
Expand Down Expand Up @@ -598,7 +597,7 @@ impl TypeSpace {
.and_then(|m| m.default.as_ref())
.cloned()
.map(WrappedValue::new);
let mut type_entry = match &mut type_entry.details {
let type_entry = match &mut type_entry.details {
// The types that are already named are good to go.
TypeEntryDetails::Enum(details) => {
details.default = default;
Expand Down Expand Up @@ -646,9 +645,9 @@ impl TypeSpace {
)
}
};
if let Some(entry_name) = type_entry.name().cloned() {
let name = type_entry.ensure_unique_name(self).unwrap_or(entry_name);
self.name_to_id.insert(name, type_id.clone());
// TODO need a type alias?
if let Some(entry_name) = type_entry.name() {
self.name_to_id.insert(entry_name.clone(), type_id.clone());
}
self.id_to_entry.insert(type_id, type_entry);
Ok(())
Expand Down Expand Up @@ -825,10 +824,10 @@ impl TypeSpace {
/// checking for duplicate type definitions (e.g. to make sure there aren't
/// two conflicting types of the same name), and deduplicates various
/// flavors of built-in types.
fn assign_type(&mut self, mut ty: TypeEntry) -> TypeId {
fn assign_type(&mut self, ty: TypeEntry) -> TypeId {
if let TypeEntryDetails::Reference(type_id) = ty.details {
type_id
} else if let Some(name) = ty.name().cloned() {
} else if let Some(name) = ty.name() {
// If there's already a type of this name, we make sure it's
// identical. Note that this covers all user-defined types.

Expand All @@ -838,7 +837,7 @@ impl TypeSpace {
// bunch of places and if that were the case we might expect
// them to be different and resolve that by renaming or scoping
// them in some way.
if let Some(type_id) = self.name_to_id.get(&name) {
if let Some(type_id) = self.name_to_id.get(name) {
// TODO we'd like to verify that the type is structurally the
// same, but the types may not be functionally equal. This is a
// consequence of types being "finalized" after each type
Expand All @@ -847,8 +846,7 @@ impl TypeSpace {
type_id.clone()
} else {
let type_id = self.assign();
let name = ty.ensure_unique_name(self).unwrap_or(name);
self.name_to_id.insert(name, type_id.clone());
self.name_to_id.insert(name.clone(), type_id.clone());
self.id_to_entry.insert(type_id.clone(), ty);
type_id
}
Expand Down
34 changes: 0 additions & 34 deletions typify-impl/src/type_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,40 +514,6 @@ impl TypeEntry {
self.check_defaults(type_space)
}

// Ensures that the name is unique within the provided `TypeSpace`.
//
// This function checks if the current object's name already exists in the
// `TypeSpace`. If it does, the function modifies the name by appending
// numbers until a unique name is found (Name2, Name3 ...).
//
// The unique name is then inserted into the `TypeSpace` and the object's
// name is updated accordingly.
pub(crate) fn ensure_unique_name(&mut self, type_space: &mut TypeSpace) -> Option<String> {
if let Some(name) = self.name() {
let mut postfix = 2;
let mut new_name = name.clone();
while type_space.name_to_id.contains_key(&new_name) {
new_name = format!("{name}{postfix}");
postfix += 1;
}
self.rename(new_name.clone());
Some(new_name)
} else {
None
}
}

pub(crate) fn rename(&mut self, new_name: String) {
match &mut self.details {
TypeEntryDetails::Enum(TypeEntryEnum { name, .. })
| TypeEntryDetails::Struct(TypeEntryStruct { name, .. })
| TypeEntryDetails::Newtype(TypeEntryNewtype { name, .. }) => {
*name = new_name;
}
_ => {}
}
}

pub(crate) fn name(&self) -> Option<&String> {
match &self.details {
TypeEntryDetails::Enum(TypeEntryEnum { name, .. })
Expand Down
26 changes: 0 additions & 26 deletions typify/tests/schemas/property-name-collision.json

This file was deleted.

243 changes: 0 additions & 243 deletions typify/tests/schemas/property-name-collision.rs

This file was deleted.

0 comments on commit fabc51d

Please sign in to comment.