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 params argument to Subscription.delete/2 #386

Merged
merged 3 commits into from Jun 14, 2018
Merged

Add params argument to Subscription.delete/2 #386

merged 3 commits into from Jun 14, 2018

Conversation

dbstratta
Copy link
Contributor

Fixes #385

@coveralls
Copy link

Coverage Status

Coverage remained the same at 83.403% when pulling 361909c on strattadb:master into 8ada585 on code-corps:master.

@coveralls
Copy link

coveralls commented Jun 13, 2018

Coverage Status

Coverage increased (+0.07%) to 83.473% when pulling df2e4a5 on strattadb:master into 8ada585 on code-corps:master.

Copy link
Contributor

@begedin begedin left a 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
Copy link
Contributor

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, [])

Copy link
Collaborator

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. 👍

Copy link
Contributor Author

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

Copy link
Collaborator

@snewcomer snewcomer Jun 13, 2018

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

Copy link
Contributor Author

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)

Copy link
Collaborator

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.

Copy link
Contributor Author

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?

Copy link
Collaborator

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 👌

Copy link
Collaborator

@snewcomer snewcomer left a 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 """
Copy link
Collaborator

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?

Copy link
Contributor Author

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)
Copy link
Collaborator

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...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

Copy link
Collaborator

@snewcomer snewcomer left a 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, [])
Copy link
Collaborator

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...

Copy link
Contributor Author

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?

Copy link
Collaborator

@snewcomer snewcomer Jun 14, 2018

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

Copy link
Contributor

@begedin begedin left a 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.

@begedin begedin merged commit 6f11198 into beam-community:master Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants