Package: jtd-codebuild
JSON Type Definition Code Buildjtd-codebuild is a tool for generating language specific schemas and interfaces code from JSON Type Definition IDL files in either yaml or json format.This tool is built on top ofjtd-codegenso check out the documentation if you don't have a clue about JSON Type Definition.Prerequisitesjtd-codegenInstallationpipinstalljtd-codebuildUsageFirst, you need to create a configuration file namedjtd-codebuild.jsonin the root of your project. (SeeConfiguration)Then, write JSON type definition IDL files in either yaml or json format in the directory you specified in the configuration file.After all of that, you can run the command below to generate code.jtd-codebuildpath/to/the/folder/where/jtd-codebuild.json/is/locatedUsage ExampleYou can find the example project here:jtd_codebuild/tests/fixtures/example_project_1Also, you can get a sense of how this tool works by looking at the test cases:jtd_codebuild/tests/test_example_project_1.pyRequired conventionsConfigurationThe script will findjtd-codebuild.jsonwhich is the configuration file of this tooling.root-schema-pathThe path to the root schema file.
Root schema file will be the entry point of the code generation,
where every definition files will be merged into.Defaults toschema.jtd.yamldefinitions-path(alias:definitions-paths)The path to single or multiple definition files.
This directory will be recursively searched for definition files.Definition file is a file that contains a single or multiple definitions.
Checkout the documentation below for more information.Defaults to[]output-schema-pathThe path for the merged schema file converted in json format.Defaults togen/schema.jtd.jsonincludesArray of JTD package paths to include.The path should havejtd-codebuild.jsonfile in it
so that this tool can find the codebuild configuration.If you specifiy a package path,
it will reference the package's schema definitions
when generating schema file you are working on.Defaults to[]allow-duplicate-defsWhether to allow duplicate definitions.This only applies to the definitions that are not in the same jtd codebuild project.That is, definitions indefinitions-pathin each project always raises error if there are duplicate definitions.Defaults tofalsetargetsCompile targets.Defaults to[]It's a JSONRecord contains the object having following properties:language (string): The language of the target.
We essentially inject this value tojtd-codegenas target language option
which is provided as a flag which is like--{language}-out.
Available languages can be found in the documentation ofjtd-codegen.
(See:https://jsontypedef.com/)path (string): The path to the directory where the generated code will be placed.targets- Language Specific Options - Pythonuse-pydantic (boolean): Whether to use pydantic as adataclassdecorator provider.
If this is set to true, the generated code will usepydantic.dataclasses.dataclassas adataclassdecorator so that you can use pydantic's validation features.
Otherwise, the generated code will be plain python dataclasses.
Defaults tofalse.use-typeddict (boolean): Whether to useTypedDictinstead ofdataclass.
If this is set to true, the generated code will useTypedDictinstead ofdataclass. This property cannot be set to true ifuse-pydanticis set to true. Also, subscriptable option will be ignored if this is set to true.property-case (enum): The case of the property names.
This property is only effective whenuse-typeddictis set to true.
Available values aresnakeandcamel.
Defaults tosnake.subscriptable (boolean): Whether to make the generated class subscriptable.
If this is set to true, the generated class will be subscriptable so that you can access the properties of the class likeobj["property"].
Otherwise, the generated class will not be subscriptable.
Defaults tofalse.targets- Language Specific Options - TypeScripttsconfig-path (string): The path to the tsconfig file.
This will be used to compile typescript code
to javascript code and type declarations.
If you want to automatically generate
plain javascript artifact with type declarations,
you should also provide this option.Configuration ExampleExample congfiguration file is provided below. Copy it and modify it to your needs.{
  "root-schema-path": "schema.jtd.yaml",
  "definitions-path": "definitions",
  "output-schema-path": "gen/schema.jtd.json",
  "includes": ["../path/to/another/folder/that/contains/jtd-codebuild.json"],
  "targets": [
    {
      "language": "python",
      "path": "gen/python"
    },
    {
      "language": "python",
      "path": "gen/python-pydantic",
      "use-pydantic": true,
      "subscriptable": true
    },
    {
      "language": "python",
      "path": "gen/python-typeddict",
      "use-typeddict": true,
      "subscriptable": true
    },
    {
      "language": "typescript",
      "path": "gen/typescript",
      "tsconfig-path": "tsconfig.build.json"
    }
  ]
}Root Schema FileRoot schema file is the entry point of the code generation.It will be the file where every definition files will be merged into.If you don't need a rootSchematype, you can just create an empty file.Definition filesDefinition files are sharable files of which each of them contains a single or multiple definitions.Each declared keys as a root key in the definition file will be merged as a key ofdefinitionsobject in the root schema file, and those symbols can be shared across the other definition files.For example, let's say you have a definition file whose code is like below.book:properties:id:type:stringtitle:type:stringThis can be referenced in other definition files like below.user:properties:id:type:stringname:type:stringbooks:elements:ref:bookThis will be merged as a single schema like below.{"definitions":{"book":{"properties":{"id":{"type":"string"},"title":{"type":"string"}}},"user":{"properties":{"id":{"type":"string"},"name":{"type":"string"},"books":{"elements":{"ref":"book"}}}}}}Checkout more aboutrefif you don't have a clue about it.https://jsontypedef.com/docs/jtd-in-5-minutes/#ref-schemasInheritancejtd-codebuildsupports inheritance between type definitions.You can do this by usingextendskeyword like below.Person:properties:id:type:stringname:type:stringChef:extends:Personproperties:restaurant:type:stringHeadChef:extends:Chefproperties:sousChef:ref:ChefYou can also extend multiple types like below.Person:properties:id:type:stringname:type:stringMyRestaurantMixin:properties:restaurant:type:stringChef:extends:[Person,MyRestaurantMixin]Manual dependency managementSince IDL files are basically just a bunch of JSON objects,
we need to manually manage the dependency between the definition files.For example, assume you have a folder structure like the below:definitions
├── book
│   └── book.jtd.yaml
└── user
    └── user.jtd.yamlAnd assume thatbook.jtd.yamlanduser.jtd.yamlare the root definition files of each module.In this case, you need to annotate thatuser.jtd.yamldepends onbook.jtd.yamllike below.# user.jtd.yaml## Depends on:#   - book (at ../book/book.jtd.yaml)user:properties:id:type:stringname:type:stringbooks:elements:ref:bookContributingAny contribution is welcome! Check outCONTRIBUTING.mdandCODE_OF_CONDUCT.mdfor more information on how to get started.Licensejtd-codebuildis licensed under aMIT License.