Skip to content

TypeScript/JavaScript code generation #10

Description

@haumacher

Problem

msgbuf currently only generates Java classes from .proto files. For web applications using SSE (Server-Sent Events) or WebSocket protocols, the client-side TypeScript/JavaScript deserialization and dispatch code must be written by hand. This is repetitive, error-prone, and defeats the purpose of a code generator.

Use Case

A server sends SSE events defined in a .proto file. The Java server code is generated, but the TypeScript client code must be manually written:

// Currently hand-written for each message type:
function dispatchSSEEvent(json: any): void {
    switch (json._type) {
        case "PatchEvent":
            handlePatch(json.controlId, json.patch);
            break;
        case "StateEvent":
            handleState(json.controlId, json.state);
            break;
        // ... every new event type requires manual additions
    }
}

Proposed Solution

Add a TypeScript/JavaScript code generation target to msgbuf. From the same .proto file, generate:

  1. TypeScript interfaces/classes with typed properties
  2. JSON deserialization (fromJson / readFrom methods)
  3. JSON serialization (toJson / writeTo methods)
  4. Polymorphic dispatch for abstract message hierarchies (automatic type-tag based deserialization)

Example generated output:

// Generated from sse.proto
export abstract class SSEEvent {
    static fromJson(reader: JsonReader): SSEEvent { /* dispatch by _type */ }
}

export class PatchEvent extends SSEEvent {
    controlId: string;
    patch: string;

    static fromJson(reader: JsonReader): PatchEvent { ... }
    toJson(writer: JsonWriter): void { ... }
}

This would ensure client and server are always in sync and eliminate an entire class of serialization bugs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions