npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@greenlabs/ppx-spice

v0.2.3

Published

ReScript PPX which generate JSON (de)serializer

Downloads

8,111

Readme

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.

  1. Last but not least, Spice supports the latest ReScript compilers: v10's optional field record, v11's untagged variant, and more.

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 // 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 // Ok(sampleRecord)

Getting Started

Read our Guide with examples

Install

Compatibility on the compiler versions

| Compiler | Ppx_spice | | -------- | -------------- | | v12 | >= v0.2.3 | | v11 | >= v0.2.1-rc.1 | | v10 | ~<= v0.1.15 |

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

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

If you want to set it to uncurried mode, add -uncurried.

Development

With Dune

Make sure running the below commands in /src.

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

Make sure running tests in /test

cd test

(install dependencies)
pnpm install

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

(run test --watch)
pnpm test:watch