Skip to content

Commit

Permalink
Use mix format
Browse files Browse the repository at this point in the history
  • Loading branch information
devinus committed Jul 27, 2018
1 parent ee8336f commit ac89db9
Show file tree
Hide file tree
Showing 12 changed files with 494 additions and 266 deletions.
13 changes: 11 additions & 2 deletions .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@
# You can give explicit globs or simply directories.
# In the latter case `**/*.{ex,exs}` will be used.
#
included: ["lib/", "src/", "test/", "web/", "apps/", "bench/", "mix.exs"],
included: [
"lib/",
"src/",
"test/",
"web/",
"apps/",
"bench/",
"{mix,.credo,.formatter}.exs"
],
excluded: [~r"/_build/", ~r"/deps/", ~r"/node_modules/"]
},
#
Expand Down Expand Up @@ -115,7 +123,8 @@
{Credo.Check.Refactor.NegatedConditionsWithElse},
{Credo.Check.Refactor.Nesting},
{Credo.Check.Refactor.PipeChainStart,
excluded_argument_types: [:atom, :binary, :fn, :keyword], excluded_functions: []},
excluded_argument_types: [:atom, :binary, :fn, :keyword],
excluded_functions: []},
{Credo.Check.Refactor.UnlessWithElse},

#
Expand Down
7 changes: 7 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
inputs: [
"{mix,.credo,.formatter}.exs",
"{config,lib,test,bench}/**/*.{ex,exs}"
],
line_length: 80
]
17 changes: 9 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ otp_release:
- 20.1
- 20.0
sudo: false
before_script:
- |
if [ "$TRAVIS_ELIXIR_VERSION|$TRAVIS_OTP_RELEASE" == "1.7.1|21.0" ]; then
set -e;
mix archive.install hex bunt '~> 0.2' --force;
mix archive.install hex credo '~> 0.9' --force;
mix credo --strict;
fi
matrix:
include:
- install:
- mix local.hex --force
- mix archive.install hex bunt '~> 0.2' --force
- mix archive.install hex credo '~> 0.9' --force
script:
- mix format --check-formatted --dry-run
- mix credo --strict
62 changes: 32 additions & 30 deletions bench/run.exs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
encode_jobs = %{
"Poison" => &Poison.encode!/1,
"Jason" => &Jason.encode!/1,
"JSX" => &JSX.encode!/1,
"Tiny" => &Tiny.encode!/1,
"jsone" => &:jsone.encode/1,
"jiffy" => &:jiffy.encode/1,
"JSON" => &JSON.encode!/1,
"Jason" => &Jason.encode!/1,
"JSX" => &JSX.encode!/1,
"Tiny" => &Tiny.encode!/1,
"jsone" => &:jsone.encode/1,
"jiffy" => &:jiffy.encode/1,
"JSON" => &JSON.encode!/1
}

