Skip to content

Commit 54e2a73

Browse files
committed
New Feature: export lenses for options management
1 parent 779c212 commit 54e2a73

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ export const defaultOptions: Options = {
118118
}
119119
```
120120

121+
## Options management
122+
123+
Example
124+
125+
```ts
126+
import { lenses, defaultOptions } from 'fp-ts-codegen/lib/ast'
127+
128+
lenses.tagName.set('tag')(defaultOptions)
129+
/*
130+
{
131+
tagName: 'tag',
132+
foldName: 'fold',
133+
matcheeName: 'fa',
134+
...
135+
}
136+
*/
137+
```
138+
121139
# Modules
122140

123141
- `ast` module: internal model -> TypeScript AST

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"homepage": "https://github.com/gcanti/fp-ts-codegen",
3131
"dependencies": {
3232
"fp-ts": "^1.13.0",
33+
"monocle-ts": "^1.5.3",
3334
"parser-ts": "^0.5.1",
3435
"typescript": "^3.2.4"
3536
},

src/ast.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Reader, reader, ask } from 'fp-ts/lib/Reader'
33
import { array, empty } from 'fp-ts/lib/Array'
44
import * as ts from 'typescript'
55
import * as M from './model'
6+
import { Lens } from 'monocle-ts'
67

78
export interface Options {
89
/** the name of the field used as tag */
@@ -19,6 +20,14 @@ export const defaultOptions: Options = {
1920
matcheeName: 'fa'
2021
}
2122

23+
const getLens = Lens.fromProp<Options>()
24+
25+
export const lenses: { [K in keyof Options]: Lens<Options, Options[K]> } = {
26+
tagName: getLens('tagName'),
27+
foldName: getLens('foldName'),
28+
matcheeName: getLens('matcheeName')
29+
}
30+
2231
export interface AST<A> extends Reader<Options, A> {}
2332

2433
const getMemberName = (m: M.Member, position: number): string => {

test/printer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as assert from 'assert'
22
import * as P from '../src/printer'
33
import * as H from './helpers'
4-
import { Options, defaultOptions } from '../src/ast'
4+
import { Options, defaultOptions, lenses } from '../src/ast'
55

66
const assertEqual = <A, B>(f: (a: A) => P.Printer<B>, a: A, expected: B, options: Options = defaultOptions) => {
77
const actual = f(a).run(options)
@@ -220,7 +220,7 @@ export function user(name: string, surname: string): User { return { type: "User
220220
it('should handle custom tag names', () => {
221221
const printer = P.print
222222
assert.strictEqual(
223-
printer(H.Option, { tagName: 'tag', foldName: 'fold', matcheeName: 'fa' }),
223+
printer(H.Option, lenses.tagName.set('tag')(defaultOptions)),
224224
`export type Option<A> = {
225225
readonly tag: "None";
226226
} | {
@@ -258,7 +258,7 @@ export function foldL<A, R>(fa: Option<A>, onNone: () => R, onSome: (value0: A)
258258
case "Some": return onSome(fa.value0);
259259
} }`
260260
],
261-
{ tagName: 'type', foldName: 'match', matcheeName: 'fa' }
261+
lenses.foldName.set('match')(defaultOptions)
262262
)
263263
})
264264

@@ -276,7 +276,7 @@ export function foldL<A, R>(fa: Option<A>, onNone: () => R, onSome: (value0: A)
276276
case "Some": return onSome(input.value0);
277277
} }`
278278
],
279-
{ tagName: 'type', foldName: 'fold', matcheeName: 'input' }
279+
lenses.matcheeName.set('input')(defaultOptions)
280280
)
281281
})
282282
})

0 commit comments

Comments
 (0)