Skip to content

Commit 7c7ccee

Browse files
authored
[elixir/plug] Clean up boilerplate when encoding to Jason (#9301)
1 parent 55c1436 commit 7c7ccee

File tree

6 files changed

+12
-29
lines changed

6 files changed

+12
-29
lines changed

frameworks/Elixir/plug/lib/framework_benchmarks/handlers/cached-world.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ defmodule FrameworkBenchmarks.Handlers.CachedWorld do
1111
:rand.uniform(10_000)
1212
end)
1313

14-
{:ok, json} =
14+
json =
1515
ids
1616
|> Enum.map(&FrameworkBenchmarks.CachedWorld.get/1)
17-
|> Jason.encode()
17+
|> Jason.encode_to_iodata!()
1818

1919
conn
2020
|> Plug.Conn.put_resp_content_type("application/json")

frameworks/Elixir/plug/lib/framework_benchmarks/handlers/db.ex

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ defmodule FrameworkBenchmarks.Handlers.DB do
55
def handle(conn) do
66
id = :rand.uniform(10_000)
77

8-
{:ok, json} =
8+
json =
99
FrameworkBenchmarks.Repo.get(FrameworkBenchmarks.Models.World, id)
10-
|> Map.from_struct()
11-
|> Map.drop([:__meta__])
12-
|> Jason.encode()
10+
|> Jason.encode_to_iodata!()
1311

1412
conn
1513
|> Plug.Conn.put_resp_content_type("application/json")

frameworks/Elixir/plug/lib/framework_benchmarks/handlers/json.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule FrameworkBenchmarks.Handlers.JSON do
33
This is the handle for the /json route
44
"""
55
def handle(conn) do
6-
{:ok, json} = Jason.encode(%{message: "Hello, World!"})
6+
json = Jason.encode_to_iodata!(%{message: "Hello, World!"})
77

88
conn
99
|> Plug.Conn.put_resp_content_type("application/json")

frameworks/Elixir/plug/lib/framework_benchmarks/handlers/query.ex

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule FrameworkBenchmarks.Handlers.Query do
55
def handle(conn) do
66
number_of_queries = FrameworkBenchmarks.Handlers.Helpers.parse_queries(conn, "queries")
77

8-
records =
8+
json =
99
1..number_of_queries
1010
|> Enum.map(fn _ ->
1111
:rand.uniform(10_000)
@@ -15,16 +15,8 @@ defmodule FrameworkBenchmarks.Handlers.Query do
1515
FrameworkBenchmarks.Repo.get(FrameworkBenchmarks.Models.World, &1)
1616
end)
1717
)
18-
|> Enum.map(&Task.await(&1))
19-
20-
{:ok, json} =
21-
records
22-
|> Enum.map(fn record ->
23-
record
24-
|> Map.from_struct()
25-
|> Map.drop([:__meta__])
26-
end)
27-
|> Jason.encode()
18+
|> Enum.map(&Task.await(&1, :infinity))
19+
|> Jason.encode_to_iodata!()
2820

2921
conn
3022
|> Plug.Conn.put_resp_content_type("application/json")

frameworks/Elixir/plug/lib/framework_benchmarks/handlers/update.ex

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ defmodule FrameworkBenchmarks.Handlers.Update do
2222
:rand.uniform(10_000)
2323
end)
2424

25-
records =
25+
json =
2626
ids
2727
|> Enum.map(
2828
&Task.async(fn ->
@@ -38,16 +38,8 @@ defmodule FrameworkBenchmarks.Handlers.Update do
3838
|> FrameworkBenchmarks.Repo.update!()
3939
end)
4040
)
41-
|> Enum.map(&Task.await(&1))
42-
43-
{:ok, json} =
44-
records
45-
|> Enum.map(fn record ->
46-
record
47-
|> Map.from_struct()
48-
|> Map.drop([:__meta__])
49-
end)
50-
|> Jason.encode()
41+
|> Enum.map(&Task.await(&1, :infinity))
42+
|> Jason.encode_to_iodata!()
5143

5244
conn
5345
|> Plug.Conn.put_resp_content_type("application/json")

frameworks/Elixir/plug/lib/framework_benchmarks/models/world.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule FrameworkBenchmarks.Models.World do
22
use Ecto.Schema
33

4+
@derive {Jason.Encoder, only: [:id, :randomnumber]}
45
schema "world" do
56
field(:randomnumber, :integer)
67
end

0 commit comments

Comments
 (0)