Skip to content

json-schema or yaml-schema to models in python, typescript, dotnet, go, and protobuf files in a single command

Notifications You must be signed in to change notification settings

LongStoryMedia/schema2code

Repository files navigation

schema2code

A tool for converting JSON schema files to type definitions in various programming languages.

Supported languages

  • Go
  • Python (with Pydantic or dataclasses)
  • TypeScript
  • C# (.NET)
  • Protocol Buffers (proto3)

Installation

Quick Install (Recommended)

Install with curl:

curl -sSL https://raw.githubusercontent.com/LongStoryMedia/schema2code/main/install.sh | bash

Or with wget:

wget -qO- https://raw.githubusercontent.com/LongStoryMedia/schema2code/main/install.sh | bash

This will:

  1. Create a .schema2code directory in your home folder
  2. Clone or download the repository
  3. Set up a virtual environment
  4. Install the package and add it to your PATH

Manual Installation

For development or manual installation:

  1. Clone the repository:
git clone https://github.com/LongStoryMedia/schema2code.git
cd schema2code
  1. Run the build script:
./build.sh

Or install directly with pip for development:

pip install -e .

Usage

python -m src.main [schema_file] --language [language] --output [output_file] [options]

Required arguments

  • schema_file: Path to the JSON or YAML schema file
  • --language, -l: Target language (go, python, typescript, csharp, dotnet, proto, protobuf)
  • --output, -o: Output file path

Optional arguments

  • --mode: create (default) or append - Whether to create a new file or append to existing
  • --no-create: Don't create the file if it doesn't exist
  • --package: Go package name or Protocol Buffer package name (default: "main")
  • --namespace: C# namespace (default: "SchemaTypes")
  • --no-pydantic: Use dataclasses instead of Pydantic for Python
  • --no-overwrite: Prevents overwriting of existing files
  • --go-package: Go package option for Protocol Buffer files

Examples

Generate Go types:

python -m src.main schema.json --language go --output models.go --package models

Generate Python types with Pydantic:

python -m src.main schema.json --language python --output models.py

Generate Python types with dataclasses:

python -m src.main schema.json --language python --output models.py --no-pydantic

Generate TypeScript interfaces:

python -m src.main schema.json --language typescript --output models.ts

Generate C# classes:

python -m src.main schema.json --language csharp --output Models.cs --namespace MyApp.Models

Generate Protocol Buffer message definitions:

python -m src.main schema.json --language proto --output message.proto --package mypackage --go-package "example/mypackage"

Append to existing file:

python -m src.main schema.json --language go --output models.go --mode append

Testing

To test the command line tool across all sample schemas and supported languages, run:

./test_command_line.sh

This script will generate code for all schemas in sample_schemas/ and output to sample_code/, reporting any errors or issues.

Contributing

Contributions are welcome! To contribute:

  1. Fork this repository and create a new branch for your feature or bugfix.
  2. Make your changes and add tests as appropriate.
  3. Run ./test_command_line.sh to ensure all code generation works as expected.
  4. Submit a pull request with a clear description of your changes.

Please follow PEP8 style for Python code and keep code generation logic modular and well-documented.

About

json-schema or yaml-schema to models in python, typescript, dotnet, go, and protobuf files in a single command

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published