genotype_cli 0.17.0

Genotype language CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
# Quick Genotype Language Tour

Welcome to the Genotype Programming Language guide!

Genotype is a language that allows you to define types and data structures and generate code implementing them in multiple programming languages, such as TypeScript, Rust, Python, and more.

**Note:**

This guide doesn't cover all the target language features, e.g., how the Zod mode works when generating TypeScript code, or how legacy Python type system support works.

See corresponding target language guides for more details:

- [TypeScript]typescript.md
- [Rust]rust.md
- [Python]python.md

## Source Files

Genotype source files have `.type` extension. Each file is a module that exports all its type definitions.

```type
Category: "fiction"

Book: {
  title: string,
  subtitle?: string,
}
```

**Tip:**

The convention is to use `snake_case` for source file names.

The generated code will automatically transform into idiomatic names in the target language.

## Type Aliases

Type aliases assign a type to a name. They are the basic building blocks of Genotype.

```type
Yeah: true

Greeting: {
  message: string
}
```

Type aliases are translated to the corresponding type definitions in the target languages:

**TypeScript**

```ts
export type Yeah = true;

export interface Greeting {
  message: string;
}
```

**Rust**

```rust
#[serde_literal(true)]
#[derive(Serialize, Deserialize)]
pub struct Yeah;

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Greeting {
    pub message: String,
}
```

**Python**

```python
type Yeah = Literal[True]

class Greeting(Model):
    message: str
```

**Tip:**

The convention is to use `PascalCase` for type names.

The generated code will automatically transform into idiomatic names in the target language.

### References

You can reference type aliases in other type definitions:

```type
User: {
  name: string,
  address: Address,
}

Address: {
  country: string,
  city: string,
}
```

The order of type definitions doesn't matter. You can reference types defined later in the file.

Languages that don't support forward references, such as Python, will generate code that works around this limitation.

### Inline Type Aliases

A type alias can also be defined inline, as part of another type definition:

```type
Customer: {
  email: string,
  billingAddress: BillingAddress: {
    street: string,
    city: string,
  },
}
```

## Modules

Every Genotype file is a module that exports all type aliases defined in it.

You can import types from other modules via `use` statement.

**shipment.type**

```type
use ./destination/Destination

Shipment: {
  trackingNumber: string,
  destination: Destination,
}
```

**destination.type**

```type
Destination: {
  country: string,
  city: string,
}
```

In this example `./destination` is a relative module path (with `.type` extension omitted). The path might point to nested directories, e.g., `./models/shipment`, as well as `..` for parent directories, e.g., `../../shipment`.

There are multiple ways to import types from other modules:

```type
// Use multiple types from the same module:
use ./location/{Location, PostalCode}

// Re-alias a type from a separate module:
use ./place/{StreetAddress as Place}

// Glob import from another separate module:
use ./coordinates/*

Venue: {
  location: Location,
  postalCode: PostalCode,
  latitude: Latitude,
  longitude: Longitude,
  // Inline import from its own module:
  venueLocation: ./venue/VenueLocation,
}
```

### External Modules

