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

Allow custom extensions to use options #683

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions lib/postgrex/type_module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -920,9 +920,12 @@ defmodule Postgrex.TypeModule do
end
end

defp configure(extensions, opts) do
defaults = Postgrex.Utils.default_extensions(opts)
Enum.map(extensions ++ defaults, &configure/1)
defp configure(customs, opts) do
defaults = Postgrex.Utils.default_extensions()

Enum.map(customs ++ defaults, fn extension ->
configure({extension, opts})
end)
end

defp configure({extension, opts}) do
Expand All @@ -932,10 +935,6 @@ defmodule Postgrex.TypeModule do
{extension, {state, matching, format}}
end

defp configure(extension) do
configure({extension, []})
end

defp define_inline(module, config, opts) do
quoted = [
directives(config, opts),
Expand Down
6 changes: 3 additions & 3 deletions lib/postgrex/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ defmodule Postgrex.Utils do
@doc """
List all default extensions.
"""
@spec default_extensions(Keyword.t()) :: [{module(), Keyword.t()}]
def default_extensions(opts \\ []) do
Enum.map(@extensions, &{&1, opts})
@spec default_extensions() :: [module()]
def default_extensions() do
@extensions
end

@doc """
Expand Down
9 changes: 5 additions & 4 deletions test/custom_extensions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ defmodule CustomExtensionsTest do
defmodule BinaryExtension do
@behaviour Postgrex.Extension

def init([]),
do: []
def init(_opts) do
[]
end

def matching([]),
do: [send: "int4send"]
Expand All @@ -36,7 +37,7 @@ defmodule CustomExtensionsTest do
defmodule TextExtension do
@behaviour Postgrex.Extension

def init([]),
def init(_opts),
do: {}

def matching({}),
Expand All @@ -63,7 +64,7 @@ defmodule CustomExtensionsTest do
defmodule BadExtension do
@behaviour Postgrex.Extension

def init([]),
def init(_opts),
do: []

def matching([]),
Expand Down
Loading