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

Add mix task (or Ash Injector?) to replace ~CURL with %Req.Request{} #23

Open
derekkraan opened this issue Dec 5, 2024 · 4 comments
Open

Comments

@derekkraan
Copy link
Owner

It would be nice to have a mix task or injector that you can call that replaces all instances of the ~CURL sigil in your code with the corresponding %Req.Request{}. That would enable workflows like: coding with ~CURL and when you're done, run the mix task to convert it all to %Req.Request{}, which is, I think, what most (or at least many) people want in their app at the end of the day.

@kevinschweikert
Copy link
Collaborator

That sounds like an awesome idea!
What would we need to do?

  • Find the AST where the sigil_CURL/1 is used
  • use Macro.expand_once/2
  • put the AST back or convert to string with Macro.to_string/1

Maybe the mix task igniter.refactor.rename_function from https://github.com/ash-project/igniter/blob/main/lib/igniter/refactors/rename.ex could be an inspiration

@derekkraan
Copy link
Owner Author

Sourceror is the other lib that can help us here (it's what igniter uses under the hood)

@kevinschweikert
Copy link
Collaborator

kevinschweikert commented Dec 5, 2024

True! I did a quick test and it could work! Heres my Livebook Test

Curl Sigil Expansion

Mix.install([
  {:sourceror, "~> 1.7"},
  {:curl_req, "~> 0.98.6"},
])

Sourceror

import CurlReq
"""
~CURL(https://example.com)
"""
|> Sourceror.parse_string!()
|> Macro.postwalk(fn 
  {:sigil_CURL, _dot_meta, _rest} = quoted -> 
    Macro.expand_once(quoted, __ENV__)
  quoted ->
    quoted
end)
|> Sourceror.to_string()
|> Code.eval_string()
|> elem(0)
%Req.Request{
  method: :get,
  url: URI.parse("https://example.com"),
  headers: %{},
  body: nil,
  options: %{},
  halted: false,
  adapter: &Req.Steps.run_finch/1,
  request_steps: [],
  response_steps: [],
  error_steps: [],
  private: %{}
}

@derekkraan
Copy link
Owner Author

It looks like you're already 90% of the way there!

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

2 participants