question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Thoughts on a decoder "code generator" given a flow type.

See original GitHub issue

One pain point is the decoder is more-or-less just the flowtype rewritten. The advantage of something like https://github.com/codemix/flow-runtime is that this is done automatically, but without the flexibility of making more complex runtime decoders and/or deciding where to apply the runtime check.

What are your thoughts about a tool that would generate decoders for all exported types? For example if you have a file ./src/myModule.js and you run $ decoder-codegen ./src/, it would create a new file ./src/__generated__/myModule.decoders.js. Then we can import the decoders into the source file.

myModule.js

import { guard } from 'decoders';
import { pointDecoder } from './__generated__/myModule.decoders';

const checkPointType = guard(pointDecoder);

export type Point = {
  x: number,
  y : number
}

export function moveUp(point: Point) {
  checkPointType(point);
  point.y = point.y - 1;
}

Inspired by https://github.com/apollographql/apollo-codegen#generate

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
sfarthincommented, Oct 2, 2018

Guard satisfies the majority of my use cases and does not require understanding of decoders to use, though it would be nice in certain situations.

The lib could provide both:

import guard from "flow-guard/macro";
import createDecoder from "flow-guard/decoder-macro";

Additionally, I am thinking decoders that go beyond flow types (integer, regex, email, etc), it might make more sense to have that as an additional optional argument that would “extend” the decoder. The implementation of pulling this from comments or somewhere else would be tricky and maybe a little too magical.

import { email } from 'decoders';
import guard from "flow-guard/macro";

type Person = {
  name: string,
  email: string
}

function personGuard(person: any): Person {
  return guard<Person>(person, {
    email
  });
}

or with decoders…

import { guard, email } from 'decoders';
import createDecoder from "flow-guard/decoder-macro";

type Person = {
  name: string,
  email: string
}

const personDecoder = createDecoder<Person>({
  email
});

const personGuard = guard(personDecoder);
1reaction
sfarthincommented, Oct 1, 2018

fyi, proof of concept started: https://github.com/sfarthin/flow-guard . Might be some time translating the flow AST into decoder code. (https://github.com/sfarthin/flow-guard/blob/master/macro.js#L5)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multi-stage code generator and decoder for communication ...
The static encoder includes a redundant symbol generator that generates a plurality of redundant symbols based on the input symbols.
Read more >
Beyond Codex: A Code Generation Model That You Can Train
This post will overview codeT5, an encoder-decoder code generation model with publicly available pre-training checkpoints that you can try today ...
Read more >
Text-to-code Generation with TensorFlow, & MBPP - Kaggle
This notebook demonstrates how to generate python code based on the natural language description using TensorFlow, HuggingFace and Mostly Basic Python ...
Read more >
Downlink Baseband Decoder Implementation - DiVA Portal
A given code can only be used if there are no other codes used on the path from that given code to the...
Read more >
Designing of 2 to 4 Line Decoder - ElProCus
This article discusses how to design 2 to 4 Line Decoder circuit which takes an 2 -bit binary number and produces an output...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found