clientgen

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 6 Imported by: 1

README

clientgen

Package clientgen provides a public API for generating type-safe client code from authorization schemas.

This package wraps the internal generator registry, providing a stable public interface for programmatic code generation. For CLI usage, see the melange command: melange generate client --runtime go.

Package Responsibilities

  • Generate type-safe constants for object types and relations
  • Create constructor functions for melange.Object values
  • Emit the exact names of the accessible functions a migration installs, so application code never builds them (long names are truncated and hashed)
  • Support multiple language runtimes (Go, TypeScript)
  • Provide configurable code generation options

Public API

Functions
// Generate produces client code for the specified runtime.
// Returns a map of filename -> content.
func Generate(runtime string, types []TypeDefinition, cfg *Config) (map[string][]byte, error)

// ListRuntimes returns all registered runtime names.
func ListRuntimes() []string

// Registered returns true if a generator exists for the given runtime name.
func Registered(name string) bool

// DefaultConfig returns sensible defaults for code generation.
func DefaultConfig() *Config
Types
// Config controls code generation behavior.
type Config struct {
    Package        string                     // Package name for generated code
    Exposure       *compiler.Exposure         // SQL API exposure the database is migrated with (nil: default)
    Relations      *compiler.RelationPatterns // Relations that get constants (nil: follow Exposure)
    RelationFilter string                     // Legacy prefix filter applied after them
    IDType         string                     // Type for object IDs (default: "string")
    Version        string                     // melange version for the header
    SourcePath     string                     // Schema path for the header
    Options        map[string]any             // Runtime-specific options
}

Exposure is the sql: section of melange.yaml (or migrator.MigrateOptions.Exposure). The accessible names are those a migration with it installs for the selected relations, so pass the exposure the database is migrated with. A Config that sets neither field generates every relation and every accessible function.

Supported Runtimes

Runtime Output
go schema_gen.go: constants, constructors and accessible function name constants
typescript types.ts, schema.ts, sql.ts, index.ts: constants, factories and the name maps

Usage Examples

Generate Go Client
import (
    "github.com/pthm/melange/pkg/parser"
    "github.com/pthm/melange/pkg/clientgen"
)

// Parse schema
types, err := parser.ParseSchema("schema.fga")
if err != nil {
    log.Fatal(err)
}

// Generate with defaults
files, err := clientgen.Generate("go", types, nil)
if err != nil {
    log.Fatal(err)
}

// Write generated files
for filename, content := range files {
    if err := os.WriteFile(filename, content, 0644); err != nil {
        log.Fatal(err)
    }
}
Custom Configuration
cfg := &clientgen.Config{
    Package:  "authz",  // Package name
    IDType:   "string", // ID type
    // The exposure migrate applies: only can_* relations
    Exposure: &compiler.Exposure{Relations: compiler.RelationPatterns{Include: []string{"can_*"}}},
}

files, err := clientgen.Generate("go", types, cfg)
Check Available Runtimes
fmt.Println("Available runtimes:", clientgen.ListRuntimes())
// Output: Available runtimes: [go typescript]

if clientgen.Registered("go") {
    fmt.Println("Go generator is available")
}

Generated Code Example

For this schema:

model
  schema 1.1

type user

type repository
  relations
    define owner: [user]
    define viewer: [user] or owner

The Go generator produces:

package authz

import "github.com/pthm/melange/melange"

// ObjectType constants from schema.
const (
    TypeRepository melange.ObjectType = "repository"
    TypeUser       melange.ObjectType = "user"
)

// Relations
const (
    RelOwner  melange.Relation = "owner"
    RelViewer melange.Relation = "viewer"
)

// Constructors
func User(id string) melange.Object {
    return melange.Object{Type: TypeUser, ID: id}
}

func Repository(id string) melange.Object {
    return melange.Object{Type: TypeRepository, ID: id}
}

// Wildcard constructors for public access patterns.
func AnyRepository() melange.Object { return melange.Object{Type: TypeRepository, ID: "*"} }
func AnyUser() melange.Object       { return melange.Object{Type: TypeUser, ID: "*"} }

// Accessible functions, as installed
const (
    SQLAccessibleRepositoryOwner  = "accessible_repository_owner"
    SQLAccessibleRepositoryViewer = "accessible_repository_viewer"
)

func SQLAccessibleFunction(objectType melange.ObjectType, relation melange.Relation) (string, bool) { ... }
Using Generated Code
import (
    "myapp/internal/authz"
    "github.com/pthm/melange/melange"
)

checker := melange.NewChecker(db)

// Type-safe permission check
allowed, err := checker.Check(ctx,
    authz.User("123"),
    authz.RelViewer,
    authz.Repository("456"),
)

