Skip to content

Commit

Permalink
feat!: add otp_app option to consumer and producer compile-time optio…
Browse files Browse the repository at this point in the history
…ns to improve configurating the modules
  • Loading branch information
yordis committed Dec 26, 2023
1 parent 460e678 commit c700ace
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
20 changes: 19 additions & 1 deletion lib/kafee/consumer.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
defmodule Kafee.Consumer do
@options_schema NimbleOptions.new!(
otp_app: [
default: nil,
doc: """
The name of the OTP application to read configuration from.
If the option is set, the configuration will be read from the application environment under the
given name. For example, if the OTP application is `:my_app`, the configuration will be read from
`Application.get_env(:my_app, Myapp.KafeeConsumer)`.
""",
required: true,
type: :atom
],
adapter: [
default: nil,
doc: """
Expand Down Expand Up @@ -197,7 +209,13 @@ defmodule Kafee.Consumer do
@doc false
@spec child_spec(Kafee.Consumer.options()) :: Supervisor.child_spec()
def child_spec(args) do
full_opts = Keyword.merge(unquote(Macro.escape(opts)), args)
opts = unquote(Macro.escape(opts))
env_opts = Application.get_env(opts[:otp_app], unquote(__MODULE__), [])

full_opts =
opts
|> Keyword.merge(env_opts)
|> Keyword.merge(args)

%{
id: __MODULE__,
Expand Down
20 changes: 19 additions & 1 deletion lib/kafee/producer.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
defmodule Kafee.Producer do
@options_schema NimbleOptions.new!(
otp_app: [
default: nil,
doc: """
The name of the OTP application to read configuration from.
If the option is set, the configuration will be read from the application environment under the
given name. For example, if the OTP application is `:my_app`, the configuration will be read from
`Application.get_env(:my_app, Myapp.KafeeProducer)`.
""",
required: true,
type: :atom
],
adapter: [
default: nil,
doc: """
Expand Down Expand Up @@ -213,7 +225,13 @@ defmodule Kafee.Producer do
@doc false
@spec child_spec(Kafee.Producer.options()) :: Supervisor.child_spec()
def child_spec(args) do
full_opts = Keyword.merge(unquote(Macro.escape(opts)), args)
opts = unquote(Macro.escape(opts))
env_opts = Application.get_env(opts[:otp_app], unquote(__MODULE__), [])

full_opts =
opts
|> Keyword.merge(env_opts)
|> Keyword.merge(args)

%{
id: __MODULE__,
Expand Down
1 change: 1 addition & 0 deletions test/kafee/consumer/broadway_adapter_integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Kafee.Consumer.BroadwayAdapterIntegrationTest do

defmodule MyConsumer do
use Kafee.Consumer,
otp_app: :kafee,
adapter: {Kafee.Consumer.BroadwayAdapter, []}

def handle_message(%Kafee.Consumer.Message{} = message) do
Expand Down
1 change: 1 addition & 0 deletions test/kafee/consumer/brod_adapter_integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Kafee.Consumer.BrodAdapterIntegrationTest do

defmodule MyConsumer do
use Kafee.Consumer,
otp_app: :kafee,
adapter: Kafee.Consumer.BrodAdapter

def handle_message(%Kafee.Consumer.Message{} = message) do
Expand Down

0 comments on commit c700ace

Please sign in to comment.