Skip to content

Commit

Permalink
[elixir/plug] Clean up boilerplate when encoding to Jason
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Sep 29, 2024
1 parent db30328 commit c848c3b
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ defmodule FrameworkBenchmarks.Handlers.CachedWorld do
:rand.uniform(10_000)
end)

{:ok, json} =
json =
ids
|> Enum.map(&FrameworkBenchmarks.CachedWorld.get/1)
|> Jason.encode()
|> Jason.encode_to_iodata!()

conn
|> Plug.Conn.put_resp_content_type("application/json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ defmodule FrameworkBenchmarks.Handlers.DB do
def handle(conn) do
id = :rand.uniform(10_000)

{:ok, json} =
json =
FrameworkBenchmarks.Repo.get(FrameworkBenchmarks.Models.World, id)
|> Map.from_struct()
|> Map.drop([:__meta__])
|> Jason.encode()
|> Jason.encode_to_iodata!()

conn
|> Plug.Conn.put_resp_content_type("application/json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule FrameworkBenchmarks.Handlers.JSON do
This is the handle for the /json route
"""
def handle(conn) do
{:ok, json} = Jason.encode(%{message: "Hello, World!"})
json = Jason.encode_to_iodata!(%{message: "Hello, World!"})

conn
|> Plug.Conn.put_resp_content_type("application/json")
Expand Down
14 changes: 3 additions & 11 deletions frameworks/Elixir/plug/lib/framework_benchmarks/handlers/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule FrameworkBenchmarks.Handlers.Query do
def handle(conn) do
number_of_queries = FrameworkBenchmarks.Handlers.Helpers.parse_queries(conn, "queries")

records =
json =
1..number_of_queries
|> Enum.map(fn _ ->
:rand.uniform(10_000)
Expand All @@ -15,16 +15,8 @@ defmodule FrameworkBenchmarks.Handlers.Query do
FrameworkBenchmarks.Repo.get(FrameworkBenchmarks.Models.World, &1)
end)
)
|> Enum.map(&Task.await(&1))

{:ok, json} =
records
|> Enum.map(fn record ->
record
|> Map.from_struct()
|> Map.drop([:__meta__])
end)
|> Jason.encode()
|> Enum.map(&Task.await(&1, :infinity))
|> Jason.encode_to_iodata!()

conn
|> Plug.Conn.put_resp_content_type("application/json")
Expand Down
14 changes: 3 additions & 11 deletions frameworks/Elixir/plug/lib/framework_benchmarks/handlers/update.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule FrameworkBenchmarks.Handlers.Update do
:rand.uniform(10_000)
end)

records =
json =
ids
|> Enum.map(
&Task.async(fn ->
Expand All @@ -38,16 +38,8 @@ defmodule FrameworkBenchmarks.Handlers.Update do
|> FrameworkBenchmarks.Repo.update!()
end)
)
|> Enum.map(&Task.await(&1))

{:ok, json} =
records
|> Enum.map(fn record ->
record
|> Map.from_struct()
|> Map.drop([:__meta__])
end)
|> Jason.encode()
|> Enum.map(&Task.await(&1, :infinity))
|> Jason.encode_to_iodata!()

conn
|> Plug.Conn.put_resp_content_type("application/json")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule FrameworkBenchmarks.Models.World do
use Ecto.Schema

@derive {Jason.Encoder, only: [:id, :randomnumber]}
schema "world" do
field(:randomnumber, :integer)
end
Expand Down

0 comments on commit c848c3b

Please sign in to comment.