Skip to main content
Version: dev

Generated Code

This document explains generated code for each target language.

Fory IDL generated types are idiomatic in host languages and can be used directly as domain objects. Generated types also include to/from bytes helpers and schema modules or registration helpers, depending on the target language.

Generated schema modules are named from the schema source file, not from the package or namespace. In targets that expose the module directly in a language package or namespace, names such as AddressbookForyModule or ComplexPbForyModule let multiple IDL files target the same package or namespace without producing colliding ForyModule types.

Reference Schemas

The examples below use two real schemas:

  1. addressbook.fdl (explicit type IDs)
  2. auto_id.fdl (no explicit type IDs)

addressbook.fdl Excerpt

package addressbook;

option go_package = "github.com/myorg/myrepo/gen/addressbook;addressbook";

message Person [id=100] {
string name = 1;
int32 id = 2;

enum PhoneType [id=101] {
PHONE_TYPE_MOBILE = 0;
PHONE_TYPE_HOME = 1;
PHONE_TYPE_WORK = 2;
}

message PhoneNumber [id=102] {
string number = 1;
PhoneType phone_type = 2;
}

list<PhoneNumber> phones = 7;
Animal pet = 8;
}

message Dog [id=104] {
string name = 1;
int32 bark_volume = 2;
}

message Cat [id=105] {
string name = 1;
int32 lives = 2;
}

union Animal [id=106] {
Dog dog = 1;
Cat cat = 2;
}

message AddressBook [id=103] {
list<Person> people = 1;
map<string, Person> people_by_name = 2;
}

auto_id.fdl Excerpt

package auto_id;

enum Status {
UNKNOWN = 0;
OK = 1;
}

message Envelope {
string id = 1;

message Payload {
int32 value = 1;
}

union Detail {
Payload payload = 1;
string note = 2;
}

Payload payload = 2;
Detail detail = 3;
Status status = 4;
}

union Wrapper {
Envelope envelope = 1;
string raw = 2;
}

Cross-Language Notes

Type ID Behavior

  • Explicit [id=...] values are used directly by generated module installation or registration helpers.
  • When type IDs are omitted, generated code uses computed numeric IDs (see auto_id.* outputs).
  • If option enable_auto_type_id = false; is set, generated module installation or registration helpers use name-based APIs instead of numeric IDs.

Nested Type Shape

LanguageNested type form
JavaPerson.PhoneNumber
PythonPerson.PhoneNumber
Rustperson::PhoneNumber
C++Person::PhoneNumber
GoPerson_PhoneNumber (default)
C#Person.PhoneNumber
JavaScript/TypeScriptPerson.PhoneNumber
SwiftPerson.PhoneNumber
DartPerson_PhoneNumber
KotlinPersonPhoneNumber
ScalaPerson.PhoneNumber

Byte Helper Naming

LanguageHelpers
JavatoBytes / fromBytes
KotlintoBytes / fromBytes
ScalatoBytes / fromBytes
Pythonto_bytes / from_bytes
Rustto_bytes / from_bytes
C++to_bytes / from_bytes
GoToBytes / FromBytes
C#ToBytes / FromBytes
JavaScript/TypeScript(via fory.serialize())
SwifttoBytes / fromBytes
Dart(via fory.serialize())

Runtime References

Choose the generated-code reference for the output runtime. Generated models remain ordinary runtime-owned types and use that runtime's supported Fory serialization APIs.

RuntimeGenerated-code reference
JavaJava
PythonPython
C++C++
GoGo
RustRust
JavaScript/TypeScriptJavaScript/TypeScript
C#C#
SwiftSwift
DartDart
ScalaScala
KotlinKotlin