Skip to content

Commit

Permalink
also allow poison 2.0 as dep
Browse files Browse the repository at this point in the history
* also allow poison 2.0 as dep

problem: 
- some project use already poison 2.0 (or 2.1), verk installation does not work there


solution: 
- also allow poison 2.0 by relaxing dependency

```
{ :poison, "~> 1.5 or ~> 2.0"},
```

* different json decoding based on Poison version

problem: 
- Poison >2.0 changed the way it accepts `as` params
```
https://github.com/devinus/poison
If you use Poison 1.x, you have to set a module to as option in order to decode into a struct. e.g. as: Person instead of as: %Person{}. The change was introduced at 2.0.0.
```
- we want to stay compatible with Poison < 2.0 and > 2.0 

solution: 
- read the current Poison version during compilation time and switch encoding logic based on whether we are before 2.0 or after.
  • Loading branch information
mindreframer authored and edgurgel committed Apr 7, 2016
1 parent 7825039 commit b18b20e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/verk/job.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Verk.Job do
use Verk.PoisonVersion
@keys [error_message: nil, failed_at: nil, retry_count: 0, queue: nil, class: nil, args: [],
jid: nil, finished_at: nil, enqueued_at: nil, retried_at: nil, error_backtrace: nil]

Expand All @@ -10,7 +11,10 @@ defmodule Verk.Job do
"""
@spec decode!(binary) :: %__MODULE__{}
def decode!(payload) do
job = Poison.decode!(payload, as: __MODULE__)
job = decode!(payload, is_before_poison_2)
%Verk.Job{ job | original_json: payload }
end

def decode!(payload, true), do: Poison.decode!(payload, as: __MODULE__)
def decode!(payload, false), do: Poison.decode!(payload, as: %__MODULE__{})
end
22 changes: 22 additions & 0 deletions lib/verk/poison_version.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule Verk.PoisonVersion do
defmacro __using__(_) do
quote do
def poison_version do
unquote(Verk.PoisonVersion.runtime_poison_version)
end

def is_before_poison_2 do
unquote(Verk.PoisonVersion.runtime_poison_version) |> Version.match?("<2.0.0")
end
end
end

def runtime_poison_version do
Mix.Project.config
|> Keyword.get(:deps)
|> Mix.Dep.loaded
|> Enum.find(fn(%{app: app})-> app == :poison end)
|> Map.get(:status)
|> elem(1)
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule Verk.Mixfile do

defp deps do
[{ :redix, "~> 0.3" },
{ :poison, "~> 1.5" },
{ :poison, "~> 1.5 or ~> 2.0"},
{ :timex, "~> 2.0" },
{ :poolboy, "~> 1.5.1" },
{ :watcher, "~> 1.0" },
Expand Down

0 comments on commit b18b20e

Please sign in to comment.