Genotype currently have no fully-fledged package manager nor external module system (it's [planned](https://github.com/kossnocorp/genotype/issues/161) [though](https://github.com/kossnocorp/genotype/issues/162)). But you can map certain paths to external modules per target language, e.g. to make `shared_types` available in all targets, configure using `[<target>.dependencies]` section in `genotype.toml`:

```toml
[ts]
enabled = true

[ts.dependencies]
genotype_core = "@genotype-lang/types"

[rs]
enabled = true

[rs.dependencies]
genotype_core = "genotype_core"

[py]
enabled = true

[py.dependencies]
genotype_core = "genotype_core"
```

Then you can import types from `genotype_core` in your Genotype files:

```type
use genotype_core/Node

Diagnostic: {
  kind: "error" | "warning",
  message: string,
  node: Node,
}
```

**Caution:**

Genotype doesn't guarantee that the external module availability nor type-check the imported types.

Use it with caution, until a proper package manager and external module system is implemented.

## Unions

One of the most basic yet powerful features of Genotype is the ability to define union types:

```type
Response: ResponseSuccess | ResponseError

ResponseSuccess: {
  status: "success",
  body: { user: User }
}

ResponseError: {
  status: "failure" | "timeout",
  error?: string,
}
```

In languages that support union types, such as TypeScript, it will translate into the corresponding syntax. Languages that don't, i.e., Rust, will generate an enum type with variants for each union member:

**TypeScript**

```ts
export type Response = ResponseSuccess | ResponseError;

export interface ResponseSuccess {
  status: "success";
  body: {
    user: User;
  };
}

export interface ResponseError {
  status: "failure" | "timeout";
  error?: string | undefined;
}
```

**Rust**

```rust
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct User {}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Response {
    Success(ResponseSuccess),
    Error(ResponseError),
}

#[serde_literals]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[literals(status = "success")]
pub struct ResponseSuccess {
    pub body: ResponseSuccessBody,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ResponseSuccessBody {
    pub user: User,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ResponseError {
    pub status: ResponseErrorStatus,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
}

#[serde_literals]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ResponseErrorStatus {
    #[literal("failure")]
    Failure,
    #[literal("timeout")]
    Timeout,
}
```

**Python**

```python
class ResponseSuccessBody(Model):
    user: User

class ResponseSuccess(Model):
    status: Literal["success"]
    body: ResponseSuccessBody

class ResponseError(Model):
    status: Literal["failure"] | Literal["timeout"]
    error: Optional[str] = None

type Response = ResponseSuccess | ResponseError
```

See more details on the union translation to [TypeScript](typescript.md#unions), [Rust](rust.md#unions), and [Python](python.md#unions).

## Comments and Docs

Genotype supports line and block comments:

```type
// Line comment.

/* A block comment. */

Hello: /* Inline block comment */ "world" // EOL comment.
```

Line and block comments are omitted in generated code.

### Doc Comments

Unlike regular comments, doc comments are preserved in generated code and translated into the target language's documentation format.

```type
//! Module documentation.
//!
//! Can span multiple lines.

/// Type documentation.
Member: {
  /// Field documentation.
  displayName: string,
}
```

**TypeScript**

```ts
/** @file Module documentation.
 *
 * Can span multiple lines. */

/** Type documentation. */
export interface Member {
  /** Field documentation. */
  displayName: string;
}
```

**Rust**

```rust
//! Module documentation.
//!
//! Can span multiple lines.
use serde::{Deserialize, Serialize};

/// Type documentation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Member {
    /// Field documentation.
    #[serde(rename = "displayName")]
    pub display_name: String,
}
```

**Python**

```python
"""Module documentation.

Can span multiple lines."""

from __future__ import annotations


from pydantic import Field
from genotype import Model


class Member(Model):
    """Type documentation."""

    display_name: str = Field(alias="displayName")
    """Field documentation."""
```

## Data Types

### Primitives

Genotype has several scalar types available:

- Numeric: `number`, `int`, `float` (as well as sized types, i.e., `i32`, `i64`, `f32` etc)
- Boolean: `boolean`.
- String: `string`.

```type
Player: {
  username: string,
  score: int,
  isOnline: boolean,
}
```

Same as in TypeScript, Genotype also supports literal types:

```type
Answer: 42

Yes: true
No: false

Result: {
  status: "ok"
}
```

...as well as `null`:

```type
Employee: {
  name: string,
  departmentId: int | null,
}
```

**Note:**

While `null` is usually a mistake when it comes to programming languages design, in Genotype
`null` allows to express data structures already present in the target languages.

Even JSON has `null`, so we had no choice but to have it.

See more details on the primitive type translation to [TypeScript](typescript.md#primitives), [Rust](rust.md#primitives), and [Python](python.md#primitives).

#### Branded Primitives

You can brand a primitive to create a distinct type that isn't interchangeable with the original primitive:

```type
UserId: @string

RowId: @int
```

You can brand any primitive, including booleans, sized integers, and floating-point types.

A branded primitive type is translated to the corresponding idiomatic type in the target language:

**TypeScript**

```ts
export type UserId = string & { [userIdBrand]: true };
declare const userIdBrand: unique symbol;

export type RowId = number & { [rowIdBrand]: true };
declare const rowIdBrand: unique symbol;
```

**Rust**

```rust
#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
pub struct UserId(pub String);

#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
pub struct RowId(pub i64);
```

**Python**

```python
UserId = NewType("UserId", str)

RowId = NewType("RowId", int)
```

See more details on the branded primitive translation to [TypeScript](typescript.md#branded-primitives), [Rust](rust.md#branded-primitives), and [Python](python.md#branded-primitives).

### Composite Types

#### Objects

An object type is a collection of named fields, each with its own type:

```type
Resident: {
  fullName: string,
  yearsAtAddress: int,
  address: Address,
}
```

Objects also can be nested:

```type
Event: {
  title: string,
  capacity: int,
  venue: {
    country: string,
    city: string,
  },
}
```

**Tip:**

The convention is to use `camelCase` for field names.

The generated code will automatically transform into idiomatic names in the target language.

See more details on the object translation to [TypeScript](typescript.md#objects), [Rust](rust.md#objects), and [Python](python.md#objects).

##### Optional Object Fields

An object field can be optional, which means it may or may not be present in the object:

```type
Delivery: {
  recipient: string,
  weight: int,
  address?: Address,
}
```

See more details on the optional object field translation to [TypeScript](typescript.md#optional-object-fields), [Rust](rust.md#optional-object-fields), and [Python](python.md#optional-object-fields).

##### Object Extensions

Object fields can be extended with other object types:

```type
MessageBase: {
  id: int,
  timestamp: int,
}

MessageSuccess: {
  ...MessageBase,
  status: "success",
}

MessageFailure: {
  ...MessageBase,
  status: "failure",
  error: string,
}
```

The languages that support extensions, such as TypeScript and Python, it would translate into the corresponding syntax. Languages that don't, i.e., Rust, will copy the fields from the base type into the extended type:

**TypeScript**

```ts
export interface MessageBase {
  id: number;
  timestamp: number;
}

export interface MessageSuccess extends MessageBase {
  status: "success";
}

export interface MessageFailure extends MessageBase {
  status: "failure";
  error: string;
}
```

**Rust**

```rust
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct MessageBase {
    pub id: i64,
    pub timestamp: i64,
}

#[serde_literals]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[literals(status = "success")]
pub struct MessageSuccess {
    pub id: i64,
    pub timestamp: i64,
}

#[serde_literals]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[literals(status = "failure")]
pub struct MessageFailure {
    pub id: i64,
    pub timestamp: i64,
    pub error: String,
}
```

**Python**

```python
class MessageBase(Model):
    id: int
    timestamp: int

class MessageSuccess(MessageBase, Model):
    status: Literal["success"]


class MessageFailure(MessageBase, Model):
    status: Literal["failure"]
    error: str
```

You can extend with multiple object types:

```type
AnimalMammal: { warmBlooded: boolean }
AnimalPet: { name: string }

AnimalCat: {
  ...AnimalMammal,
  ...AnimalPet,
}
```

See more details on the object extension translation to [TypeScript](typescript.md#object-extensions), [Rust](rust.md#object-extensions), and [Python](python.md#object-extensions).

#### Arrays

An array type represents a list of elements of a given type:

```type
Post: {
  title: string,
  tags: [string],
}
```

The element type can be a union, i.e., each element can have any type in the union:

```type
Values: [string | int | boolean]
```

In languages that don't allow mixed types in array-like structures, such as Rust, Genotype will generate a `Vec` of an enum type:

```rust
pub type Values = Vec<ValuesItem>;

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(untagged)]
pub enum ValuesItem {
    String(String),
    Int(i64),
    Boolean(bool),
}
```

See more details on the array translation to [TypeScript](typescript.md#arrays), [Rust](rust.md#arrays), and [Python](python.md#arrays).

#### Tuples

A tuple type represents a fixed-length list of elements of given types:

```type
Point: (float, float)
```

Tuple elements can be union types, i.e., each element can have any type in the union:

```type
Setting: (string | int, boolean)
```

See more details on the tuple translation to [TypeScript](typescript.md#tuples), [Rust](rust.md#tuples), and [Python](python.md#tuples).

#### Records

A record type represents a collection of named fields, each with its own type, but unlike objects, records can have arbitrary field names:

```type
Scores: { [int]: float }
```

A record key can be `string`, `boolean`, a numeric type, or a reference to one of these primitives. You can also use references to branded primitives:

```type
ProductId: @int

ProductNames: { [ProductId]: string }
```

A record with `string` as a key can omit the key type:

```type
AliasMap: { []: string }
```

**Caution:**

Composite type references aren't supported as record keys ([not
yet](https://github.com/kossnocorp/genotype/issues/159)).

See more details on the record translation to [TypeScript](typescript.md#records), [Rust](rust.md#records), and [Python](python.md#records).

### Special Data Types

There are several special data types available in Genotype:

#### Any Type

When you need to represent a value of any type, similar to TypeScript's `any`, you can use the `any` type:

```type
ApiResponse: {
  status: "ok" | "error",
  payload: any,
}
```

Languages that has `any` type, it will translate into the corresponding syntax. Languages that don't, i.e., Rust, will use Genotype runtime to represent it:

**TypeScript**

```ts
export interface ApiResponse {
  status: "ok" | "error";
  payload: any;
}
```

**Rust**

```rust
use genotype_runtime::Any;

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct ApiResponse {
    pub status: ApiResponseStatus,
    pub payload: Any,
}

#[serde_literals]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub enum ApiResponseStatus {
    #[literal("ok")]
    Ok,
    #[literal("error")]
    Error,
}
```

**Python**

```python
class ApiResponse(Model):
    status: Literal["ok"] | Literal["error"]
    payload: Any
```

**Caution:**

The `any` type is not meant to be used to silence type errors, like it often utilized in TypeScript. It meant to represent values of any type, such as JSON payloads, or data from untyped sources.

Use it sparingly!

See more details on the any type translation to [TypeScript](typescript.md#any-type), [Rust](rust.md#any-type), and [Python](python.md#any-type).

## Generic Types

Genotype supports basic generic types, enabling more of the type composition and reuse.

```type
Message<Payload>: {
  id: int,
  timestamp: int,
  payload: Payload,
}
```

Generics also can be used in object extensions, allowing to reuse the same base type with different payloads:

```type
Notification<Payload>: {
  notificationId: int,
  sentAt: int,
  payload: Payload,
}

NotificationText: {
  ...Notification<string>
}
```

...or even compose generic types with other generic types:

```type
Request<Payload>: {
  requestId: int,
  timeout: int,
  payload: Payload,
}

RequestReply: {
  ...Request<Reply<string>>
}

Reply<Body>: ReplySuccess<Body> | ReplyFailure

ReplyBase<Status>: {
  status: Status
}

ReplySuccess<Body>: {
  ...ReplyBase<"success">,
  body: Body
}

ReplyFailure: {
  ...ReplyBase<"failure">,
  error: string
}
```

See more details on the generic type translation to [TypeScript](typescript.md#generic-types), [Rust](rust.md#generic-types), and [Python](python.md#generic-types).

## Recursive Types

Types can refer to themselves or form cycles across definitions and modules:

```type
LinkedNode: {
  value: string,
  next?: LinkedNode,
}

Json: null | boolean | number | string | [Json] | { []: Json }
```

Targets add the indirection their type systems need. E.g., Rust boxes direct recursive fields, while TypeScript's Zod mode uses lazy schemas or object getters:

**TypeScript**

```ts
export interface LinkedNode {
  value: string;
  next?: LinkedNode | undefined;
}

export type Json = null | boolean | number | string | Array<Json> | Record<string, Json>;
```

**Rust**

```rust
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LinkedNode {
    pub value: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub next: Option<Box<LinkedNode>>,
}

#[serde_literals]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Json {
    #[literal(null)]
    Null,
    Boolean(bool),
    Number(f64),
    String(String),
    Vec(Vec<Json>),
    Map(BTreeMap<String, Json>),
}
```

**Python**

```python
type Json = Literal[None] | bool | float | str | list[Json] | dict[str, Json]

class LinkedNode(Model):
    value: str
    next: Optional[LinkedNode] = None
```

See more details on the recursive type translation to [TypeScript](typescript.md#recursive-types), [Rust](rust.md#recursive-types), and [Python](python.md#recursive-types).

## Annotations

Annotations add structured metadata to types, fields, and union members. Most annotations belong to a specific target.

For example:

```type
#[discriminator = "status"]
JobResult: JobSuccess | JobFailure

JobSuccess: {
  status: "success",
  body: string,
}

JobFailure: {
  status: "failure",
  error: string,
}

JobStatus:
  | #[variant = "Ok"] "success"
  | #[variant = "Nope"] "failure"
```

Here `discriminator` adds Python union schema metadata:

```py
class JobSuccess(Model):
    status: Literal["success"]
    body: str


class JobFailure(Model):
    status: Literal["failure"]
    error: str


type JobResult = Annotated[
    JobSuccess | JobFailure,
    Field(json_schema_extra={"discriminator": "status"}),
]

# ...
```

While `variant` customizes Rust enum variant names:

```rust
// ...

#[serde_literals]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub enum JobStatus {
    #[literal("success")]
    Ok,
    #[literal("failure")]
    Nope,
}
```

Generators ignore annotations they don't recognize.

See more details on the annotation translation to [TypeScript](typescript.md#annotations), [Rust](rust.md#annotations), and [Python](python.md#annotations).