-
Notifications
You must be signed in to change notification settings - Fork 66
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
Change the get function to let user find country by name #56
base: master
Are you sure you want to change the base?
Change the get function to let user find country by name #56
Conversation
1e81bf2
to
48f16f5
Compare
lib/countries.ex
Outdated
""" | ||
|
||
def get(country_code) do | ||
[country] = filter_by(:alpha2, country_code) | ||
def get(attrs) when bit_size(attrs) == 16 do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you rename this function to get_by_alpha2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@steffenix sure!
ce16b19
to
854863b
Compare
|
||
case length(country) do | ||
0 -> | ||
[] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The returned value is not consistent and breaking the current API.
We could update it to adopt the following pattern:
{:ok, country}
:not_found pattern
And also add a get!
function that breaks if country does not exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! I will do ASAP
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, you can change the whole case
in this way:
case country do
[] -> []
[country_info | _] -> country_info
end
iex> name | ||
"Poland" | ||
|
||
""" | ||
|
||
def get(country_code) do | ||
[country] = filter_by(:alpha2, country_code) | ||
def get_by_alpha2(attrs) when bit_size(attrs) == 16 do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because you're using a strong match, you should add a !
indicating that if the country isn't found it's going to fail.
def get_by_alpha2!(<<_ :: binary-size(16)>> = attrs) do
hd(filter_by(:alpha2, attrs))
end
def get_by_alpha2(<<_ :: binary-size(16)>> = attrs) do
case filter_by(:alpha2, attrs) do
[] -> nil
[country] -> country
end
end
Description
Change the function get to use guard, this allows users to find countries by name and returning only one record.