refractive-ppx
所属分类:collect
开发工具:Reason
文件大小:0KB
下载次数:0
上传日期:2020-03-08 22:26:17
上 传 者:
sh-1993
说明: PPX重写器,用于生成折射透镜和选择器,
(PPX Rewriter for generating Refractive lenses and selectors,)
文件列表:
.npmignore (190, 2020-03-08)
LICENSE (1393, 2020-03-08)
Makefile (203, 2020-03-08)
bin/ (0, 2020-03-08)
bin/bin.re (404, 2020-03-08)
bin/dune (142, 2020-03-08)
copyPlatformBinaryInPlace.js (1542, 2020-03-08)
dune-project (491, 2020-03-08)
dune-workspace (74, 2020-03-08)
esy.json (990, 2020-03-08)
esy.lock/ (0, 2020-03-08)
esy.lock/index.json (28649, 2020-03-08)
esy.lock/opam/ (0, 2020-03-08)
esy.lock/opam/atd.2.0.0/ (0, 2020-03-08)
esy.lock/opam/atd.2.0.0/opam (999, 2020-03-08)
esy.lock/opam/atdgen-runtime.2.0.0/ (0, 2020-03-08)
esy.lock/opam/atdgen-runtime.2.0.0/opam (741, 2020-03-08)
esy.lock/opam/atdgen.2.0.0/ (0, 2020-03-08)
esy.lock/opam/atdgen.2.0.0/opam (1339, 2020-03-08)
esy.lock/opam/base-threads.base/ (0, 2020-03-08)
esy.lock/opam/base-threads.base/opam (156, 2020-03-08)
esy.lock/opam/base-unix.base/ (0, 2020-03-08)
esy.lock/opam/base-unix.base/opam (153, 2020-03-08)
esy.lock/opam/biniou.1.2.1/ (0, 2020-03-08)
esy.lock/opam/biniou.1.2.1/opam (1767, 2020-03-08)
esy.lock/opam/conf-m4.1/ (0, 2020-03-08)
esy.lock/opam/conf-m4.1/opam (724, 2020-03-08)
esy.lock/opam/cppo.1.6.6/ (0, 2020-03-08)
esy.lock/opam/cppo.1.6.6/opam (1184, 2020-03-08)
esy.lock/opam/dune.2.3.1/ (0, 2020-03-08)
esy.lock/opam/dune.2.3.1/opam (2119, 2020-03-08)
esy.lock/opam/easy-format.1.3.2/ (0, 2020-03-08)
esy.lock/opam/easy-format.1.3.2/opam (1708, 2020-03-08)
... ...
# Refractive PPX
[![npm version](https://badge.fury.io/js/%40tizoc%2Frefractive_ppx.svg)](https://badge.fury.io/js/%40tizoc%2Frefractive_ppx)
![pipeline](https://github.com/tizoc/refractive-ppx/workflows/refractive_ppx%20pipeline/badge.svg)
PPX Rewriter for generating [Refractive](https://github.com/tizoc/refractive) lenses and selectors.
## To enable
Install `@tizoc/refractive_ppx`:
```
npm i --save-dev @tizoc/refractive_ppx
```
Add this to your `bsconfig.json` file:
```json
"ppx-flags": [
"@tizoc/refractive_ppx/ppx.exe"
],
```
## How to use
Add a `[@refractive.derive]` annotation to a type declaration (only record types are supported):
```reason
[@refractive.derive]
type t = { ... };
```
The expansion will define two new modules:
- `Lenses` if the type name is `t`, or `Lenses` for any other name.
- `Selectors` if the type name is `t`, or `Selectors` for any other name.
The `Lenses` module will include one lense declaration for each record field, with the same name.
The `Selectors` module will include one selector declaration for each record field, with the same name. The path will be the same as the name of the field, and a lense of the same name will be used.
For recursive type definitions, the `[@refractive.derive]` has to be added to each type declaration for which lenses and selectors should be generated.
## Example
The following declaration:
```reason
module User = {
[@refractive.derive]
type t = {
id: string,
email: string,
username: string,
score: int,
};
};
```
gets expanded to:
```reason
module User = {
type t = {
id: string,
email: string,
username: string,
score: int,
};
module Lenses = {
let id =
Refractive.Lens.make(
~get=x => x.id,
~set=(newVal, x) => {...x, id: newVal},
);
let email =
Refractive.Lens.make(
~get=x => x.email,
~set=(newVal, x) => {...x, email: newVal},
);
let username =
Refractive.Lens.make(
~get=x => x.username,
~set=(newVal, x) => {...x, username: newVal},
);
let score =
Refractive.Lens.make(
~get=x => x.score,
~set=(newVal, x) => {...x, score: newVal},
);
};
module Selectors = {
let id = Refractive.Selector.make(~lens=Lenses.id, ~path=[|"id"|]);
let email = Refractive.Selector.make(~lens=Lenses.email, ~path=[|"email"|]);
let username = Refractive.Selector.make(~lens=Lenses.username, ~path=[|"username"|]);
let score = Refractive.Selector.make(~lens=Lenses.score, ~path=[|"score"|]);
};
};
```
For a type named `t`, no prefix is added to the generated modules. Otherwise, the name of the type is titlecased and used as a prefix. A type named `user` will generate modules named `UserLenses` and `UserSelectors`.
近期下载者:
相关文件:
收藏者: