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

Pattern matching on Cloudex.upload/2 departs from Elixir conventions? #20

Closed
abitdodgy opened this issue Jan 9, 2017 · 4 comments
Closed

Comments

@abitdodgy
Copy link
Contributor

Correct me if I'm wrong, but it seems that pattern matching for Cloudex.upload/2 departs from Elixir conventions.

Normally, in Elixir we do something like this:

case func do
  {:ok, result} ->
    # ...
  {:error, reason} ->
    # ...
end

Cloudex returns a list, however. How come?

case Cloudex.upload("foo.jpg") do
  [ok: result] ->
    # ...
  [error: reason} ->
    # ...
end
@smeevil
Copy link
Owner

smeevil commented Jan 10, 2017

The reason is that Cloudex.upload/2 can handle a single file, multiple files, or a directory. Thus the result you get back is a list with a result for each file it uploaded, in case that you uploaded only one file, you will get a list with one element. if you uploaded a directory for example you would get a result per uploaded file. for example :

[
      {:ok, %Cloudex.UploadedImage{...}},
      {:ok, %Cloudex.UploadedImage{...}},
      {:ok, %Cloudex.UploadedImage{...}}
    ] = Cloudex.upload("/assets/multiple")

Hope this makes sense for you :)

@abitdodgy
Copy link
Contributor Author

Thanks, that makes sense. I don't mean to nitpick, but I was curious. Another thing that peaked my interest was passing the opts to upload as a map, versus a keyword list, which you tend to see in most examples.

@smeevil
Copy link
Owner

smeevil commented Jan 10, 2017

No problem, always ask :)

Regarding a map vs keyword list, this is mostly a personal preference. Practically a keyword list is ordered and thus makes it a lot harder to use for function head pattern matching when there are a lot of options, since you need to make sure you use the same order as the function head expects. With a map its a lot easier to match on specific elements, no matter what the order.

@abitdodgy
Copy link
Contributor Author

Great, thank you the explanations and, of course, for Cloudex. 👍

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