All Projects → rjdellecese → gleam_decode

rjdellecese / gleam_decode

Licence: Apache-2.0 license
Transform Erlang or Elixir data into Gleam data

Programming Languages

erlang
1774 projects

Projects that are alternatives of or similar to gleam decode

sms
A Go library for encoding and decoding SMSs
Stars: ✭ 37 (+48%)
Mutual labels:  decoding
BeFoR64
BeFoR64, Base64 encoding/decoding library for FoRtran poor men
Stars: ✭ 17 (-32%)
Mutual labels:  decoding
pycayennelpp
A Cayenne Low Power Payload (CayenneLPP) decoder and encoder for Python
Stars: ✭ 17 (-32%)
Mutual labels:  decoding
CTF-CryptoTool
CTF-CryptoTool is a tool written in python, for breaking crypto text of CTF challenges. It tries to decode the cipher by bruteforcing it with all known cipher decoding methods easily. Also works for the cipher which does not have a key.
Stars: ✭ 38 (+52%)
Mutual labels:  decoding
template-gleam-javascript
🧃 A template for trying out Gleam compiled to JavaScript
Stars: ✭ 22 (-12%)
Mutual labels:  gleam
d3coder
Chrome extension for encoding/decoding and hashing text on websites
Stars: ✭ 26 (+4%)
Mutual labels:  decoding
LiveVideo10ms
Real time video decoding on android
Stars: ✭ 41 (+64%)
Mutual labels:  decoding
otp
📫 Fault tolerant multicore programs with actors
Stars: ✭ 169 (+576%)
Mutual labels:  gleam
bs-decode
Type-safe JSON decoding for ReasonML and OCaml
Stars: ✭ 105 (+320%)
Mutual labels:  decoding
website
🏡 Gleam's website and guide
Stars: ✭ 35 (+40%)
Mutual labels:  gleam
vorbis aotuv
"aoTuV" is library for encoding and decoding of OggVorbis
Stars: ✭ 35 (+40%)
Mutual labels:  decoding
Symbolic-Regression
predicting equations from raw data with deep learning
Stars: ✭ 48 (+92%)
Mutual labels:  decoding
plug
🔌 A Gleam HTTP service adapter for the Plug web application interface
Stars: ✭ 25 (+0%)
Mutual labels:  gleam
morton-nd
A header-only compile-time Morton encoding / decoding library for N dimensions.
Stars: ✭ 78 (+212%)
Mutual labels:  decoding
universal-base64
Small universal base64 functions for node.js and browsers
Stars: ✭ 25 (+0%)
Mutual labels:  decoding
multibase
multi base encoding/decoding utility
Stars: ✭ 15 (-40%)
Mutual labels:  decoding
go-webp
Simple and fast webp library for golang
Stars: ✭ 91 (+264%)
Mutual labels:  decoding
stdlib
🎁 Gleam's standard library
Stars: ✭ 153 (+512%)
Mutual labels:  gleam
JSONUtilities
Easily load JSON objects and decode them into structs or classes
Stars: ✭ 57 (+128%)
Mutual labels:  decoding
DjvuNet
DjvuNet is a cross platform fully managed .NET library for working with Djvu documents which can run on Linux, macOS and Windows. Library has been written in C# and targets .NET Core v3.0 and .NET Standard v2.1 or later. We intend to provide DjVu document processing capabilities on par with DjVuLibre reference library (or even better).
Stars: ✭ 54 (+116%)
Mutual labels:  decoding

decode

Hex.pm HexDocs.pm

A Gleam library for transforming Erlang or Elixir data into Gleam data.

decode allows you to build Decoders, which can then be used to transform data from Erlang or Elixir into type-safe Gleam data!

Getting started

If you're only concerned with decoding data from one of either Elixir or Erlang, skip ahead to the relevant section, and then read the Gleam example.

Elixir

Let's say that you have an Elixir struct that you'd like to work with in your Gleam code.

%User{name: "Jose Valim", age: 25}

Remember that under the hood, Elixir structs are just maps with a __struct__ field, and module names are just atoms!

%{__struct__: Elixir.User, name: "Jose Valim", age: 25}

So you have an Elixir function that returns one of these User structs, that you'd like to use in your Gleam code.

# user.ex

@spec get_user() :: User.t()

Erlang

Let's say that you have an Erlang record that you'd like to work with in your Gleam code.

-record(user, {name :: string(), age :: non_neg_integer()}).

Remember that Erlang records are just tagged tuples under the hood!

{user, "Joe Armstrong", 35}

So you have an Erlang function that returns one of these user records.

%% user.erl

-spec get_user() -> user()

Gleam

In order to translate this Elixir or Erlang data into a format that Gleam can work with, we'll need to explain to Gleam how it maps onto Gleam types, and probably define some custom types in the process.

// user.gleam

import decode.{Decoder, decode_dynamic, atom_field, element, map2, int, string}

// First, we define the Gleam type that we'd like to transform this data into.
pub type User {
  User(
    name: String,
    age: Int
  )
};

// Second, we define a decoder that will be used to transform an Elixir User
// struct into our custom Gleam type.
pub fn ex_user_decoder() -> Decoder(User) {
  map2(
    User,
    atom_field("name", string()),
    atom_field("age", int())
  )
}

// Or an Erlang user record into our custom Gleam type.
pub fn erl_user_decoder() -> Decoder(User) {
  map2(
    User,
    element(1, string()),
    element(2, int())
  )
}

// Third, we create an external function that calls the Elixir function
// that returns a User struct.
fn external ex_external_get_user() -> Dynamic
  = "Elixir.User" "get_user"

// Or the Erlang function that returns a user record.
fn external erl_external_get_user() -> Dynamic
  = "user" "get_user"

// And finally, we write the functions that perform the decoding!
pub fn ex_create_user() -> Result(User, String) {
  ex_external_create_user()
  |> decode_dynamic(ex_user_decoder())

pub fn erl_create_user() -> Result(User, String) {
  erl_external_create_user()
  |> decode_dynamic(erl_user_decoder())
}

Installation

Add gleam_decode to the deps section of your rebar.config file.

{deps, [
    {gleam_decode, "1.7.0"}
]}.

Need help?

If you are having trouble understanding how to use this library, or find yourself dealing with a decoding problem that you don't believe is solvable with the current API, please open an issue!

Credit

Most of this library is based on the Elm language's JSON decoding library (elm/json), with some attention paid also to the community-driven "JSON extras" library (elm-community/json-extra). Thanks to them for the great ideas!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].