Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feature: Allow user to override or expand package mappings #74

Merged
merged 20 commits into from
May 10, 2024

Conversation

ayrtonandino
Copy link
Contributor

Add a new key custom_mappings to the config, allows the user to override existing mappings, or add new ones like Point from geometry, or missing ones like character from #73.

For this, a new Action was created GetMappings, This action takes the package mappings, merges them with the ones in config, and set all the keys to lowercase (because MapReturnType::class expects them to be lowercase).

public function __invoke(bool $setTimestampsToDate = false): array
{
      $mappings = TypescriptMappings::$mappings;

      if ($setTimestampsToDate) {
          $mappings['datetime'] = 'Date';
          $mappings['date'] = 'Date';
          $mappings['timestamp'] = 'Date';
      }

      return array_change_key_case(array_merge(
          $mappings,
          config('modeltyper.custom_mappings', []),
      ), CASE_LOWER);
}

It's initialized in Generator class, passed down to GenerateJsonOutput/GenerateCliOutput, then to WriteColumnAttribute and finally to MapReturnType.

It's not pretty, but it works, I would prefer to access those mappings and the commands arguments from a Facade.

A side effect is that you now can cast classes like Custom Casts globally, instead of per model.

Added some test, for GetMappings, and ComplexModel with Custom Casts casted from config.

Rename phpstan.neon to phpstan.neon.dist to allow per machine config, fix ignored error in phpstan.

Update github workflows, remove unsupported Laravel 9 from matrix, update actions (Warning: Node.js 16 actions are deprecated), use phpstan.neon.dist in phpstan.yml

Added character as string to TypescriptMappings, fixes #73.

Update readme.md

add new custom_mappings to config, fix typo
Add new getMappings method, takes default mappings and merges them with custom_mappings from config, set keys to lowercase
Add modeltyper to cSpell.words
Use LazilyRefreshDatabase
Add new action GetMappings, move getMappings from TypescriptMappings to GetMappings class
Initialize $mappings in Generator class, and pass it down.

Remove timestampsDate from classes, replace it with mappings array.
Fix old test
Add tests for GetMappings class
Fix Call to an undefined method ReflectionType::getName

As of php 8.1 ReflectionMethod::getReturnType can return ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null, but only ReflectionNamedType implements getName method
rename phpstan.neon to phpstan.neon.dist to allow per machine config
Fix warning "Node.js 16 actions are deprecated"

Remove unsuported laravel 9 from test.yml
@tcampbPPU
Copy link
Member

Thanks for the PR, looking into this now! (sorry for the delay)

@tcampbPPU tcampbPPU merged commit 998c7af into fumeapp:master May 10, 2024
10 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Missing types definitions in psql?
2 participants