Code Generation
In order for the messages to be usable in code,
the schemas must be first compiled to the desired programming languages via nanoc
(pronounced na-nock),
a compiler provided by NanoPack.
Download
Pre-built binaries of nanoc
are published as GitHub releases.
Put the binary in a directory that is in PATH, or add the directory it is in to PATH, then it is ready to go.
Building nanoc
nanoc
can also be built from the source code, which requires:
- Go 1.20 or higher (install link)
- Code formatter for the corresponding programming language installed because
nanoc
will use it to format the generated code- C++:
clang-format
: available as part of Clang. - Swift:
swift-format
: available here - TypeScript:
prettier
will be used vianpx
.
- C++:
Clone the nanoc
repository here:
Change into the nanoc
directory:
Then run the installation command:
This tells Go to compile the project, then move the output binary to a directory where all the Go binaries are installed in.
The nanoc
command should now be available. If not, make sure the directory to where Go installs binaries is in PATH.
To add it to PATH:
Usage
This is an overview of the nanoc
command:
Argument | Description |
---|---|
language | (Required) The programming language the generated code should be in |
factory-out | (Optional) The directory where the message factory code file should be put in |
input-files | (Required) A space-delimited list of relative/absolute paths to the schema files that should be processed |
namespace | (Optional) The namespace under which the generated code should be. Use dot notation, e.g. My.Message |
Output
nanoc
will put the generated files next to each schema file.
For example, nanoc
will put the TypeScript code for src/MySchema.yml
in src/my-schema.ts
Every nanoc
generated file will have an extension .np
right before the real file
extension: .np.ts
, .np.swift
, .np.cxx
, etc.
Example
Consider the following files in a hypothetical src
directory,
and suppose the current working directory is where the src
directory resides.
MessageA.yml
MessageB.yml
To compile the two schemas to TypeScript:
nanoc
will produce two files, message-a.np.ts
and message-b.np.ts
.
Now, the src
directory will contain the following files:
MessageA.yml
message-a.np.ts
MessageB.yml
message-b.np.ts
Casing
nanoc
will convert the casing of field names and file names to what the convention is
for the output language. For example, if the field names in the schema files are in snake_case
,
nanoc
will convert them to camelCase
when compiling to Swift or TypeScript.
In general, nanoc
follows the following table when it comes to casing:
Name\Language | TypeScript | Swift | C++ |
---|---|---|---|
Field names | camelCase | camelCase | snake_case |
File names | kebab-case | PascalCase | snake_case |
Support Library
The generated code assumes that the support library for that language is installed and reachable from the source code. Below is the list of support libraries for each programming language:
- C++:
nanopack
- Swift:
swift-nanopack
- TypeScript:
ts-nanopack