Skip to content

A small library that provides an opionated wrapper around Msgpax to help encode/decode Elixir Date and NaiveDateTime data

Notifications You must be signed in to change notification settings

aisrael/msgpax_helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MsgpaxHelper

Elixir CI Coverage Status

This is a small, personal library that provides a thin but opionated wrapper around Msgpax that can conveniently encode and decode Elixir Date and NaiveDateTime values.

IMPORTANT!: Version 0.1 of this library had a critical flaw that it couldn't encode NaiveDateTimes where the time component was after 18:12:15 (this would overflow a 16-bit unsigned integer). Please upgrade to version 0.2 immediately (using tag: "0.2", see below)!

Installation

Until this library is published in Hex, you can install it from Github by adding msgpax_helper to your list of dependencies in mix.exs as follows:

def deps do
  [
    {:msgpax_helper, github: "aisrael/msgpax_helper", tag: "0.2"}
  ]
end

Usage

To use this library, simply replace all calls to Msgpax.pack/Msgpax.unpack to MessagePack.pack/MessagePack.unpack:

iex> MessagePack.pack(~D[2008-08-16])
{:ok, [214, 101 | <<7, 216, 8, 16>>]}

iex> MessagePack.unpack([214, 101 | <<7, 216, 8, 16>>])
{:ok, ~D[2008-08-16]}

iex> MessagePack.pack!(~N[1879-03-14 11:30:00])
[[199, 7], 102 | <<7, 87, 3, 14, 11, 30, 0>>]

iex> MessagePack.unpack!([[199, 7], 102 | <<7, 87, 3, 14, 11, 30, 0>>])
~N[1879-03-14 11:30:00]

Encoding/Decoding Structs

Msgpax, when combined with maptu, already provides a way to conveniently pack/unpack Elixir structs.

First use @derive [{Msgpax.Packer, include_struct_field: true}] on your struct:

defmodule Foo do

  @derive [{Msgpax.Packer, include_struct_field: true}]
  defstruct foo: "bar"
end

Packing the struct now encodes the __struct__ field:

iex> MessagePack.pack!(%Foo{foo: "bar"})
[130, [170 | "__struct__"], [170 | "Elixir.Foo"], [163 | "foo"], [163 | "bar"]]

When unpacking, just pipe the result to Maptu.struct!:

iex> [130, [170 | "__struct__"], [170 | "Elixir.Foo"], [163 | "foo"], [163 | "bar"]] |> Msgpax.unpack! |> Maptu.struct!
%Foo{foo: "bar"}

About

A small library that provides an opionated wrapper around Msgpax to help encode/decode Elixir Date and NaiveDateTime data

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages