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
- Variables
- func BatchCheckGo(m *core.Manifest) ([]byte, error)
- func BatchGo(m *core.Manifest) ([]byte, error)
- func BytesType(lang core.Lang) string
- func CBridge(m *core.Manifest, root string) (map[string][]byte, error)
- func CDecl(cType, name string) string
- func CHeader(m *core.Manifest) string
- func CSignatureNamed(p clang.Proto, name string) string
- func CUnit(m *core.Manifest) string
- func FindIndex(portSrc []byte, p BatchPlan) (index string, ranks bool, err error)
- func GeneratedGoFiles(m *core.Manifest) (map[string][]byte, error)
- func GoExported(name string) string
- func GoLocal(name string) string
- func GoMod(m *core.Manifest) []byte
- func GoType(t Type) string
- func HasLiteralDefault(p core.Param) bool
- func IndexCJS(m *core.Manifest) ([]byte, error)
- func LibName(m *core.Manifest) string
- func MainGo(m *core.Manifest) ([]byte, error)
- func NothingType(lang core.Lang) string
- func PHPCodec() string
- func PHPTargets(m *core.Manifest, root string) (map[string]string, error)
- func PHPWrapper(m *core.Manifest, root string) (string, error)
- func PHPWrapperPath(m *core.Manifest) string
- func PackageJSON(m *core.Manifest) []byte
- func ParseBatch(spec string) (core.Batch, error)
- func PortGo(m *core.Manifest) ([]byte, error)
- func PythonInit(m *core.Manifest, root string) ([]byte, error)
- func PythonModule(root, file string) string
- func PythonPackage(m *core.Manifest) string
- func RubyNative(m *core.Manifest) string
- func RubyReceivers(m *core.Manifest, root string) (map[string]string, error)
- func RubyWrapper(m *core.Manifest, root string) ([]byte, error)
- func RubyWrapperFile(m *core.Manifest) string
- func RustArg(t *rust.Type, v string) string
- func RustOwned(t *rust.Type) string
- func RustResult(t *rust.Type) (ok string, isResult bool)
- func SplitTop(ts string, sep rune) []string
- func SwiftCamel(m *core.Manifest) string
- func SwiftDriverSource(m *core.Manifest, root string, port bool) (string, error)
- func SwiftErrorType(m *core.Manifest) string
- func SwiftModuleFiles(m *core.Manifest) map[string]string
- func SwiftNativeModule(m *core.Manifest) string
- func SwiftWrapperPath(m *core.Manifest) string
- func WireGo(m *core.Manifest) ([]byte, error)
- func WireTableText(m *core.Manifest) (string, error)
- func Wired(lang core.Lang, f Func) bool
- func WrapperFiles(m *core.Manifest, root string, initial bool) (map[string][]byte, error)
- type BatchPlan
- type Class
- type Func
- type Kind
- type Param
- type RustUnit
- type Type
- type Wrapper
Constants ¶
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);
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).
const CBridgeFile = "c_bridge.c"
CBridgeFile is the generated C file of a C port module, in build/.
const GoVersion = "1.25"
GoVersion is the `go` line of a port module's go.mod.
const KoffiVersion = "3.4.0"
KoffiVersion is the koffi the TS/JS wrapper loads the shared library with.
const SwiftDriverModule = "GoAnythingDriver"
SwiftDriverModule is the module name the driver is compiled under, removed from recorded error type names.
const WireVersion = "goany-wire/1"
WireVersion is the first line of the wire table a library answers (goany.TableText).
Variables ¶
var Objects []byte
Objects is goany/objects.go, the handle table a port module with classes carries.
var Runtime []byte
Runtime is goany/goany.go, the runtime every port module carries.
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.
var WireRuntime []byte
WireRuntime is goany/wire.go, the binary wire and the batches' runtime.
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 ¶
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 ¶
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 CDecl ¶
CDecl declares name with a C type of the prototypes (`const char*` and `s` give `const char *s`).
func CSignatureNamed ¶
CSignatureNamed is the prototype as the original declares it, typedefs resolved, under name.
func CUnit ¶
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 ¶
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 ¶
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 ¶
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 ¶
GoLocal is a parameter's Go name: `max_length` is maxLength; a Go keyword gets a `_`.
func HasLiteralDefault ¶
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 ¶
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 ¶
LibName is the module's name as Go, Python and the shared library spell it: `textkit-go` becomes `textkit_go`.
func NothingType ¶
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 ¶
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 ¶
PHPWrapper is php/<name>.php.
func PHPWrapperPath ¶
PHPWrapperPath is the wrapper's file, relative to the port module.
func PackageJSON ¶
PackageJSON is the port module's package.json: index.cjs and its one dependency.
func ParseBatch ¶
ParseBatch reads a `--batch function:param` spec.
func PortGo ¶
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 ¶
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 ¶
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 ¶
PythonPackage is the folder of the Python wrapper, relative to the port module.
func RubyNative ¶
RubyNative is the module of the wrapper that calls the library: `GoAnythingNativeTextkitGo`.
func RubyReceivers ¶
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 ¶
RubyWrapper is ruby/<name>.rb.
func RubyWrapperFile ¶
RubyWrapperFile is the wrapper's path in the port module.
func RustArg ¶
RustArg is how an argument read into its owned type is passed: borrowed to a `&` parameter.
func RustOwned ¶
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 ¶
RustResult is what a call answers on success, and whether the function returns a Result, whose Err is the error path.
func SwiftCamel ¶
SwiftCamel is the port module's library name in UpperCamelCase: `textkit_go` is `TextkitGo`.
func SwiftDriverSource ¶
SwiftDriverSource is SwiftDriver for the manifest of the original at root.
func SwiftErrorType ¶
SwiftErrorType is the error type the wrapper throws for a port's `error: "Type: message"`.
func SwiftModuleFiles ¶
SwiftModuleFiles are `swift/<Native>/module.modulemap` and its header, by path.
func SwiftNativeModule ¶
SwiftNativeModule is the Clang module the wrapper imports: the C declarations of the entry point, and the library to link.
func SwiftWrapperPath ¶
SwiftWrapperPath is the wrapper's path in the port module: `swift/TextkitGo.swift`.
func WireGo ¶
WireGo is wire.go: per export, decode the arguments, call the port, encode the answer.
func WireTableText ¶
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.
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 ¶
PlanBatches checks every batch of the manifest against the functions, with rust-anything's reasons for what cannot be batched.
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 ¶
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.
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 ¶
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.
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 ¶
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 (Type) ContainsJSON ¶
ContainsJSON reports a JSON value anywhere in t.
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).
Source Files
¶
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. |