Giter Site home page Giter Site logo

flowtypify's People

Contributors

rradczewski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

flowtypify's Issues

Implement type name normalization

"foo-bar-123" is a valid name for referencing a type in JSON schemata, but it's not a valid identifier in flowtype/javascript. Should be transformed to FooBar123 (or fail in a strict mode, but be allowed when transformed)

Call for Ideas: Proper parsing for $ref and $id

flowtypify creates a flowtype definition from a JSON schema. This has been highly useful to me because it gives me compile-time types for an API contract so I can build a service for an API without having to reverse-engineer the types.

TL;DR: This needs to be a proper parser and I don't know where to start.

Status quo

flowtypify can already generate types for lots of different schemas by very crudely using strings instead of properly generating an AST and generating flowtype definitions from it. While that works for simple types, it fails for complex types involving references.

The problem

The problem (illustrated by this failing testcase) is that $id and in general paths outside of #/definitions are not resolved to distinct type definitions in the current implementation.

Even worse, the current definition just parses a $ref that matches /^#\/definitions\/([^\/]+)$/ (src) and prints out the matching group as the target type and "hopes for the best".

The missing idea

The missing piece to this is probably keeping track of all the types that are to be extracted and their names ($id and their path inside the schema) so we can reference them later when transforming the schema into a flowtype definition.

It's a fundamental problem I can't seem to wrap my head around. What's something I could look into that would teach me to write such a parser? Is there some prior art in other languages that I could look at?

Superseeds Issue #1 as I feel like this is more broad.

Resolve `$id` fields to proper types

Right now, flowtypify only supports $ref pointing to #/ or everything below #/definitions/. Implementing these was easy because we know all root-level types that will ever exist in this schema right after parsing .definitions.

With $id and how it is used in the core schema, any subschema can be given a name through $id and can then be referenced from anywhere else.

This means we probably would need to rework the parser so it can create new (exported) types and resolve references to them.

json-schema-to-typescript uses json-schema-ref-parser for that - their bundled approach is interesting but only partially solves the problem.

A new parser would need to work in two steps (hooray for lexers and parsers):

  • Step 1: Parsing the schema while keeping $id and $ref fields
  • Step 2: Extract every type (and replace it with a ReferenceType, if it...
    • has a $id field
    • is referenced through $ref (be it through #/definitions or #/properties/address/type

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.