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

Custom param parser escape hatch #1845

Open
jwoertink opened this issue Nov 18, 2023 · 0 comments
Open

Custom param parser escape hatch #1845

jwoertink opened this issue Nov 18, 2023 · 0 comments

Comments

@jwoertink
Copy link
Member

I was thinking about how we could allow any custom type in params without needing to support everything. The best way is just provide an escape hatch so when you need something super custom, you have access to it.

For params, maybe you're doing a JSON API (or in my case, graphql), and you want to pass in a custom type

class Graph::Comments::Create < GraphAction
  param comment_input : Graph::Inputs::CommentInput

  post "/graph/comments/create_comment" do
    #....
  end
end

In this case, it's going to be some sort of nested JSON object. We could maybe use an annotation here to let you specify your own parser. Crystal's JSON::Serializable actually has a similar option where you can pass converter like the EpochConverter.

So I'm thinking maybe it looks something like

class Graph::Comments::Create < GraphAction

  # note: can't be Lucky::Param 
  @[Lucky::Param(converter: Graph::Inputs::CommentInput)]
  param comment_input : Graph::Inputs::CommentInput

  post "/graph/comments/create_comment" do
    #....
  end
end

class Graph::Inputs::CommentInput
  include JSON::Serializable

   property text : String
   property author_id : Int64

  # no clue how this would work, but maybe this is ok?
   def self.from_params(value : String)
      self.from_json(value)
   end
end

I'm sure there's a lot of weird edge cases around this, but I wanted to at least get this written down.

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

No branches or pull requests

1 participant