Skip to content

Commit

Permalink
allow custom extensions to use options
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Rychlewski authored and Greg Rychlewski committed Jun 28, 2024
1 parent cb9c213 commit a874f0c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
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

0 comments on commit a874f0c

Please sign in to comment.