CLI Usage

Generate code via the melange CLI:

# Generate Go client code (writes internal/authz/schema_gen.go)
melange generate client --runtime go --output internal/authz/

# Generate with custom package
melange generate client --runtime go --package authz --output .

# Generate TypeScript (a directory: types.ts, schema.ts, sql.ts, index.ts)
melange generate client --runtime typescript --output src/authz/

Integration with Build

Add to your go:generate directives:

//go:generate melange generate client --runtime go --output .

Or in a Makefile:

generate:
	melange generate client --runtime go --output internal/authz/

Deprecated API

The following are provided for backwards compatibility:

// Deprecated: Use Generate("go", types, cfg) instead.
func GenerateGo(w io.Writer, types []TypeDefinition, cfg *GenerateConfig) error

// Deprecated: Use Config instead.
type GenerateConfig struct { ... }

// Deprecated: Use DefaultConfig instead.
func DefaultGenerateConfig() *GenerateConfig

Dependency Information

This package imports:

  • github.com/pthm/melange/internal/clientgen - Generator registry
  • github.com/pthm/melange/internal/clientgen/go - Go generator (via init)
  • github.com/pthm/melange/internal/clientgen/typescript - TypeScript generator (via init)
  • github.com/pthm/melange/pkg/schema - Schema types

Documentation

Overview

Package clientgen provides a public API for generating type-safe client code from authorization schemas.

This package wraps the internal generator registry, providing a stable public interface for programmatic code generation. For CLI usage, see the melange command: `melange generate client --runtime go`.

Supported Runtimes

  • "go" - Type-safe Go code with constants, constructors and the names of the installed accessible functions (one schema_gen.go file)
  • "typescript" - TypeScript constants, union types, factory functions and the accessible function name maps (types.ts, schema.ts, sql.ts, index.ts)

Relations and accessible function names

Config.Exposure is the SQL API exposure the database is migrated with (the sql section of melange.yaml, or migrator.MigrateOptions.Exposure). Relation constants are generated for the relations it exposes, or for the ones Config.Relations selects when set, and the accessible function names for the functions a migration with that exposure installs for them: the exact, unqualified names, including names truncated to PostgreSQL's 63 bytes. A nil Exposure is the default exposure, so a Config that sets neither field generates every relation and every accessible function.

Example Usage

types, _ := parser.ParseSchema("schema.fga")
files, err := clientgen.Generate("go", types, nil)
if err != nil {
    log.Fatal(err)
}
for filename, content := range files {
    os.WriteFile(filename, content, 0644)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(runtime string, types []TypeDefinition, cfg *Config) (map[string][]byte, error)

Generate produces client code for the specified runtime.

Supported runtimes: "go", "typescript"

Returns a map of filename -> content. For single-file outputs (like Go), the map contains one entry. Multi-file outputs (like TypeScript) will contain multiple entries.

Returns an error if the runtime is not registered or generation fails.

func GenerateGo deprecated

func GenerateGo(w io.Writer, types []TypeDefinition, cfg *GenerateConfig) error

GenerateGo writes type-safe Go code from a parsed OpenFGA schema.

This function is provided for backwards compatibility. New code should use Generate("go", types, cfg) instead.

Deprecated: Use Generate("go", types, cfg) instead.

func ListRuntimes

func ListRuntimes() []string

ListRuntimes returns all registered runtime names.

func Registered

func Registered(name string) bool

Registered returns true if a generator exists for the given runtime name.

Types

type Config

type Config = clientgen.Config

Config is an alias for the internal Config type. This allows users to configure code generation without importing internal packages.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns sensible defaults for code generation. Package: "authz", the default exposure (all relations and accessible functions), no relation filter, string IDs.

type GenerateConfig deprecated

type GenerateConfig struct {
	// Package name for generated code. Default: "authz"
	Package string

	// RelationPrefixFilter is a prefix filter for relation names.
	// Only relations with this prefix will have constants and accessible
	// function names generated. If empty, all relations will be generated.
	RelationPrefixFilter string

	// IDType specifies the type to use for object IDs.
	// Default: "string"
	IDType string
}

GenerateConfig is provided for backwards compatibility. New code should use Config instead.

Deprecated: Use Config instead.

func DefaultGenerateConfig deprecated

func DefaultGenerateConfig() *GenerateConfig

DefaultGenerateConfig returns sensible defaults for code generation.

Deprecated: Use DefaultConfig instead.

type TypeDefinition

type TypeDefinition = schema.TypeDefinition

TypeDefinition is an alias for schema.TypeDefinition.

Jump to

Keyboard shortcuts

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