codegen

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateClient

func GenerateClient(actions []ActionFunc, outputDir string) error

GenerateClient groups actions by their declaring package and writes one TypeScript file per package to outputDir/<package>.ts, plus a barrel file at outputDir/index.ts that re-exports every generated package file. Generated functions call native fetch only; no external libraries are imported.

func GenerateStreamClient added in v1.1.0

func GenerateStreamClient(streams []StreamFunc, outputDir string) error

GenerateStreamClient groups stream functions by package and writes one TypeScript file per package to outputDir/<package>.streams.ts, plus a barrel export in outputDir/streams.ts.

Each generated function returns a ready-to-use React hook that internally calls useZyraStream and connects over WebSocket to GET /__zyra/stream/{package}/{name}.

func GenerateTypeInterfaces

func GenerateTypeInterfaces(structs []StructDef, outputDir string) error

GenerateTypeInterfaces writes outputDir/types.ts containing one exported TypeScript interface per StructDef. Each field's JSON tag name is used as the TypeScript property name when present; fields tagged json:"-" are omitted, and fields tagged with omitempty are rendered as optional properties.

func MapGoTypeToTS

func MapGoTypeToTS(goType string) string

MapGoTypeToTS converts a Go type string (as produced by go/types.ExprString) into the corresponding TypeScript type.

Recognized mappings:

string                          -> string
bool                            -> boolean
int, int8..int64, uint, uint8.. -> number
float32, float64, byte, rune    -> number
error                           -> void
[]T                             -> <TS(T)>[]
*SomeStruct                     -> SomeStruct
SomeStruct / pkg.SomeStruct     -> SomeStruct
anything else                   -> unknown

Types

type ActionFunc

type ActionFunc struct {
	Name     string        // function name, e.g. "CreateUser"
	Package  string        // declaring package name, e.g. "actions"
	Params   []ActionParam // function parameters, in declaration order
	Returns  []string      // TypeScript-mapped return types, excluding a trailing error
	HasError bool          // true if the function's last return value is error
	FilePath string        // path to the source file the function was found in
}

ActionFunc describes a Go function annotated with "// +zyraaction", along with the metadata needed to generate a matching TypeScript client binding.

func ScanActions

func ScanActions(dir string) ([]ActionFunc, error)

ScanActions recursively scans every .go file under dir (skipping _test.go files, vendor, node_modules, and .git) and returns metadata for each top-level function annotated with the "// +zyraaction" directive on the comment block directly above its declaration. If dir does not exist, ScanActions returns an empty slice and no error.

type ActionParam

type ActionParam struct {
	Name   string // parameter name, e.g. "userID"
	Type   string // TypeScript-mapped type, e.g. "string"
	GoType string // original Go type, e.g. "string" or "*models.User"
}

ActionParam describes a single parameter of a scanned action function.

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator produces TypeScript artifacts (route manifests and type declarations) derived from the discovered file-based page routes.

func New

func New(cfg *config.Config) *Generator

New creates a Generator bound to the given Zyra configuration.

func (*Generator) GenerateClientTypes

func (g *Generator) GenerateClientTypes(routes []router.Route) error

GenerateClientTypes writes a TypeScript declaration file exposing strongly typed page route paths for use in the client runtime.

func (*Generator) GenerateRoutesManifest

func (g *Generator) GenerateRoutesManifest(routes []router.Route) error

GenerateRoutesManifest writes a JSON manifest describing every discovered page route, consumed by the client router at runtime.

type StreamFunc added in v1.1.0

type StreamFunc struct {
	Name     string        // e.g. "GetLiveStats"
	Package  string        // e.g. "actions"
	Params   []ActionParam // function parameters (same type as actions)
	ElemType string        // TypeScript type of the channel element, e.g. "ServerStats"
	HasError bool          // true if the last return is error
	FilePath string
}

StreamFunc describes a Go function annotated with "// +zyrastream". The function must return a receive-only channel (or bidirectional channel) plus an optional error:

func GetLiveStats(ctx context.Context) (<-chan ServerStats, error)
func WatchNotifications(userID string) (<-chan Notification, error)

func ScanStreams added in v1.1.0

func ScanStreams(dir string) ([]StreamFunc, error)

ScanStreams recursively scans every .go file under dir and returns metadata for each top-level function annotated with "// +zyrastream". Functions that do not return a channel type are skipped with a warning.

type StructDef

type StructDef struct {
	Name   string
	Fields []StructField
}

StructDef describes a Go struct that GenerateTypeInterfaces turns into a matching TypeScript interface.

type StructField

type StructField struct {
	Name    string // Go field name, e.g. "ID"
	GoType  string // Go type, e.g. "string" or "*User"
	JSONTag string // raw json tag value (without the `json:` prefix or quotes), e.g. "id,omitempty" or "-"
}

StructField describes a single field of a Go struct.

Jump to

Keyboard shortcuts

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