kafkaex / kafka_ex

Kafka client library for Elixir
MIT License
596 stars 162 forks source link

Typespec for KafkaEx.Protocol.Produce.Message with optional :timestamp #480

Open esambo opened 1 year ago

esambo commented 1 year ago

I am getting some typespec errors because Dialyzer doesn't like Message{timestamp: nil}. This timestamp was added as a required key. Shouldn't it be optional, so that it can be skipped as in the compression example, and not produce any dialyzer errors?

alias KafkaEx.Protocol.Produce.{Message, Request}
@type kafka_response ::
        nil
        | :ok
        | {:ok, integer()}
        | {:error, :closed}
        | {:error, :inet.posix()}
        | {:error, any()}
        | iodata()
        | :leader_not_available

@spec test :: kafka_response()
def test do
  %Message{
    key: "ID 1", value: "payload" #, timestamp: 666 # <== required to fix dialyzer error
  }
  |> produce_on_kafka()
end

@spec produce_on_kafka(Message.t()) :: kafka_response()
def produce_on_kafka(%Message{} = message) do
  %Request{
    topic: "test_topic",
    messages: [message]
  }
  |> KafkaEx.produce()
end