Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Replace avram parsing with ParamParser #1616

Merged
merged 1 commit into from
Nov 20, 2021

Conversation

matthewmcgarvey
Copy link
Member

Purpose

Fixes #1383

Description

Where we were relying on Avram's parsing, this adds a new Lucky::ParamParser that handles it a bit differently than Avram does. Instead of adding methods to the types, there's one module with methods (with the same name) for the various types. Logically, though, it works the same. This new way is a bit nicer in that everything for parsing is in one place and not spread out in multiple files.

I did not include parsing for Enums and JSON. Unfortunately it is not possible to have a method generic for all enums and I didn't think JSON was necessary though I think it would be easy enough to add.

I have a potential solution for the enum thing though. What if param could be provided a block that gets the string value and it returns the parsed value matching their variable or nil if it couldn't be parsed?

param role : User::Role do |param|
  User::Role.parse?(param)
end

Just a thought if we feel like it's absolutely necessary to support that.

Checklist

  • - An issue already exists detailing the issue/or feature request that this PR fixes
  • - All specs are formatted with crystal tool format spec src
  • - Inline documentation has been added and/or updated
  • - Lucky builds on docker with ./script/setup
  • - All builds and specs pass on docker with ./script/test

Second
end

struct FormattedTime
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of this was directly taken from Avram https://github.com/luckyframework/avram/blob/9a5a921bb05cc3031d9436d2fdc6927606314876/spec/type_extensions/time_spec.cr#L3-L27

@stephendolan saved me from having to figure out all these tests myself 😅

@@ -479,17 +479,17 @@ module Lucky::Routable
{% end %}
end

result = {{ base_type }}.adapter.parse(val)
result = Lucky::ParamParser.parse(val, {{ base_type }})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the line where we are switching from Avram code to this new module

Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@matthewmcgarvey matthewmcgarvey merged commit e13a8a6 into luckyframework:master Nov 20, 2021
@matthewmcgarvey matthewmcgarvey deleted the param-parser branch November 20, 2021 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lucky depends on Avrams type extensions for param parsing
2 participants