-
Notifications
You must be signed in to change notification settings - Fork 356
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 params
argument to Subscription.delete/2
#386
Conversation
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.
Certainly a worthy addition, but there may be improvements we can do to keep backwards compatibility.
when params: %{ | ||
optional(:at_period_end) => boolean | ||
} | ||
def delete(id, params \\ %{}, opts \\ []) 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.
@snewcomer Is this a breaking change. If I'm interpreting it correctly, any app that's using this endpoint as
id |> delete(custom_options)
will stop working with this change.
Should we add clauses to keep backwards compatibility. For example
def delete(id, opts) when is_list(opts), do: delete(id, %{}, opts)
def delete(id, params) when is_map(params), do: delete(id, params, [])
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.
@begedin Looking back, it seems params was taken out ~8 months ago and went into 2.0-beta. I missed it in the refactor.
So given the time frame, it's probably a good idea to do what you recommended and perhaps add 2 tests for Subscription.delete - one passing a map and one passing a list as the second arg. 👍
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.
@begedin You're right! I'd forgotten about backwards compatibility.
What about:
def delete(id, opts \\ []) when is_list(opts), do: delete(id, %{}, opts)
def delete(id, params, opts) when is_map(params) do
# The actual thing
end
This way you can choose to pass params
and opts
in a call and it's still backwards compatible. The only downside I see is that you can't pass only params
and get the default opts
.
So, if you want to call the function with params
and the default opts
it'd have to be:
delete(id, params, [])
What do you think? @begedin @snewcomer
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.
@strattadb I think this will work... Note - without the when params: {
spec stuff.
@spec delete(Stripe.id() | t, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
def delete(id, opts \\ []) when is_list(opts), do: delete(id, %{}, opts)
@spec delete(Stripe.id() | t, map, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
def delete(id, params \\ %{}, opts \\ []) 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.
@snewcomer I think that's the best solution, but I tried it in IEx and it didn't compile.
** (CompileError) iex:3: def delete/3 defaults conflicts with delete/2
iex:3: (module)
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.
@strattadb Oh right b/c of expansion at compile time. I think Nikola was right in the first place 😆 (with guards)...
@spec delete(Stripe.id() | t, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
def delete(id, opts) when is_list(opts), do: delete(id, %{}, opts)
@spec delete(Stripe.id() | t, map) :: {:ok, t} | {:error, Stripe.Error.t()}
def delete(id, params) when is_map(params), do: delete(id, params, [])
@spec delete(Stripe.id() | t, map, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
def delete(id, params \\ %{}, opts \\ []) do
We basically need an arity of 1, 2, or 3, with the second argument being either of type List or Map.
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.
@snewcomer Oh, of course! I misunderstood Nikola's suggestion the first time. I'll add the clauses and tests.
Can I ask you why not the params
spec clause?
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.
params spec is needed for the docs. my bad 👌
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.
All good now! Just one minor ask.
optional(:at_period_end) => boolean | ||
} | ||
def delete(id, params) when is_map(params), do: delete(id, params, []) | ||
|
||
@doc """ |
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.
mind moving this doc block above the other two delete funcs?
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!
params = %{at_period_end: true} | ||
|
||
assert {:ok, %Stripe.Subscription{} = subscription} = | ||
Stripe.Subscription.delete("sub_123", params) |
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.
inline params if you wanted while you are in here...
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!
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.
💯 Really appreciate the work you put into this!
when params: %{ | ||
optional(:at_period_end) => boolean | ||
} | ||
def delete(id, params) when is_map(params), do: delete(id, params, []) |
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.
Technically we wouldn't need the third []
since it defaults to that...
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.
Maybe I'm missing something, but wouldn't that make that clause call itself infinitely?
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.
I wish I could delete that comment :) . Yep you are right
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.
Looking good. I'll try and cut a new release today.
Fixes #385