Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@greenlabs/ppx-spice

Package Overview
Dependencies
Maintainers
4
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@greenlabs/ppx-spice

ReScript PPX which generate the (polymorphic) variant (de)serializer

Source
npmnpm
Version
0.1.6-rc3
Version published
Weekly downloads
906
25.48%
Maintainers
4
Weekly downloads
 
Created
Source

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 Decco@1.5.0 and additional useful features for (polymorphic) variant of its own.

Motivation

  • 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.

  • 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

  • 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.JSONString(`하나`)

let decoded = Js.Json.string(`second`)->t_decode // Belt.Result.Ok(Two)
  • 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)

Install

yarn add -D @greenlabs/ppx-spice
// bsconfig.json
"ppx-flags": [
  ...,
  "@greenlabs/ppx-spice/ppx"
],

Development

With Esy

  • Install dependencies
cd src
esy install
  • Build
(make sure in /src directory)
esy build
  • Test
cd test

(install dependencies)
yarn

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

(run test --watch)
yarn test:watch

With Dune

  • Create a sandbox with opam
opam switch create spice 4.12.1
  • Install dependencies
opam install dune ppxlib ocaml-lsp-server ocamlformat ocp-indent
  • Build
dune build
  • Test
cd test

(install dependencies)
yarn

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

(run test --watch)
yarn test:watch

Keywords

ReScript

FAQs

Package last updated on 09 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts