GithubHelp home page GithubHelp logo

shanahanjrs / typebuf Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 61 KB

Dead simple way to create shared type definitions between applications

License: Apache License 2.0

Python 100.00%
python types typescript hacktoberfest hacktoberfest-accepted

typebuf's Introduction

TypeBuf

Downloads PyPI version CircleCI codecov Maintainability Rating Reliability Rating


Dead simple way to create shared type definitions between applications

  • Other tools have too much setup? Too much boilerplate? Do too much "magic"? Just:
    • Define your Types in types.yaml
    • Run TypeBuf: $ typebuf compile -f types.yaml -l python -l typescript

Install

pip install typebuf

poetry add --dev typebuf

Quickstart

  1. Create a file called types.yaml, it can be anywhere in your projects repo

  2. Add the following lines to the newly created file:

---
typedefs:
  - typename: User
    imports:
      python:
        - 'from typing import Optional'
    fields:
      - name: first_name
        type: string
      - name: age
        type: int
        optional: true
  1. Now call TypeBuf with: $ typebuf compile -l python -l typescript

  2. You now have two new files, one called user.py and one called user.ts that can you use for things like data serialization/deserialization, validation, subclassing, etc

Generated Python:

"""
User type definition file generated by TypeBuf
Note: Any changes made to this file will be overwritten
      during next compilation
"""

from typing import Optional


class User:
    first_name: str
    age: Optional[int]

Generated TypeScript:

/**
 * User type definition file generated by TypeBuf
 * Note: Any changes made to this file will be overwritten
 *       during next compilation
 */

interface User {
  first_name: string;
  age?: number;
}

Note: So far only Python and TypeScript are supported. More languages coming soon!

Demo

Here's a quick demo of me using TypeBuf 0.1.1. First I show the contents of the types.yaml file then I generate Python and TypeScript source code and show the contents of the new files

asciicast

Documentation

  • Inside types.yaml there is an array called typedefs. This is where you add your shared type definitions

  • Each type can have the following fields (required fields in bold):

    • typename: string
    • imports: map[string, array[str]]
    • fields: array[Field]
      • A Field has the following attributes:
        • name: string
        • type: string
          • a type can be any one of:
            • any
            • string
            • int
            • float
            • boolean
            • bytes
            • date
            • datetime
            • map
            • array
            • tuple
            • null
            • double
            • sint8
            • sint16
            • sint32
            • sint64
            • uint8
            • uint16
            • uint32
            • uint64
          • union types are also experimentally supported:
            • int | string
        • optional: boolean
  • In a type definition you can also specify custom/required imports for any depedencies your classes may need:

    • ---
      typedefs:
        - typename: Address
          imports:
            python:
              - 'from mymodule.user import User'
              - 'from typing import Optional'
            typescript:
              - 'import { User } from "./User";'
          fields:
            - ...,
      

typebuf's People

Contributors

shanahanjrs avatar

Watchers

 avatar  avatar

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.