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 eb4d3fa
Show file tree
Hide file tree
Showing 2 changed files with 38 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

0 comments on commit eb4d3fa

Please sign in to comment.