Skip to content

Instantly share code, notes, and snippets.

@shiryel
Created May 19, 2023 22:55
Show Gist options
  • Save shiryel/a5194ee4d9730f0b0d03dbebda9276fb to your computer and use it in GitHub Desktop.
Save shiryel/a5194ee4d9730f0b0d03dbebda9276fb to your computer and use it in GitHub Desktop.
Using Avro on Elixir
###########
# MIX.EXS #
###########
[ {:avro_ex, "~> 2.0"} ]
###########
# CONTEXT #
###########
schema =
AvroEx.decode_schema!(%{
"name" => "any",
"type" => "array",
"items" => %{
"type" => "record",
"name" => "package",
"fields" => [
%{"name" => "id", "type" => "int"},
%{"name" => "name", "type" => "string"},
%{"name" => "attr", "type" => "string"},
%{
"name" => "attr_aliases",
"type" => %{
"type" => "array",
"items" => "string"
}
},
%{"name" => "description", "type" => ["string", "null"]},
%{"name" => "is_cached", "type" => ["boolean", "null"]},
%{"name" => "broken", "type" => ["boolean", "null"]},
%{"name" => "insecure", "type" => ["boolean", "null"]},
%{"name" => "unfree", "type" => ["boolean", "null"]},
%{
"name" => "homepage",
"type" => [
%{
"type" => "array",
"items" => "string"
},
%{"type" => "null"}
]
},
%{
"name" => "binaries",
"type" => [
%{
"type" => "array",
"items" => "string"
},
%{"type" => "null"}
]
}
]
}
})
result = AvroEx.encode!(schema, data_map)
##############
# CONTROLLER #
##############
alias Plug.Conn
conn
|> Conn.resp(200, result)
|> Conn.put_resp_header("content-encoding", encoding)
|> Conn.put_resp_header("content-type", "application/*+avro")
|> Conn.send_resp()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment