Elm Kafka Library

I’ve been thinking about the Avro file format and how nice a library for it could be.

It’s written (but not quite ready for prime time as Named Types aren’t supported yet).

Here’s a feel for the API

type alias Degree =
    { name : String }

type alias Student =
    { name : String, age : Int, sex : Maybe String, degrees : List Degree }

type alias Academic =
    { name : String, school : String, title : String }

type People
    = Student_ Student
    | Academic_ Academic

degreeCodec : Codec Degree
degreeCodec =
    success Degree
        |> requiring "name" string .name
        |> record { baseName = "degree", nameSpace = [] }

studentCodec : Codec Student
studentCodec =
    success Student
        |> requiring "name" string .name
        |> requiring "age" int .age
        |> optional "sex" string .sex
        |> withFallback "degrees" (array degreeCodec) [] .degrees
        |> record { baseName = "student", nameSpace = [] }

academicCodec : Codec Academic
academicCodec =
    success Academic
        |> requiring "name" string .name
        |> requiring "school" string .school
        |> requiring "title" string .title
        |> record { baseName = "academic", nameSpace = [] }

peopleCodec : Codec People
peopleCodec =
    imap (foldPeople Err Ok) (foldResult Student_ Academic_) <|
        union studentCodec academicCodec

Then one calls

makeDecoder : Codec a -> Schema -> Maybe (Bytes.Decode.Decoder a)
makeEncoder : Codec a -> a -> Bytes.Encode.Encoder

To read with your Codec data written with a Schema or write with the schema developed from your Codec.

This uses the profunctor applicative technique to make the API nice for building Schemas, decoders and encoders in one pass.

Anyone think this would be useful? I’m writing it currently as I feel that a elm might actually be excellent for writing Kafka consumers and transformers. Still working on that half though.

1 Like

I have previously had the same thought. I guess typically these are written in Java? Expressing these in a pure functional style as Elm would be really nice. Sadly, it is not something I have ever had the time to explore, but its great to see that someone else is having the same idea!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.