-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ArangoDB-Community/dev
add an exception struct, fix single key-value pair objects, add some …
- Loading branch information
Showing
16 changed files
with
1,206 additions
and
802 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,32 @@ | ||
# VelocyPack | ||
|
||
Parser and Generator for [VelocyPack](https://github.com/arangodb/velocypack) | ||
An Elixir parser and generator for [VelocyPack](https://github.com/arangodb/velocypack/blob/master/VelocyPack.md) v1. | ||
|
||
The implementation is heavily inspired by [Jason](https://github.com/michalmuskala/jason) and | ||
borrows some code (specifically the Codegen module). | ||
|
||
## Installation | ||
|
||
If [available in Hex](https://hex.pm/docs/publish), the package can be installed | ||
by adding `velocy_pack` to your list of dependencies in `mix.exs`: | ||
## Examples | ||
|
||
```elixir | ||
def deps do | ||
[ | ||
{:velocy_pack, "~> 0.1.0"} | ||
] | ||
end | ||
``` | ||
iex> {:ok, vpack} = VelocyPack.encode(10.2312514) | ||
{:ok, <<27, 245, 78, 96, 149, 102, 118, 36, 64>>} | ||
iex> VelocyPack.decode(vpack) | ||
{:ok, 10.2312514} | ||
|
||
iex> vpack = VelocyPack.encode!(%{a: "a", b: %{bool: true, float: 10.2312514}}) | ||
<<11, 37, 2, 65, 97, 65, 97, 65, 98, 11, 26, 2, 68, 98, 111, 111, 108, 26, 69, 102, 108, 111, 97, 116, 27, 245, 78, 96, 149, 102, 118, 36, 64, 3, 9, 3, 7>> | ||
iex> VelocyPack.decode!(vpack) | ||
%{"a" => "a", "b" => %{"bool" => true, "float" => 10.2312514}} | ||
|
||
iex> VelocyPack.decode(<<11>>) | ||
{:error, %VelocyPack.Error{message: "unexpected sequence", dump: nil}} | ||
|
||
iex> VelocyPack.decode!(<<11>>) | ||
** (VelocyPack.Error) unexpected sequence | ||
|
||
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) | ||
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can | ||
be found at [https://hexdocs.pm/velocy_pack](https://hexdocs.pm/velocy_pack). | ||
iex> VelocyPack.decode(<<11, 823891328731>>) | ||
{:error, %VelocyPack.Error{message: "unexpected byte", dump: "<<0xDB>>"}} | ||
|
||
iex> VelocyPack.decode!(<<11, 823891328731>>) | ||
** (VelocyPack.Error) unexpected byte: <<0xDB>> | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,63 @@ | ||
defmodule VelocyPack do | ||
@moduledoc """ | ||
An Elixir parser and generator for [VelocyPack](https://github.com/arangodb/velocypack). | ||
""" | ||
@moduledoc File.read!("#{__DIR__}/../README.md") | ||
|> String.split("\n") | ||
|> Enum.drop(2) | ||
|> Enum.join("\n") | ||
|
||
@type vpack :: binary | iodata | ||
|
||
@doc """ | ||
Parses the VelocyPack value from `input` iodata. | ||
Parses the the first _VelocyPack_ value from a binary or iodata. | ||
The options parameter is reserved for future use and not used at the moment. | ||
""" | ||
def decode(input, opts \\ []), do: | ||
VelocyPack.Decoder.parse(IO.iodata_to_binary(input), opts) | ||
@spec decode(vpack) :: {:ok, term} | {:ok, {term, vpack}} | {:error, any} | ||
def decode(vpack, opts \\ []) do | ||
__MODULE__.Decoder.parse(IO.iodata_to_binary(vpack), opts) | ||
end | ||
|
||
@doc """ | ||
Parses the VelocyPack value from `input` iodata. | ||
Parses the the first _VelocyPack_ value from a binary or iodata. | ||
Similar to `decode/2` except it will unwrap the error tuple and raise | ||
Same as `decode/2` except it will unwrap the tuple and raise | ||
in case of errors. | ||
""" | ||
def decode!(input, opts \\ []) do | ||
case decode(input, opts) do | ||
@spec decode!(vpack) :: term | {term, vpack} | ||
def decode!(vpack, opts \\ []) do | ||
case decode(vpack, opts) do | ||
{:ok, value} -> value | ||
{:error, err} -> raise err | ||
{:error, reason} -> raise reason | ||
end | ||
end | ||
|
||
@doc """ | ||
Generates VelocyPack corresponding to `input`. | ||
Generates a _VelocyPack_ value as a binary corresponding to `term`. | ||
The generation is controlled by the `VelocyPack.Encoder` protocol, | ||
The generation is controlled by the `Velocy.Encoder` protocol, | ||
please refer to the module to read more on how to define the protocol | ||
for custom data types. | ||
The options parameter is reserved for future use and not used at the moment. | ||
""" | ||
def encode(input, opts \\ []) do | ||
case VelocyPack.Encode.encode(input, opts) do | ||
{:ok, result} -> {:ok, IO.iodata_to_binary(result)} | ||
{:error, error} -> {:error, error} | ||
@spec encode(term) :: {:ok, vpack} | {:error, any} | ||
def encode(term, opts \\ []) do | ||
case __MODULE__.Encode.encode(term, opts) do | ||
{:ok, vpack} -> {:ok, IO.iodata_to_binary(vpack)} | ||
{:error, reason} -> {:error, reason} | ||
end | ||
end | ||
|
||
@doc """ | ||
Generates VelocyPack corresponding to `input`. | ||
Generates a _VelocyPack_ value as a binary corresponding to `term`. | ||
Similar to `encode/2` except it will unwrap the error tuple and raise | ||
Same as `encode/2` except it will unwrap the tuple and raise | ||
in case of errors. | ||
""" | ||
def encode!(input, opts \\ []) do | ||
case encode(input, opts) do | ||
{:ok, result} -> result | ||
{:error, error} -> raise error | ||
@spec encode!(term) :: vpack | ||
def encode!(term, opts \\ []) do | ||
case encode(term, opts) do | ||
{:ok, vpack} -> vpack | ||
{:error, reason} -> raise reason | ||
end | ||
end | ||
end |
Oops, something went wrong.