codegen

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: Apache-2.0, MIT Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package codegen writes the files of a port module: the stubs to port (`port/port.go`), the generated cgo side (`main.go`, `wire.go`), the runtime (`goany/goany.go`) and the wrapper the source language loads (`python/<name>/__init__.py`, `index.cjs`). It is rust-anything's `codegen.rs`, `codegen_py.rs` and `codegen_ffi.rs` with Go as the target.

Every type is read from the TS-like dialect the language plugins write into the manifest and mapped to Go by the rules of rust-anything's `RType::from_ts`, with two changes Go needs: `Record<string, T>` is an insertion-ordered `*goany.Map[T]` (a Go map iterates in random order), and an optional value is a pointer.

Index

Constants

View Source
const ABIWire = "GoAnythingWire"

ABIWire is the third symbol of a port module, the binary wire:

int64_t GoAnythingWire(int64_t op, int64_t handle, const uint8_t* in, size_t len, uint8_t* out, size_t cap);
View Source
const BatchCheckDir = ".go-anything/batchcheck"

BatchCheckDir is where gate's batch check writes and runs its program, inside the port module (a folder whose name starts with a dot, which `go ... ./...` and gate's own walks leave out).

View Source
const CBridgeFile = "c_bridge.c"

CBridgeFile is the generated C file of a C port module, in build/.

View Source
const GoVersion = "1.25"

GoVersion is the `go` line of a port module's go.mod.

View Source
const KoffiVersion = "3.4.0"

KoffiVersion is the koffi the TS/JS wrapper loads the shared library with.

View Source
const SwiftDriverModule = "GoAnythingDriver"

SwiftDriverModule is the module name the driver is compiled under, removed from recorded error type names.

View Source
const WireVersion = "goany-wire/1"

WireVersion is the first line of the wire table a library answers (goany.TableText).

Variables

View Source
var Objects []byte

Objects is goany/objects.go, the handle table a port module with classes carries.

View Source
var Runtime []byte

Runtime is goany/goany.go, the runtime every port module carries.

View Source
var RustRuntime string

RustRuntime is goany.rs without its FFI half: the JSON the Rust driver and wrapper write and read, and the conversions between it and the Rust types the bridge carries. Both drivers and the wrapper carry the same file, so both sides of a case are written the same way.

View Source
var WireRuntime []byte

WireRuntime is goany/wire.go, the binary wire and the batches' runtime.

View Source
var Wrappers = core.NewRegistry[Wrapper](core.StepWrapper, func(l core.Lang, have []core.Lang) string {
	return "`bridge` has no wrapper for " + l.DisplayName() + " yet: it bridges " + core.LangList(have)
})

Wrappers are the registered languages' Wrapper parts.

Functions

func BatchCheckGo

func BatchCheckGo(m *core.Manifest) ([]byte, error)

BatchCheckGo is the program gate's batch check runs inside the port module (rust-anything's `batch_check`, which generates a cargo test). It reads jobs as JSON on standard input, one per group of items that share the other arguments:

{"batch": i, "items": [..], "fixed": [..], "fresh": bool}

