#Erlang Atom question with Kafka library

1 messages · Page 1 of 1 (latest)

tender merlin
#

I'm thinking of building a Kafka library on top of Brod. This is my first FFI project that I'm taking seriously and I have so many questions. My first goal is to implement all the functions necessary to make the Quick Demo from Brod in Gleam.

I'm starting with the brod:start_client function (https://hexdocs.pm/brod/brod.html#start_client/2) with the following spec:
-spec start_client([endpoint()], client_id()) -> ok | {error, any()}.

My first question is what is client_id() as the specs show that is an atom. How can I get such value in Gleam, it looks like a function with no arguments that shoots out a client_id, but I have no clue. Do atoms have a representation in gleam? How should I create one for the start_client/2 function?
-type client_id() :: atom().

#

Erlang Atom question with Kafka library

placid pebble
#

you can create atoms using the gleam/erlang/atom module in gleam_erlang.

it looks like a function with no arguments that shoots out a client_id
no this is just the syntax used in typespecs.

-type client_id() :: atom().

is basically

type ClientId = atom.Atom

in gleam

tender merlin
#

The Brod quick demo:

rr(brod),
{ok, _} = application:ensure_all_started(brod),
KafkaBootstrapEndpoints = [{"localhost", 9092}],
Topic = <<"test-topic">>,
Partition = 0,
ok = brod:start_client(KafkaBootstrapEndpoints, client1),
ok = brod:start_producer(client1, Topic, _ProducerConfig = []),
{ok, FirstOffset} = brod:produce_sync_offset(client1, Topic, Partition, <<"key1">>, <<"value1">>),
ok = brod:produce_sync(client1, Topic, Partition, <<"key2">>, <<"value2">>),
SubscriberCallbackFun = fun(Partition, Msg, ShellPid = CallbackState) -> ShellPid ! Msg, {ok, ack, CallbackState} end,
Receive = fun() -> receive Msg -> Msg after 1000 -> timeout end end,
brod_topic_subscriber:start_link(client1, Topic, Partitions=[Partition],
                                 _ConsumerConfig=[{begin_offset, FirstOffset}],
                                 _CommittedOffsets=[], message, SubscriberCallbackFun,
                                 _CallbackState=self()),
AckCb = fun(Partition, BaseOffset) -> io:format(user, "\nProduced to partition ~p at base-offset ~p\n", [Partition, BaseOffset]) end,
ok = brod:produce_cb(client1, Topic, Partition, <<>>, [{<<"key3">>, <<"value3">>}], AckCb).
Receive().
Receive().
{ok, {_, [Msg]}} = brod:fetch(KafkaBootstrapEndpoints, Topic, Partition, FirstOffset + 2), Msg.
tender merlin
placid pebble
#

technically all no-argument custom type variants are represented as bare atoms too, so

type Wibble {
  Wibble
  Wobble
}

is literally the atoms wibble and wobble at runtime. but there's no way you can exploit this information in a type-safe way

tender merlin
#

What is the typical thing to do when an erlang function returns either an ok atom or an error? Is there any helper function to translate that to a Result value or should I write some erlang to transform those cases?

forest creek
#

i think the move is a small erlang function

tender merlin
#

Going forward with the implementation I've faced another block. One of the main functions of the library takes a module as an argument. How is this translated into gleam?

#

Should I define my own module in the ffi function with the insides of the module being from gleam?

forest creek
#

sorry missed this, pretty sure modules are just atoms, so you could atom.create_from_string... if you have like project/subdir/module you'd do atom.create_from_string("project@subdir@module")