Giter VIP home page Giter VIP logo

ppx_spice's Introduction

Spice

A ReScript PPX, which generates JSON (de)serializers.

Spice is originated from

  • The Spice melange in the novel, Dune
  • A flavor for the (polymorphic) variant

This PPX is highly influenced by Decco and developed with forking the source codes of Decco. Spice has implemented all the features of [email protected] and additional useful features for the (polymorphic) variant of its own.

Motivation

  1. Parse the string instead of the array to the (polymorphic) variant

To parse the JSON data, Decco is being heavily used in many projects. But, there's a restriction to parse the JSON string into a (polymorphic) variant with the Decco. The JSON data should be formed in an array. It is obvious at some point. But generally, we face the string data which needs to be parsed into the variant in most use cases.

Whenever it is needed to parse the response in Json, the custom encoder/decoder functions are needed to parse the Json string into the (polymorphic) variant with Decco. But you don't need to write it with the Spice as long as you add @spice.as in (polymorphic) variants.

with Decco

@decco
type status = WAITING | PROCESSING | SUCCESS | FAIL

let encoderStatus = v =>
  switch v {
  | WAITING => "waiting"
  | PROCESSING => "processing"
  | SUCCESS => "success"
  | FAIL => "fail"
  }->Js.Json.string

let decoderStatus = json => {
  switch json |> Js.Json.classify {
  | Js.Json.JSONString(str) =>
    switch str {
    | "waiting" => WAITING->Ok
    | "processing" => PROCESSING->Ok
    | "success" => SUCCESS->Ok
    | "fail" => FAIL->Ok
    | _ => Error({Decco.path: "", message: "Expected JSONString", value: json})
    }
  | _ => Error({Decco.path: "", message: "Expected JSONString", value: json})
  }
}

let codecStatus: Decco.codec<status> = (encoderStatus, decoderStatus)


@decco
type data = {
  status: @decco.codec(codecStatus) status,
}

with Spice

@spice
type status =
  | @spice.as("waiting") WAITING
  | @spice.as("processing") PROCESSING
  | @spice.as("success") SUCCESS
  | @spice.as("fail") FAIL

@spice
type data = {
  status: status,
}
  1. Parse/stringify the Unicode string

There are many cases to parse and stringify the string data into (polymorphic) variants. Furthermore, the Unicode string needs to be handled with a variant. Currently, pattern matching is not working for the Unicode string in ReScript, the Spice is using if ... then ... else to compare the Unicode string in case of adding @spice.as attribute.

Example

  1. Variant
@spice
type t = | @spice.as(`하나`) One | @spice.as(`second`) Two

// automatically generated
let t_encode = ...

// automatically generated
let t_decode = ...

let encoded = One->t_encode // Js.Json.string(`하나`)

let decoded = Js.Json.string(`second`)->t_decode // Belt.Result.Ok(Two)
  1. Record
@spice
type t = {
  @spice.key("spice-label") label: string,
  @spice.key("spice-value") value: int,
}

let sample = Js.Dict.empty()
sample->Js.Dict.set("spice-label", Js.Json.string("sample"))
sample->Js.Dict.set("spice-value", Js.Json.number(1.0))
let sampleJson = sample->Js.Json.object_

let sampleRecord: t = {
  label: "sample",
  value: 1,
}


let encoded = sampleRecord->Records.t_encode // sampleJson

let decoded = sampleJson->Records.t_decode // Belt.Result.Ok(sampleRecord)

Getting Started

Read our Guide with examples

Install

yarn add -D @greenlabs/ppx-spice
// bsconfig.json

"bs-dependencies": [
  "@greenlabs/ppx-spice"
],
"ppx-flags": [
  ...,
  "@greenlabs/ppx-spice/ppx"
],

Development

With Dune

Make sure running the below commands in /src.

  1. Create a sandbox with opam
opam switch create spice 4.12.1
  1. Install dependencies
opam install . --deps-only
  1. Build
dune build
  1. Test

Make sure running tests in /test

cd test

(install dependencies)
yarn

(build --watch)
yarn res:clean && yarn res:watch

(run test --watch)
yarn test:watch

ppx_spice's People

Contributors

mununki avatar jsonkim avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.