codegen

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package codegen reads a schema-registry snapshot and emits typed Go and TypeScript bindings for each registered ChannelPattern.

The output supports two use cases:

  • Go publishers and subscribers that want strongly-typed payloads via client.OnAspectTyped[T any] without writing the structs by hand.
  • TypeScript browser clients that want compile-time guarantees on envelope payloads and channel names.

The generator is deliberately small: a hand-written walker over schema.JSONSchema (the subset already shipped in the schema package) driven by a tiny template. It does not pull a full JSON-Schema compiler — anything richer than the supported subset is emitted as json.RawMessage / unknown so the consumer can extend later without fork.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generators

func Generators() map[string]Generator

Generators returns the registered generators by name. Adding a new language means registering a new Generator implementation here.

Types

type Generator

type Generator interface {
	Name() string
	Generate(snap schema.Snapshot, opts Options) ([]byte, error)
}

Generator is the language-specific code emitter. Implementations produce a single self-contained file.

type GoGenerator

type GoGenerator struct{}

GoGenerator emits a single self-contained Go file.

func (GoGenerator) Generate

func (g GoGenerator) Generate(snap schema.Snapshot, opts Options) ([]byte, error)

Generate implements Generator. The output is gofmt'd; if the buffer fails to parse, the unformatted bytes are returned together with the format error so callers can diagnose template bugs.

func (GoGenerator) Name

func (GoGenerator) Name() string

Name implements Generator.

type Options

type Options struct {
	// Package is the Go package name to emit. Defaults to "parsecgen".
	// Ignored when the target language is TypeScript.
	Package string

	// Header is prepended verbatim above the generator's own header.
	// Use for project-specific build tags or license stanzas.
	Header string
}

Options controls a generation run.

type Source

type Source struct {
	URL    string
	File   string
	Client *http.Client // optional override; defaults to http.DefaultClient with a 30s timeout
}

Source is where the generator loads a snapshot from.

Exactly one of URL or File must be set. The URL form fetches the JSON returned by schema.Handler at the configured prefix; the File form reads a snapshot previously written to disk (typically by piping the HTTP response through `jq` and committing it).

func (Source) Load

func (s Source) Load() (schema.Snapshot, error)

Load returns the snapshot described by s.

type TSGenerator

type TSGenerator struct{}

TSGenerator emits a single self-contained TypeScript file.

func (TSGenerator) Generate

func (g TSGenerator) Generate(snap schema.Snapshot, opts Options) ([]byte, error)

Generate implements Generator.

func (TSGenerator) Name

func (TSGenerator) Name() string

Name implements Generator.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL