Skip to main content

Dead simple way to define shared type definitions between applications

Project description

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 project 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
        • 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:
            - ...,
      

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typebuf-0.1.4.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typebuf-0.1.4-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file typebuf-0.1.4.tar.gz.

File metadata

  • Download URL: typebuf-0.1.4.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.9.2 Linux/4.4.0-19041-Microsoft

File hashes

Hashes for typebuf-0.1.4.tar.gz
Algorithm Hash digest
SHA256 28262ccbae6708294f45ec965d6547e901e02e7fab595165ade7c1d63b0a5277
MD5 e9a283149a51d275a23cbf58a4e2823e
BLAKE2b-256 475952d4beb85e718a451c79ceea1b442425926c031f052a0c884ac2cf034cf7

See more details on using hashes here.

File details

Details for the file typebuf-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: typebuf-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.9.2 Linux/4.4.0-19041-Microsoft

File hashes

Hashes for typebuf-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 28fbcd26e2eb7e481d6cbd62be26687902b93a437f682bed73e60c977f59dd44
MD5 cc4831402b4b26e5794588adf7f01fb2
BLAKE2b-256 acf100a739ee6d4ed3210f0e3c5b60afab563629cc5f16f51c4971188df36567

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page