protocol

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package protocol carries sqlc's codegen plugin wire format across this module's boundary and no further.

sqlc invokes a plugin as a subprocess: it writes a GenerateRequest protobuf to the process's stdin, and reads a GenerateResponse protobuf back from its stdout. That makes stdout a protocol channel rather than a place to print, which is why every other package here logs to stderr and why Serve takes its streams as arguments instead of reaching for os.Stdin and os.Stdout — a test can drive the whole exchange in memory.

The wire framing stops here. Reading the analysis a request carries is the convergence core's job, and rendering it is an emitter's; this package only gets the bytes on and off the process boundary.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsPluginInvocation

func IsPluginInvocation(args []string, stdin *os.File) bool

IsPluginInvocation reports whether args (os.Args[1:]) and stdin describe a call from sqlc rather than a person at a shell.

sqlc v1.24.0 and later pass the gRPC method to invoke as the first argument — "/plugin.CodegenService/Generate" — which is why a leading slash is the signal. Nothing a person would type as a subcommand starts with one.

Older sqlc passed no arguments at all, which is indistinguishable from a person typing `unison` and expecting help. stdin settles it: sqlc pipes the request in, a terminal does not.

func Serve

func Serve(ctx context.Context, stdin io.Reader, stdout io.Writer, gen Generator) error

Serve reads one GenerateRequest from stdin, hands it to gen, and writes the GenerateResponse to stdout. It is the entire lifetime of a plugin process: sqlc starts one per block and reads exactly one response.

An error from gen is returned rather than encoded, and nothing is written to stdout in that case. sqlc treats a non-zero exit with no response as a plugin failure and reports the process's stderr, which is where our diagnostics already are.

Types

type Generator

type Generator func(context.Context, *pb.GenerateRequest) ([]*pb.File, error)

Generator turns one dialect's analysis into the files sqlc should write.

It is called once per sqlc `sql:` block, which is once per dialect — the constraint the whole design is arranged around. The returned files are written verbatim, relative to that block's `out:` directory.

Jump to

Keyboard shortcuts

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