型

型の宣言#

名前 { フィールド } で宣言します。フィールドはカンマ区切りで、最後のカンマは付けても付けなくても構いません。

comat
// Built-in scalar types (custom types wrapping strings etc.; how each language generates them is decided in SPEC.md)
//   string, bool, int
//   email, phone
//   uuid   : UUID (any version)
//   uuidv4 : UUID v4
//   uuidv7 : UUID v7 (time-ordered)
//   date   : date (YYYY-MM-DD)
UserData {
    name: string,
    email: email,
    phone: phone,
    uid: uuidv4,
}

? を付けたフィールドは省略可能(キーが無くてもよい)です。T | null はnull を許す(キーは必須)で、別の意味です。

comat
User {
    name: string,
    nickname?: string,           // キーを省略できる
    deletedAt: datetime | null,  // キーは必須で、null を許す
    tags: string[],              // 配列
}

組み込みの型#

型通信上の形式補足
string / bool / intJSON の文字列 / 真偽値 / 数値
email文字列メールアドレスの形式を検証
phone文字列E.164(+819012345678)
uuid文字列バージョン不問
uuidv4 / uuidv7文字列バージョンを検証
dateYYYY-MM-DD
datetimeRFC 3339(タイムゾーン付き)
timeHH:MM:SS
int64文字列JavaScript で 2^53 を超えると精度が落ちるため
decimal文字列金額など。誤差を避ける
url文字列
bytesbase64 の文字列
json任意の JSON

言語ごとの対応#

型TypeScriptGoPython
string bool intstring boolean numberstring bool intstr bool int
email phone urlEmail Phone URLcomatype.Email Phone Url検証付きの str
uuid uuidv4 uuidv7Uuid Uuidv4 Uuidv7(string の別名)comatype.Uuid Uuidv4 Uuidv7(string を包んだ型)uuid.UUID
dateDateStr(別名)comatype.Datedatetime.date
datetimeDateTimeStr(別名)time.Timedatetime(タイムゾーン付き)
timeTimeStr(別名)comatype.Timedatetime.time
int64bigintcomatype.Int64int
decimalDecimal(文字列の別名)comatype.Decimaldecimal.Decimal
bytesUint8Array[]bytebytes
jsonunknownjson.RawMessageAny

TypeScript では別名です

uuid などは、TypeScript では string の別名です(ブランドは付けません)。{ id } に string をそのまま渡せる使いやすさを優先しました。形式の検証は、ランタイムが実行時に行います。

enum とリテラルのユニオン#

comat
enum Role {
    admin,
    member,
    guest,
}

Profile {
    role: Role,
    status: "active" | "banned",   // その場で書くリテラルのユニオン
}

制約#

書き方意味
string(1..50)文字数
int(0..150)値の範囲(片方は省略できる: int(0..) int(..150))
string(pattern: "^[0-9]{3}-[0-9]{4}$")正規表現

制約は、送る前と受け取った後の検証に使います。言語上の型は変わりません(string(1..50) も string です)。

comat
// A showcase of the type syntax (not used by any endpoint; it shows the list of types and the constraint syntax)

// Named enum (string values)
enum Role {
    admin,
    member,
    guest,
}

Profile {
    // Constraints: string(min..max), int(min..max). Either side can be omitted
    nickname: string(1..50),
    age?: int(0..150),
    // Regular expression
    zip: string(pattern: "^[0-9]{3}-[0-9]{4}$"),

    role: Role,
    // Inline literal union
    status: "active" | "banned",

    birthday?: date,
    createdAt: datetime,
    openAt: time,

    // 64-bit integers and decimals are sent as strings on the wire
    followers: int64,
    balance: decimal,

    website?: url,
    avatar?: bytes,
    // Arbitrary JSON
    extra?: json,

    // Arrays and null
    tags: string[],
    deletedAt: datetime | null,
}

オブジェクトの組み立て#

...Type はフィールドの展開、empty はフィールドを消します(後ろに書いたものが勝つ)。

comat
body {
    ...UserData,    // name, email, phone, uid が入る
    uid: empty,     // uid を消す
    password: string,
}

型の解決#

import した名前、または同じファイルで宣言した型だけが使えます。

comat
import UserData from ../types/UserData.comat

パスは、書いたファイルからの相対です。型の宣言だけを書いたファイルは、comatDir の外(types/ など)に置きます。

注意

  • 型名は、プロジェクトの中で一意にしてください(TypeScript と Python は、1 つのファイルにまとめるため)。
  • ...Type が循環している(A が B を、B が A を展開する)とエラーです。
  • Go は、T | null と文字列リテラルだけのユニオンに対応しています。それ以外のユニオン(string | int など)は、生成時にエラーになります。