builds the batch the library builds (batch.New<Fn>Batch, over the default index or the port's own), runs it, checks its rank against the run's results, and for a fresh job also calls the port once per item and compares. It writes one answer per job:

{"decoded": [bool per item], "run": {"ok": [..]} | {"error": text} | {"panic": text},
 "rank_checks": n, "rank_bad": [text, ..], "fresh_bad": [text, ..], "skipped": text}

Results are written as the record driver of the source language writes them (a Python float with an integral value is {"$float": repr}), so gate compares them with the recording as diff does.

func BatchGo

func BatchGo(m *core.Manifest) ([]byte, error)

BatchGo is batch/batch.go, the generated package that runs each batch: a typed class over the default index or over the port's own. The library's wire calls it, and so does gate's batch check, so what is checked is what callers run.

func BytesType

func BytesType(lang core.Lang) string

BytesType is the dialect type of Go's []byte for lang, "" when it has none.

func CBridge

func CBridge(m *core.Manifest, root string) (map[string][]byte, error)

CBridge is the C library's header and bridge source, by file name.

func CDecl

func CDecl(cType, name string) string

CDecl declares name with a C type of the prototypes (`const char*` and `s` give `const char *s`).

func CHeader

func CHeader(m *core.Manifest) string

CHeader is the header a C caller includes: the original's prototypes.

func CSignatureNamed

func CSignatureNamed(p clang.Proto, name string) string

CSignatureNamed is the prototype as the original declares it, typedefs resolved, under name.

func CUnit

func CUnit(m *core.Manifest) string

CUnit is the C library's name: the module's without `_go` (`buf` for `buf_go`), so the port is `libbuf.dylib`, as the original's library would be.

func FindIndex

func FindIndex(portSrc []byte, p BatchPlan) (index string, ranks bool, err error)

FindIndex reads port.go for the index a batch's port may define (FuzzyScoreIndex), and checks its signatures: New<Index>(items []T) *<Index>, (*<Index>).Run(<other parameters>) ([]R, error) and, optionally, (*<Index>).Rank(<other parameters>, min float64) ([]uint32, error). It returns "" when port.go defines none.

func GeneratedGoFiles

func GeneratedGoFiles(m *core.Manifest) (map[string][]byte, error)

GeneratedGoFiles are the Go files of a port module that `bridge` writes and nobody edits, by path relative to the module, with their exact contents: the C entry points, the wire functions and the runtime. `bridge` writes exactly these, and `gate` leaves exactly these out of the checks about the port's own code (and refuses a file that has the same name and other contents), so there is one list and it cannot drift. port/port.go is not among them.

func GoExported

func GoExported(name string) string

GoExported is an export's Go name: `word_counts` and `wordCounts` are WordCounts, the rest of each word kept as written (`parseHTTP` is ParseHTTP); a name that would not start with a letter gets an X. It is the rule gate's `api` check looks the stubs up by (gate.GoName).

func GoLocal

func GoLocal(name string) string

GoLocal is a parameter's Go name: `max_length` is maxLength; a Go keyword gets a `_`.

func GoMod

func GoMod(m *core.Manifest) []byte

GoMod is the port module's go.mod.

func GoType

func GoType(t Type) string

GoType is t as written in port.go.

func HasLiteralDefault

func HasLiteralDefault(p core.Param) bool

HasLiteralDefault reports a parameter whose default the wrapper can write as it is: a boolean, a number or a plain quoted string (rust-anything's `has_literal_default`).

func IndexCJS

func IndexCJS(m *core.Manifest) ([]byte, error)

IndexCJS is index.cjs: the original's functions with the original's parameters, each calling the Go port through koffi, writing changed arguments back into the caller's objects, printing what the port printed, and throwing the original's error class from the "Class: message" text.

func LibName

func LibName(m *core.Manifest) string

LibName is the module's name as Go, Python and the shared library spell it: `textkit-go` becomes `textkit_go`.

func MainGo

func MainGo(m *core.Manifest) ([]byte, error)

MainGo is main.go: the two C symbols of the ABI.

func NothingType

func NothingType(lang core.Lang) string

NothingType is the dialect type of a function of lang that returns nothing.

func PHPCodec

func PHPCodec() string

PHPCodec is the namespace that holds the value codec: the wrapper and the driver both carry it.

func PHPTargets

func PHPTargets(m *core.Manifest, root string) (map[string]string, error)

PHPTargets maps each export's name to the callable the driver and the wrapper name: `Acme\Text\slugify` or `Acme\Text\Words::count`. It reads the original's files for where each export is declared.

func PHPWrapper

func PHPWrapper(m *core.Manifest, root string) (string, error)

PHPWrapper is php/<name>.php.

func PHPWrapperPath

func PHPWrapperPath(m *core.Manifest) string

PHPWrapperPath is the wrapper's file, relative to the port module.

func PackageJSON

func PackageJSON(m *core.Manifest) []byte

PackageJSON is the port module's package.json: index.cjs and its one dependency.

func ParseBatch

func ParseBatch(spec string) (core.Batch, error)

ParseBatch reads a `--batch function:param` spec.

func PortGo

func PortGo(m *core.Manifest) ([]byte, error)

PortGo is port/port.go: one stub per export, typed from the manifest, each panicking with core.StubMarker until it is ported. The only file of the module a person or agent writes.

func PythonInit

func PythonInit(m *core.Manifest, root string) ([]byte, error)

PythonInit is python/<name>/__init__.py: the original's functions with the original's signatures (positional and keyword parameters, literal defaults), each calling the Go port through ctypes, writing changed arguments back into the caller's objects, printing what the port printed, and raising the original's exception class from the "Class: message" text.

func PythonModule

func PythonModule(root, file string) string

PythonModule is the dotted name a Python source file is imported by: its folder climbs while it holds an `__init__.py`, as the record driver's `import_root` does.

func PythonPackage

func PythonPackage(m *core.Manifest) string

PythonPackage is the folder of the Python wrapper, relative to the port module.

func RubyNative

func RubyNative(m *core.Manifest) string

RubyNative is the module of the wrapper that calls the library: `GoAnythingNativeTextkitGo`.

func RubyReceivers

func RubyReceivers(m *core.Manifest, root string) (map[string]string, error)

RubyReceivers are the module each export of the manifest is a function of, read again from the original's files (the manifest keeps the file and line, not the module).

func RubyWrapper

func RubyWrapper(m *core.Manifest, root string) ([]byte, error)

RubyWrapper is ruby/<name>.rb.

func RubyWrapperFile

func RubyWrapperFile(m *core.Manifest) string

RubyWrapperFile is the wrapper's path in the port module.

func RustArg

func RustArg(t *rust.Type, v string) string

RustArg is how an argument read into its owned type is passed: borrowed to a `&` parameter.

func RustOwned

func RustOwned(t *rust.Type) string

RustOwned is the owned type an argument is read into: `String` for `&str`, `Vec<T>` for `&[T]`, the type itself without its `&` otherwise.

func RustResult

func RustResult(t *rust.Type) (ok string, isResult bool)

RustResult is what a call answers on success, and whether the function returns a Result, whose Err is the error path.

func SplitTop

func SplitTop(ts string, sep rune) []string

SplitTop splits on sep outside brackets; the `>` of an arrow (`=>`) closes nothing.

func SwiftCamel

func SwiftCamel(m *core.Manifest) string

SwiftCamel is the port module's library name in UpperCamelCase: `textkit_go` is `TextkitGo`.

func SwiftDriverSource

func SwiftDriverSource(m *core.Manifest, root string, port bool) (string, error)

SwiftDriverSource is SwiftDriver for the manifest of the original at root.

func SwiftErrorType

func SwiftErrorType(m *core.Manifest) string

SwiftErrorType is the error type the wrapper throws for a port's `error: "Type: message"`.

func SwiftModuleFiles

func SwiftModuleFiles(m *core.Manifest) map[string]string

SwiftModuleFiles are `swift/<Native>/module.modulemap` and its header, by path.

func SwiftNativeModule

func SwiftNativeModule(m *core.Manifest) string

SwiftNativeModule is the Clang module the wrapper imports: the C declarations of the entry point, and the library to link.

func SwiftWrapperPath

func SwiftWrapperPath(m *core.Manifest) string

SwiftWrapperPath is the wrapper's path in the port module: `swift/TextkitGo.swift`.

func WireGo

func WireGo(m *core.Manifest) ([]byte, error)

WireGo is wire.go: per export, decode the arguments, call the port, encode the answer.

func WireTableText

func WireTableText(m *core.Manifest) (string, error)

WireTableText is what the library built from m answers to OpTable (goany.TableText): the wrappers compare it with what they were generated with before they call anything by index.

func Wired

func Wired(lang core.Lang, f Func) bool

Wired reports whether a function crosses the binary wire, as rust-anything's `wire_fns` decides: synchronous, with no effects (a function that changes its arguments or prints answers with more than a result), no rest parameter, and types the wire carries in the source language.

func WrapperFiles

func WrapperFiles(m *core.Manifest, root string, initial bool) (map[string][]byte, error)

WrapperFiles are the wrapper's files for the manifest's language, or the error naming the language and the step.

Types

type BatchPlan

type BatchPlan struct {
	Batch core.Batch
	Func  Func
	Item  int    // the batched parameter, an index into Func.Params
	Class string // FuzzyScoreBatch: the class callers construct
	Base  string // FuzzyScore
	// Index is the port's own index type (FuzzyScoreIndex) when the manifest records one, else "".
	Index string
	// IndexRanks says the port's index has its own Rank.
	IndexRanks bool
}

BatchPlan is one batch the port's signature allows.

func PlanBatches

func PlanBatches(m *core.Manifest) ([]BatchPlan, error)

PlanBatches checks every batch of the manifest against the functions, with rust-anything's reasons for what cannot be batched.

func (BatchPlan) Fixed

func (p BatchPlan) Fixed() []Param

Fixed are the parameters other than the items, in order.

func (BatchPlan) IndexName

func (p BatchPlan) IndexName() string

IndexName is the type a port defines to prepare the items once: FuzzyScoreIndex.

func (BatchPlan) ItemType

func (p BatchPlan) ItemType() Type

ItemType is the type of one item.

func (BatchPlan) Ranks

func (p BatchPlan) Ranks() bool

Ranks says the batch has rank: its results are numbers.

type Class

type Class struct {
	Class   core.Class
	GoName  string // the Go type, e.g. TextStats
	NewName string // its constructor, e.g. NewTextStats
	Recv    string // the receiver's name in port.go, e.g. t
	Ctor    Func   // the constructor's parameters, GoName NewName; Ret is unused
	Methods []Func // GoName is the Go method name
}

Class is one class of the unit as the generated code sees it: a Go type in port.go with a constructor `New<Type>` and one method per method of the original. Its objects live in Go; the caller's wrapper holds each by a handle (docs/wire.md).

func PlanClasses

func PlanClasses(m *core.Manifest) ([]Class, error)

PlanClasses maps every class of the manifest to its Go type, refusing names that collide with each other or with the functions, and methods the generated side cannot carry: a method that changes an argument or prints is recorded by its results only, so it cannot be checked.

type Func

type Func struct {
	Export core.Export
	GoName string // the exported Go function in port.go
	Params []Param
	Ret    Type
	Prints bool // takes `stdout *strings.Builder`
}

Func is one export as the generated code sees it.

func Plan

func Plan(m *core.Manifest) ([]Func, error)

Plan maps every export of the manifest to its Go function, refusing two exports that would become one Go name (`fooBar` and `foo_bar`).

func WiredFuncs

func WiredFuncs(m *core.Manifest) ([]Func, error)

WiredFuncs are the functions of the manifest that cross the binary wire, in manifest order: a function's index on the wire is its position here.

type Kind

type Kind int

Kind is what a dialect type is to the port.

const (
	Str   Kind = iota // string
	Float             // number: float64
	Int               // int: int64
	Bool              // boolean
	Bytes             // pybytes: []byte
	Path              // pypath: string, {"$path": ..} on the wire
	Unit              // void, undefined, None: no value
	Slice             // T[]: []T
	Opt               // T | undefined, T | null, x?: T: *T
	Map               // Record<string, T>: *goany.Map[T]
	JSON              // anything structured: goany.Value
)

type Param

type Param struct {
	core.Param
	GoName  string
	Type    Type
	Mutated bool // the original changes it in place: a pointer in port.go
}

Param is one parameter of a Func.

func (Param) GoParamType

func (p Param) GoParamType() string

GoParamType is the parameter's type in port.go.

type RustUnit

type RustUnit struct {
	Crate *rust.Crate
	// Path is the module path code outside the crate calls the functions by (`text`), empty
	// for the library's root file.
	Path string
	// Funcs are the exports' Rust signatures, in the manifest's order.
	Funcs []rust.Func
}

RustUnit is what the Rust wrapper and driver need of the original: its crate, the module path of the unit's file, and the Rust signature of each export.

func RustUnitOf

func RustUnitOf(m *core.Manifest, root string) (*RustUnit, error)

RustUnitOf reads the unit's file again for the Rust signatures: the manifest keeps only the dialect types.

type Type

type Type struct {
	Kind Kind
	Elem *Type // Slice, Opt, Map
	// For Opt: the union named null and not undefined, so a JS caller gets null for nil.
	Null bool
}

Type is a dialect type as the port sees it.

func ParamType

func ParamType(p core.Param) Type

ParamType is a parameter's type for the port: a rest parameter is a slice, an optional one is an Opt (undefined when left out), unless its default is a literal the wrapper fills in, in which case the port always gets a value (rust-anything's `port_param_type`). A Record is a goany.Value here: `record` hands a structured parameter values of other shapes too, as a caller may, and the port has to answer what the original answers to them.

func ParseType

func ParseType(ts string) Type

ParseType reads a dialect type, as `RType::from_ts` does.

func (Type) ContainsJSON

func (t Type) ContainsJSON() bool

ContainsJSON reports a JSON value anywhere in t.

func (Type) String

func (t Type) String() string

type Wrapper

type Wrapper interface {
	// Files are the wrapper's files, by path relative to the port module, for a manifest whose
	// original is at root. initial is true for `bridge`, false for `refresh`, which writes again
	// only what follows the signatures (index.cjs, not the package.json that pins koffi).
	Files(m *core.Manifest, root string, initial bool) (map[string][]byte, error)
	// NoneIsNull is true when a Go nil crosses back as null in every case (Python's None);
	// false when it is undefined unless the type said null (JS).
	NoneIsNull() bool
	// WireBytes is true when the binary wire carries bytes and paths to and from the language.
	WireBytes() bool
	// FloatNumbers is true when the language's numbers are floats (JS): an int64 crosses the
	// binary wire as a float, and a string batch crosses as UTF-16 units.
	FloatNumbers() bool
	// BatchCheckEncoders is Go source defining `encFloat(float64) goany.Value` and
	// `encInt(int64) goany.Value` the way the language's record driver writes numbers, for the
	// program gate's batch check builds.
	BatchCheckEncoders() string
	// NothingType is the dialect type of a function that returns nothing ("None", "void"), and
	// BytesType the dialect type of Go's []byte ("" when the language has none): what
	// `refresh` writes into the manifest for those Go types.
	NothingType() string
	BytesType() string
}

Wrapper is what `bridge` and `refresh` generate for a source language: the wrapper its runtime loads in place of the original (it keeps the original's names, parameters and error classes and calls the Go library through the language's FFI), and the few ways the generated Go side encodes values for that runtime. A language registers one from an init function in its own file (python.go, node.go).

Directories

Path Synopsis
Package goany is the runtime of a go-anything port module: the JSON value the wire carries, an insertion-ordered map, and the decoders and encoders the generated bindings call.
Package goany is the runtime of a go-anything port module: the JSON value the wire carries, an insertion-ordered map, and the decoders and encoders the generated bindings call.

Jump to

Keyboard shortcuts

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