Skip to content

Commit

Permalink
Fix check of existing types, rename arguments with the same name on c…
Browse files Browse the repository at this point in the history
…odegen (#141)

* Fix typos in codegen module

* Avoid duplicate arguments

* Changelog

* Changelog
  • Loading branch information
droserasprout authored Sep 27, 2021
1 parent 3007015 commit 65ef228
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Changelog

## 3.0.2 - 2021-09-25
## [unreleased]

### Added

Expand All @@ -15,7 +15,9 @@
* Removed unnecessary calls to TzKT API during the partial sync.
* Fixed removal of PostgreSQL extensions (`timescaledb`, `pgcrypto`) by function `truncate_database` triggered on reindex.
* Fixed updating relation between index and head in DB.
* Fixed creation of missing project package
* Fixed creation of missing project package on `init`.
* Fixed invalid handler callbacks generated on `init`.
* Fixed detection of existing types in the project.

## 3.0.1 - 2021-09-24

Expand Down
2 changes: 1 addition & 1 deletion src/dipdup/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ async def generate_types(self, overwrite_types: bool = False) -> None:
input_path = join(root, file)
output_path = join(types_root, f'{pascal_to_snake(name)}.py')

if exists and not overwrite_types:
if exists(output_path) and not overwrite_types:
continue

# NOTE: Skip if the first line starts with "# dipdup: ignore"
Expand Down
23 changes: 15 additions & 8 deletions src/dipdup/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,15 @@ def format_imports(self, package: str) -> Iterator[str]:
yield f'from {package} import {cls}'

def format_arguments(self) -> Iterator[str]:
for name, cls in self.iter_arguments():
yield f'{name}: {cls}'
arguments = list(self.iter_arguments())
i, counter = 0, Counter(name for name, _ in arguments)

for name, cls in arguments:
if counter[name] > 1:
yield f'{name}_{i}: {cls}'
i += 1
else:
yield f'{name}: {cls}'

def locate_arguments(self) -> Dict[str, Optional[Type]]:
kwargs: Dict[str, Optional[Type]] = {}
Expand All @@ -251,13 +258,13 @@ class PatternConfig(CodegenMixin, ABC):
@classmethod
def format_storage_import(cls, package: str, module_name: str) -> Tuple[str, str]:
storage_cls = f'{snake_to_pascal(module_name)}Storage'
return f'from {package}.types.{module_name}.storage', storage_cls
return f'{package}.types.{module_name}.storage', storage_cls

@classmethod
def format_parameter_import(cls, package: str, module_name: str, entrypoint: str) -> Tuple[str, str]:
entrypoint = entrypoint.lstrip('_')
parameter_cls = f'{snake_to_pascal(entrypoint)}Parameter'
return f'from {package}.types.{module_name}.parameter.{pascal_to_snake(entrypoint)}', parameter_cls
return f'{package}.types.{module_name}.parameter.{pascal_to_snake(entrypoint)}', parameter_cls

@classmethod
def format_origination_argument(cls, module_name: str, optional: bool) -> Tuple[str, str]:
Expand Down Expand Up @@ -396,7 +403,7 @@ def iter_imports(self, package: str) -> Iterator[Tuple[str, str]]:
return

module_name = self.destination_contract_config.module_name
yield 'from dipdup.models', 'Transaction'
yield 'dipdup.models', 'Transaction'
yield self.format_parameter_import(package, module_name, self.entrypoint)
yield self.format_storage_import(package, module_name)

Expand Down Expand Up @@ -459,7 +466,7 @@ def iter_imports(self, package: str) -> Iterator[Tuple[str, str]]:
module_name = self.originated_contract_config.module_name
else:
raise ConfigurationError('Origination pattern must have at least one of `source`, `similar_to`, `originated_contract` fields')
yield 'from dipdup.models', 'Origination'
yield 'dipdup.models', 'Origination'
yield self.format_storage_import(package, module_name)

def iter_arguments(self) -> Iterator[Tuple[str, str]]:
Expand Down Expand Up @@ -666,13 +673,13 @@ def __post_init_post_parse__(self):
def format_key_import(cls, package: str, module_name: str, path: str) -> Tuple[str, str]:
key_cls = f'{snake_to_pascal(module_name)}Key'
key_module = f'{path}_key'
return f'from {package}.types.{module_name}.big_map.{key_module}', key_cls
return f'{package}.types.{module_name}.big_map.{key_module}', key_cls

@classmethod
def format_value_import(cls, package: str, module_name: str, path: str) -> Tuple[str, str]:
value_cls = f'{snake_to_pascal(module_name)}Value'
value_module = f'{path}_value'
return f'from {package}.types.{module_name}.big_map.{value_module}', value_cls
return f'{package}.types.{module_name}.big_map.{value_module}', value_cls

@classmethod
def format_big_map_diff_argument(cls, module_name: str) -> Tuple[str, str]:
Expand Down

0 comments on commit 65ef228

Please sign in to comment.