package melange-atdgen-codec-runtime

  1. Overview
  2. Docs
A Melange runtime for atdgen

Install

Dune Dependency

Authors

Maintainers

Sources

melange-atdgen-codec-runtime-1.0.0.tbz
sha256=bc922594a417889050d18cb53401e15f684572c8a3984a2931b74d3894789fe6
sha512=a8198aafd146217739c7e27a711c9d29c87c19dd64fcf46554714be23e45c6a6d87f8e98e5227133d3be77c6e6dadc512989f615b82009921209341553a1116b

README.md.html

melange-atdgen-codec-runtime

melange-atdgen-codec-runtime is a Melange runtime for atdgen. It is based on the json type provided by bucklescript and combinators of melange-json.

Installation

Install opam package manager.

Then:

opam pin add melange-atdgen-codec-runtime.dev git+https://github.com/ahrefs/melange-atdgen-codec-runtime.git#master

Usage

To generate ml files from atd ones, add a couple of rules to your dune file:

(rule
 (targets test_bs.ml test_bs.mli)
 (deps test.atd)
 (action
  (run atdgen -bs %{deps})))

(rule
 (targets test_t.ml test_t.mli)
 (deps test.atd)
 (action
  (run atdgen -t %{deps})))

You can see examples in the tests or the example.

To use the generated modules, you will need to include the runtime library in your project. To do so, add melange-atdgen-codec-runtime to the libraries field in your dune file:

; ...
  (libraries melange-atdgen-codec-runtime)
; ...

To write atd type definitions, please have a look at the great atd documentation.

Simple example

Reason code to query and deserialize the response of a REST API. It requires melange-fetch.

let get = (url, decode) =>
  Js.Promise.(
    Fetch.fetchWithInit(
      url,
      Fetch.RequestInit.make(~method_=Get, ()),
    )
    |> then_(Fetch.Response.json)
    |> then_(json => json |> decode |> resolve)
  );

let v: Meetup_t.events =
  get(
    "http://localhost:8000/events",
    Atdgen_codec_runtime.Decode.decode(Meetup_bs.read_events),
  );

Full example

The example directory contains a full example of a simple cli to read and write data in a JSON file.

For a complete introduction from atdgen installation to json manipulation, please refer to Getting started with atdgen and bucklescript.

OCaml

Innovation. Community. Security.