Skip to content

Commit

Permalink
figure out recusion depth
Browse files Browse the repository at this point in the history
  • Loading branch information
armandobelardo committed Sep 10, 2024
1 parent 74c842a commit b7a80fd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Callable, List, Optional, Set
from typing import Callable, Dict, List, Optional, Set

import fern.ir.resources as ir_types
from fern.generator_exec import GeneratorConfig
Expand Down Expand Up @@ -72,6 +72,10 @@ def does_circularly_reference_itself(self, type_id: ir_types.TypeId) -> bool:
def get_non_union_circular_references(self) -> Set[ir_types.TypeId]:
...

@abstractmethod
def get_self_referencing_dependencies_from_non_union_types(self) -> Dict[ir_types.TypeId, Set[ir_types.TypeId]]:
...

@abstractmethod
def do_types_reference_each_other(self, a: ir_types.TypeId, b: ir_types.TypeId) -> bool:
...
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable, List, Optional, Set
from typing import Callable, Dict, List, Optional, Set

import fern.ir.resources as ir_types
from fern.generator_exec import GeneratorConfig
Expand Down Expand Up @@ -53,14 +53,16 @@ def __init__(
):
self._non_union_self_referencing_type_ids.add(id)

self._non_union_types_with_self_referencing_dependencies = set()
self._non_union_types_with_self_referencing_dependencies: Dict[str, Set[str]] = dict()
for id, type in self.ir.types.items():
if type.shape.get_as_union().type != "union" and type.shape.get_as_union().type != "undiscriminatedUnion":
for referenced_id in type.referenced_types:
referenced_type = self.ir.types[referenced_id]
# This referenced type is self-referential
if referenced_id in referenced_type.referenced_types:
self._non_union_types_with_self_referencing_dependencies.add(id)
if self._non_union_types_with_self_referencing_dependencies.get(id) is None:
self._non_union_types_with_self_referencing_dependencies[id] = set()
self._non_union_types_with_self_referencing_dependencies[id].add(referenced_id)

def get_module_path_in_project(self, module_path: AST.ModulePath) -> AST.ModulePath:
return self._project_module_path + module_path
Expand Down Expand Up @@ -156,7 +158,7 @@ def does_circularly_reference_itself(self, type_id: ir_types.TypeId) -> bool:
def get_non_union_circular_references(self) -> Set[ir_types.TypeId]:
return self._non_union_self_referencing_type_ids

def get_non_union_types_with_self_referencing_dependencies(self) -> Set[ir_types.TypeId]:
def get_self_referencing_dependencies_from_non_union_types(self) -> Dict[ir_types.TypeId, Set[ir_types.TypeId]]:
return self._non_union_types_with_self_referencing_dependencies

def do_types_reference_each_other(self, a: ir_types.TypeId, b: ir_types.TypeId) -> bool:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,13 @@ def _must_import_after_current_declaration(self, type_name: ir_types.DeclaredTyp
type_id=type_name.type_id, other_type_id=type_id_to_reference
)

is_referencing_circular_reference = (
type_name.type_id in self._context.get_non_union_types_with_self_referencing_dependencies()
)
if is_referencing_circular_reference:
should_import_after = is_referencing_circular_reference
# Get self-referencing dependencies of the type you are trying to add
# And add them as ghost references at the bottom of the file
self_referencing_dependencies_from_non_union_types = self._context.get_self_referencing_dependencies_from_non_union_types()
if type_name.type_id in self_referencing_dependencies_from_non_union_types:
self_referencing_dependencies = self_referencing_dependencies_from_non_union_types[type_name.type_id]
for dependency in self_referencing_dependencies:
self.add_ghost_reference(dependency)

if should_import_after:
self._model_contains_forward_refs = True
Expand Down

0 comments on commit b7a80fd

Please sign in to comment.