Skip to content

Commit 1641274

Browse files
committed
New Feature: add support for fp-ts encoding
1 parent 824a9d5 commit 1641274

File tree

13 files changed

+696
-164
lines changed

13 files changed

+696
-164
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
**Note**: Gaps between patch versions are faulty/broken releases.
1414
**Note**: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice.
1515

16+
# 0.1.0
17+
18+
- **Breaking Change**
19+
- rename `parser.ts` to `haskell.ts` (@gcanti)
20+
- **New Feature**
21+
- add support for `fp-ts` encoding (@gcanti)
22+
1623
# 0.0.2
1724

1825
- **Breaking Change**

README.md

Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
POC: TypeScript code generation from a haskell-like syntax for ADT (algebraic data types)
1+
TypeScript code generation from a haskell-like syntax for ADT (algebraic data types)
22

33
# Installation
44

@@ -75,9 +75,11 @@ Syntax: `{ name :: type }`
7575

7676
Example
7777

78-
```ts
79-
// record ---v
80-
console.log(run('data User = User { name :: string, surname :: string }'))
78+
Source
79+
80+
```haskell
81+
-- record ---v
82+
data User = User { name :: string, surname :: string }
8183
```
8284

8385
Output
@@ -100,9 +102,11 @@ Syntax: `(type1, type2, ...types)`
100102

101103
Example
102104

103-
```ts
104-
// tuple ---v
105-
console.log(run('data Tuple2 A B = Tuple2 (A, B)'))
105+
Source
106+
107+
```haskell
108+
-- tuple ---v
109+
data Tuple2 A B = Tuple2 (A, B)
106110
```
107111

108112
Output
@@ -124,9 +128,83 @@ Syntax: `(<name> :: <constraint>)`
124128

125129
Example
126130

131+
Source
132+
133+
```haskell
134+
-- constraint ---v
135+
data Constrained (A :: string) = Fetching | GotData A
136+
```
137+
138+
Output
139+
140+
```ts
141+
export type Constrained<A extends string> =
142+
| {
143+
readonly type: 'Fetching'
144+
}
145+
| {
146+
readonly type: 'GotData'
147+
readonly value0: A
148+
}
149+
```
150+
151+
# `fp-ts` encoding
152+
153+
Example
154+
155+
Source
156+
157+
```haskell
158+
data Option A = None | Some A
159+
```
160+
161+
Output
162+
127163
```ts
128-
// constraint ---v
129-
console.log(run('data Constrained (A :: string) = Fetching | GotData A'))
164+
declare module 'fp-ts/lib/HKT' {
165+
interface URI2HKT<A> {
166+
Option: Option<A>
167+
}
168+
}
169+
170+
export const URI = 'Option'
171+
172+
export type URI = typeof URI
173+
174+
export type Option<A> = None<A> | Some<A>
175+
176+
export class None<A> {
177+
static value: Option<never> = new None()
178+
readonly _tag: 'None' = 'None'
179+
readonly _A!: A
180+
readonly _URI!: URI
181+
private constructor() {}
182+
fold<R>(onNone: R, _onSome: (value0: A) => R): R {
183+
return onNone
184+
}
185+
foldL<R>(onNone: () => R, _onSome: (value0: A) => R): R {
186+
return onNone()
187+
}
188+
}
189+
190+
export class Some<A> {
191+
readonly _tag: 'Some' = 'Some'
192+
readonly _A!: A
193+
readonly _URI!: URI
194+
constructor(readonly value0: A) {}
195+
fold<R>(_onNone: R, onSome: (value0: A) => R): R {
196+
return onSome(this.value0)
197+
}
198+
foldL<R>(_onNone: () => R, onSome: (value0: A) => R): R {
199+
return onSome(this.value0)
200+
}
201+
}
202+
203+
export const none: Option<never> = None.value
204+
205+
export function some<A>(value0: A): Option<A> {
206+
return new Some(value0)
207+
}
130208
```
131209

132210
# Options
@@ -146,13 +224,15 @@ export interface Options {
146224
* or a single object literal `tag -> handler`
147225
*/
148226
handlersStyle: { type: 'positional' } | { type: 'record'; handlersName: string }
227+
encoding: 'literal' | 'fp-ts'
149228
}
150229
151230
export const defaultOptions: Options = {
152231
tagName: 'type',
153232
foldName: 'fold',
154233
matcheeName: 'fa',
155-
handlersStyle: { type: 'positional' }
234+
handlersStyle: { type: 'positional' },
235+
encoding: 'literal'
156236
}
157237
```
158238

@@ -179,7 +259,7 @@ lenses.tagName.set('tag')(defaultOptions)
179259
- `ast` module: internal model -> TypeScript AST
180260
- `model` module: internal model
181261
- `printer` module: internal model -> TypeScript code
182-
- `parser` module: haskell-like syntax -> internal model
262+
- `haskell` module: haskell-like syntax -> internal model
183263
- `index` module: haskell-like syntax -> TypeScript code
184264

185265
# Roadmap

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fp-ts-codegen",
3-
"version": "0.0.2",
3+
"version": "0.1.0",
44
"description": "TypeScript code generation from a haskell-like syntax for ADT",
55
"files": [
66
"lib"
@@ -16,7 +16,7 @@
1616
"clean": "rimraf rm -rf lib/*",
1717
"build": "npm run clean && tsc",
1818
"dtslint": "dtslint dtslint",
19-
"mocha": "mocha -r ts-node/register test/*.ts",
19+
"mocha": "mocha -r ts-node/register \"./test/**/*.ts\" --recursive",
2020
"examples": "rimraf examples/*.ts && ts-node generate-examples.ts && ts-node examples/index.ts"
2121
},
2222
"repository": {

0 commit comments

Comments
 (0)