clientgen

package
v0.6.7 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2026 License: MIT Imports: 2 Imported by: 0

README

clientgen

Generator registry for language-specific client code generation.

Responsibility

This package defines the Generator interface and maintains a registry of language-specific implementations. It enables pluggable code generation for different target languages.

Architecture Role

pkg/clientgen (public API)
       │
       └── internal/clientgen (registry + interface)
               │
               ├── internal/clientgen/go (Go implementation)
               └── internal/clientgen/typescript (TypeScript stub)

The public pkg/clientgen package wraps this internal registry, providing a stable API while keeping implementation details private.

Key Components

  • Generator interface - Contract for language-specific generators
  • Register() - Called by generators in their init() functions
  • Get() / List() - Registry lookup functions
  • Config - Language-agnostic generation options (package name, ID type, etc.)

Adding a New Generator

  1. Create internal/clientgen/<language>/generate.go
  2. Implement the Generator interface
  3. Call clientgen.Register() in init()
  4. Import the package in pkg/clientgen/api.go for registration

Subpackages

  • go/ - Go code generator (implemented)
  • typescript/ - TypeScript generator (stub, not yet implemented)

Documentation

Overview

Package clientgen provides a registry of language-specific client code generators.

The generator interface supports pluggable, language-specific code generators that produce type-safe client code from authorization schemas. Generators return a file map to support languages that need multiple output files (e.g., TypeScript with separate types.ts, client.ts, index.ts).

This is an internal package used by the melange CLI. For programmatic code generation, use pkg/clientgen which provides a stable public API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func List

func List() []string

List returns all registered generator names.

func Register

func Register(g Generator)

Register adds a generator to the global registry. Generators should call this from their init() function.

Panics if a generator with the same name is already registered.

func Registered

func Registered(name string) bool

Registered returns true if a generator is registered for the given name.

Types

type Config

type Config struct {
	// Package is the package/module name for generated code.
	// For Go: package name (e.g., "authz")
	// For TypeScript: not typically used (uses exports)
	Package string

	// RelationFilter limits which relations get constants generated.
	// If empty, all relations are included.
	// Example: "can_" generates only permission relations, omitting roles.
	RelationFilter string

	// IDType specifies the type to use for object IDs in constructors.
	// For Go: "string", "int64", "uuid.UUID", etc.
	// Other languages may ignore this or use their own type mappings.
	IDType string

	// Version is the melange version used for generation.
	// Included in the generated file header for traceability.
	// Example: "v0.4.3"
	Version string

	// SourcePath is the path to the source schema file.
	// Included in the generated file header for traceability.
	// Example: "schemas/schema.fga"
	SourcePath string

	// Options holds language-specific configuration.
	// Each generator documents its supported options.
	Options map[string]any
}

Config holds language-agnostic generation options.

Each generator may interpret these options differently based on language conventions. Language-specific options can be passed via the Options map.

type Generator

type Generator interface {
	// Name returns the runtime identifier ("go", "typescript").
	// This is used as the value for --runtime in the CLI.
	Name() string

	// Generate returns a map of filename -> content for all generated files.
	// For single-file outputs (like Go), returns a single entry.
	// For multi-file outputs (like TypeScript), returns multiple entries.
	//
	// The filenames are relative paths (e.g., "schema_gen.go" or "types.ts").
	// The caller is responsible for writing these to the appropriate location.
	Generate(types []schema.TypeDefinition, cfg *Config) (map[string][]byte, error)

	// DefaultConfig returns the default configuration for this generator.
	// This is used when the user doesn't provide explicit configuration.
	DefaultConfig() *Config
}

Generator produces language-specific client code from a schema.

Implementations should be registered via Register() in their init() function. The CLI uses the registry to dispatch generation based on the --runtime flag.

func Get

func Get(name string) Generator

Get returns the generator for the given runtime name. Returns nil if no generator is registered for that name.

Directories

Path Synopsis
Package gogen implements the Go client code generator for melange.
Package gogen implements the Go client code generator for melange.
Package typescript implements the TypeScript client code generator for melange.
Package typescript implements the TypeScript client code generator for melange.

Jump to

Keyboard shortcuts

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