encode_inputs = [
Expand All @@ -16,17 +16,17 @@ encode_inputs = [
"Pokedex",
"JSON Generator",
"UTF-8 unescaped",
"Issue 90",
"Issue 90"
]

decode_jobs = %{
"Poison" => &Poison.decode!/1,
"Jason" => &Jason.decode!/1,
"JSX" => &JSX.decode!(&1, [:strict]),
"Tiny" => &Tiny.decode!/1,
"jsone" => &:jsone.decode/1,
"jiffy" => &:jiffy.decode(&1, [:return_maps]),
"JSON" => &JSON.decode!/1,
"Jason" => &Jason.decode!/1,
"JSX" => &JSX.decode!(&1, [:strict]),
"Tiny" => &Tiny.decode!/1,
"jsone" => &:jsone.decode/1,
"jiffy" => &:jiffy.decode(&1, [:return_maps]),
"JSON" => &JSON.decode!/1
}

decode_inputs = [
Expand All @@ -39,32 +39,33 @@ decode_inputs = [
"JSON Generator (Pretty)",
"UTF-8 escaped",
"UTF-8 unescaped",
"Issue 90",
"Issue 90"
]

read_data = fn (name) ->
read_data = fn name ->
name
|> String.downcase
|> String.downcase()
|> String.replace(~r/([^\w]|-|_)+/, "-")
|> String.trim("-")
|> (&"data/#{&1}.json").()
|> Path.expand(__DIR__)
|> File.read!
|> File.read!()
end

Benchee.run(encode_jobs,
parallel: 4,
# warmup: 5,
# time: 30,
inputs: for name <- encode_inputs, into: %{} do
name
|> read_data.()
|> Poison.decode!
|> (&{name, &1}).()
end,
inputs:
for name <- encode_inputs, into: %{} do
name
|> read_data.()
|> Poison.decode!()
|> (&{name, &1}).()
end,
formatters: [
&Benchee.Formatters.HTML.output/1,
&Benchee.Formatters.Console.output/1,
&Benchee.Formatters.Console.output/1
],
formatter_options: [
html: [
Expand All @@ -77,14 +78,15 @@ Benchee.run(decode_jobs,
parallel: 4,
# warmup: 5,
# time: 30,
inputs: for name <- decode_inputs, into: %{} do
name
|> read_data.()
|> (&{name, &1}).()
end,
inputs:
for name <- decode_inputs, into: %{} do
name
|> read_data.()
|> (&{name, &1}).()
end,
formatters: [
&Benchee.Formatters.HTML.output/1,
&Benchee.Formatters.Console.output/1,
&Benchee.Formatters.Console.output/1
],
formatter_options: [
html: [
Expand Down
28 changes: 16 additions & 12 deletions lib/poison.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Poison do
@readme_path [__DIR__, "..", "README.md"] |> Path.join |> Path.expand
@readme_path [__DIR__, "..", "README.md"] |> Path.join() |> Path.expand()
@external_resource @readme_path
@moduledoc @readme_path |> File.read! |> String.trim
@moduledoc @readme_path |> File.read!() |> String.trim()

alias Poison.{Decode, DecodeError, Decoder}
alias Poison.{EncodeError, Encoder}
Expand All @@ -13,8 +13,9 @@ defmodule Poison do
iex> Poison.encode([1, 2, 3])
{:ok, "[1,2,3]"}
"""
@spec encode(Encoder.t, keyword | Encoder.options) :: {:ok, iodata}
| {:error, EncodeError.t}
@spec encode(Encoder.t(), keyword | Encoder.options()) ::
{:ok, iodata}
| {:error, EncodeError.t()}
def encode(value, options \\ %{}) do
{:ok, encode!(value, options)}
rescue
Expand All @@ -28,7 +29,7 @@ defmodule Poison do
iex> Poison.encode!([1, 2, 3])
"[1,2,3]"
"""
@spec encode!(Encoder.t, keyword | Encoder.options) :: iodata | no_return
@spec encode!(Encoder.t(), keyword | Encoder.options()) :: iodata | no_return
def encode!(value, options \\ %{})

def encode!(value, options) when is_list(options) do
Expand All @@ -37,10 +38,11 @@ defmodule Poison do

def encode!(value, options) do
iodata = Encoder.encode(value, options)

if options[:iodata] do
iodata
else
iodata |> IO.iodata_to_binary
iodata |> IO.iodata_to_binary()
end
end

Expand All @@ -50,10 +52,12 @@ defmodule Poison do
iex> Poison.decode("[1,2,3]")
{:ok, [1, 2, 3]}
"""
@spec decode(iodata) :: {:ok, Parser.t}
| {:error, ParseError.t}
@spec decode(iodata, keyword | Decoder.options) :: {:ok, any}
| {:error, ParseError.t | DecodeError.t}
@spec decode(iodata) ::
{:ok, Parser.t()}
| {:error, ParseError.t()}
@spec decode(iodata, keyword | Decoder.options()) ::
{:ok, any}
| {:error, ParseError.t() | DecodeError.t()}
def decode(iodata, options \\ %{}) do
{:ok, decode!(iodata, options)}
rescue
Expand All @@ -67,12 +71,12 @@ defmodule Poison do
iex> Poison.decode!("[1,2,3]")
[1, 2, 3]
"""
@spec decode!(iodata) :: Parser.t | no_return
@spec decode!(iodata) :: Parser.t() | no_return
def decode!(value) do
Parser.parse!(value, %{})
end

@spec decode!(iodata, keyword | Decoder.options) :: Decoder.t | no_return
@spec decode!(iodata, keyword | Decoder.options()) :: Decoder.t() | no_return
def decode!(value, options) when is_list(options) do
decode!(value, Map.new(options))
end
Expand Down
42 changes: 24 additions & 18 deletions lib/poison/decoder.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Poison.DecodeError do
@type t :: %__MODULE__{message: String.t, value: any}
@type t :: %__MODULE__{message: String.t(), value: any}

defexception message: nil, value: nil

Expand Down Expand Up @@ -51,22 +51,24 @@ defmodule Poison.Decode do
case Map.get(acc, key) do
value when is_map(value) or is_list(value) ->
Map.put(acc, key, transform(value, keys, as, options))

_ ->
acc
end
end)
end

defp transform_struct(value, keys, as, options) when keys in [:atoms, :atoms!] do
defp transform_struct(value, keys, as, options)
when keys in [:atoms, :atoms!] do
as
|> Map.from_struct
|> Map.from_struct()
|> Map.merge(value)
|> do_transform_struct(keys, as, options)
end

defp transform_struct(value, keys, as, options) do
as
|> Map.from_struct
|> Map.from_struct()
|> Enum.reduce(%{}, fn {key, default}, acc ->
Map.put(acc, key, Map.get(value, Atom.to_string(key), default))
end)
Expand All @@ -77,18 +79,22 @@ defmodule Poison.Decode do
default = struct(as.__struct__)

as
|> Map.from_struct
|> Map.from_struct()
|> Enum.reduce(%{}, fn {key, as}, acc ->
new_value = case Map.fetch(value, key) do
{:ok, ^as} when is_map(as) or is_list(as) ->
Map.get(default, key)
{:ok, value} when is_map(value) or is_list(value) ->
transform(value, keys, as, options)
{:ok, value} ->
value
:error ->
Map.get(default, key)
end
new_value =
case Map.fetch(value, key) do
{:ok, ^as} when is_map(as) or is_list(as) ->
Map.get(default, key)

{:ok, value} when is_map(value) or is_list(value) ->
transform(value, keys, as, options)

{:ok, value} ->
value

:error ->
Map.get(default, key)
end

Map.put(acc, key, new_value)
end)
Expand All @@ -104,9 +110,9 @@ defprotocol Poison.Decoder do
@typep as :: map | struct | [as]

@type options :: %{
optional(:keys) => keys,
optional(:as) => as,
}
optional(:keys) => keys,
optional(:as) => as
}

@spec decode(t, options) :: any
def decode(value, options)
Expand Down
Loading

0 comments on commit ac89db9

Please sign in to comment.