Documentation
¶
Overview ¶
Package wiregen generates TypeScript interfaces, decoders, and an SSE registry from Go struct types using go/packages + go/types + ast.Inspect. Consumers register types via the compile-time-safe TypeRef[T]() helper and invoke Generate to emit TS files.
Example ¶
package main
import (
"fmt"
"github.com/cplieger/wiregen"
"github.com/cplieger/wiregen/testdata/basic"
)
func main() {
r := wiregen.NewRegistry(
wiregen.WithValidatorsImport("./validators.js"),
wiregen.WithBusImport("./bus.js"),
)
r.PackagePaths = []string{"github.com/cplieger/wiregen/testdata/basic"}
r.Types = []wiregen.WireType{wiregen.TypeRef[basic.Address]()}
r.SSEEvents = []wiregen.SSERegEntry{
{EventType: "addr", TypeName: "Address"},
}
ts := r.GenerateTypes()
fmt.Println(ts != "")
}
Output: true
Index ¶
- type EnumDef
- type Option
- func WithBusImport(v string) Option
- func WithFilenames(types, decoders, registry, constants string) Option
- func WithHeaderComment(v string) Option
- func WithRegisterFuncName(v string) Option
- func WithRegistryFuncName(v string) Option
- func WithSelfContainedRegistry(v bool) Option
- func WithTypesImportPath(v string) Option
- func WithValidatorsImport(v string) Option
- type Registry
- type SSERegEntry
- type UnionDef
- type WireConst
- type WireType
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EnumDef ¶
type EnumDef struct{ Values []string }
EnumDef defines a named string enum with its valid values.
type Option ¶
type Option func(*options)
Option configures optional behavior knobs on a Registry.
func WithBusImport ¶
WithBusImport sets the import path for the SSE bus module.
func WithFilenames ¶
WithFilenames overrides the output filenames for generated files.
func WithHeaderComment ¶
WithHeaderComment sets the header comment prepended to every generated file.
func WithRegisterFuncName ¶
WithRegisterFuncName sets the function name imported from the bus module.
func WithRegistryFuncName ¶
WithRegistryFuncName sets the exported function name in the registry file.
func WithSelfContainedRegistry ¶
WithSelfContainedRegistry enables self-contained registry mode.
func WithTypesImportPath ¶
WithTypesImportPath sets the import path used in decoders to reference types.
func WithValidatorsImport ¶
WithValidatorsImport sets the import path for the validators module.
type Registry ¶
type Registry struct {
Enums map[string]EnumDef
EnumTSName map[string]string
TSNameOverride map[string]string
PathNameOverride map[string]string
TypeMappings map[string]string
DecoderMappings map[string]string
DiscriminatorMap map[string]map[string]string
ValidatorsImport string
TypesFilename string
ConstantsFilename string
RegistryFilename string
DecodersFilename string
BusImport string
TypesImportPath string
HeaderComment string
RegisterFuncName string
RegistryFuncName string
Types []WireType
PackagePaths []string
Constants []WireConst
SSEEvents []SSERegEntry
SelfContainedRegistry bool
// contains filtered or unexported fields
}
Registry holds all type registrations for code generation.
func NewRegistry ¶
NewRegistry creates a Registry with the given functional options applied.
func (*Registry) Generate ¶
Generate writes generated TS files to outDir using the AST engine.
Example ¶
ExampleRegistry_Generate demonstrates basic wiregen usage.
package main
import (
"fmt"
"github.com/cplieger/wiregen"
"github.com/cplieger/wiregen/testdata/basic"
)
func main() {
r := wiregen.NewRegistry(
wiregen.WithValidatorsImport("./validators.js"),
wiregen.WithBusImport("./bus.js"),
)
r.PackagePaths = []string{"github.com/cplieger/wiregen/testdata/basic"}
r.Types = []wiregen.WireType{
wiregen.TypeRef[basic.Address](),
}
fmt.Println(r.GenerateTypes() != "")
}
Output: true
func (*Registry) GenerateConstants ¶
GenerateConstants returns the constants.gen.ts content as a string.
func (*Registry) GenerateDecoders ¶
GenerateDecoders returns the decoders.gen.ts content as a string.
func (*Registry) GenerateRegistry ¶
GenerateRegistry returns the registry.gen.ts content as a string.
func (*Registry) GenerateTypes ¶
GenerateTypes returns the types.gen.ts content as a string.
func (*Registry) GenerateValidators ¶ added in v1.2.0
GenerateValidators returns the opt-in validators starter module as a string: the reference implementation of the "Validators contract" (the 11 helper functions the generated decoders import — asObject, asArray, reqStr, reqNum, reqBool, optStr, optNum, optBool, reqOneOf, decodeArray, decodeRecord — plus the Decoder<T> type alias).
Unlike the other Generate* methods, the content is constant (it does not depend on the registered types) and the module carries a distinct "copy once, then own it" banner instead of r.HeaderComment — it is a one-time scaffold a NEW consumer copies once and then OWNS and edits freely. It is never regenerated and is deliberately NOT part of Generate's default writes, so an existing consumer's hand-edited copy is never clobbered.
type SSERegEntry ¶
SSERegEntry maps an SSE event type to a registered struct name.