{"modulePath":"github.com/mockzilla/mockzilla-codegen","version":"v1.0.2","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package codegen\n\nPackage codegen generates Go models, HTTP servers, clients and MCP tools from OpenAPI specs.\n\n## Variables\n\n```go\nvar (\n\tErrWrite        = errors.New(\"write generated file\")\n\tErrFramework    = errors.New(\"no router for the framework\")\n\tErrTemplateFile = errors.New(\"read template file\")\n\tErrExtraFile    = errors.New(\"extra file\")\n\tErrNameClash    = errors.New(\"name declared twice\")\n)\n```\n\n## Functions\n\n```go\nfunc Version() string\n```\nVersion returns the version of this module in the running binary, or \"dev\" for local builds.\n\n## Types\n\n```go\ntype API struct {\n\tPackage     string\n\tService     TypeRef\n\tOperations  []Operation\n\tTypes       []TypeRef\n\tUserContext map[string]any\n}\n```\nAPI is what the template of an extra file sees of the generated code once names are resolved: the package of the default output file, the service interface, which is empty without a server, every operation, every declared type and the config's user-context. It only ever gains fields.\n\n```go\ntype Body struct {\n\tContentType string\n\tField       string\n\tType        TypeRef\n}\n```\nBody is one request body of an operation: its media type, the field of the request options that holds it, server and client alike, and the type of that field.\n\n```go\ntype Diagnostic struct {\n\tSeverity Severity\n\tCode     string\n\tPointer  string\n\tFile     string\n\tLine     int\n\tCol      int\n\tMessage  string\n}\n```\nDiagnostic is a problem found in the spec, or in what the config asks of it. Pointer is a JSON pointer into the prepared spec; File, Line and Col say where it is in the source, Line and Col 0 when unknown. A problem that has no place in the spec has none of them.\n\n```go\nfunc Prepare(ctx context.Context, cfg *config.Config, opts ...Option) ([]byte, []Diagnostic, error)\n```\nPrepare returns the spec the generator reads: refs to other files bundled in, overlays applied, then filtered, simplified and pruned as cfg says. A spec no step changes comes back byte for byte.\n\n```go\nfunc (d Diagnostic) String() string\n```\nString is the line the command prints: file:line:col: severity code: message \\[pointer], with each place left out when unknown.\n\n```go\ntype File struct {\n\tPath          string\n\tPackage       string\n\tParts         []string\n\tKind          FileKind\n\tIsOverwritten bool\n\tContent       []byte\n}\n```\nFile is one generated file. Path is resolved against the config folder; Parts lists the parts it holds, such as models.types. IsOverwritten is set on a scaffold that server.scaffold.overwrite lets Write replace.\n\n```go\ntype FileKind int\n```\n\n```go\nconst (\n\t// FileGenerated is rewritten on every run.\n\tFileGenerated FileKind = iota\n\t// FileScaffold is a starter file, written only when missing unless told to overwrite.\n\tFileScaffold\n)\n```\n\n```go\ntype Operation struct {\n\tID                   string\n\tMethod               string\n\tPath                 string\n\tSummary              string\n\tTags                 []string\n\tHasOptions           bool\n\tIsRouted             bool\n\tRequestOptions       TypeRef\n\tResponseData         TypeRef\n\tClientRequestOptions TypeRef\n\tClientResponse       TypeRef\n\tBodies               []Body\n\tResponses            []Response\n\tSuccess              *Response\n}\n```\nOperation is one operation or webhook of the spec. ID is its Go name, which the handlers report as the operation ID; HasOptions says whether it takes parameters or a body, IsRouted whether the router registers it. RequestOptions and ResponseData are the types of the service contract, empty without a server; ClientRequestOptions is what the client method takes and ClientResponse the envelope its WithResponse method returns, empty without a client or without envelopes. Bodies are the request bodies, in spec order. Responses come in the order of the generated code: codes, ranges, other keys, then default. Success points to the first of them with a Code from 200 to 299, nil without one.\n\n```go\ntype Option func(*options)\n```\n\n```go\nfunc WithSpec(data []byte) Option\n```\nWithSpec passes the spec in memory instead of reading spec.path. When spec.path is set it still names the spec, so refs to other files resolve next to it; otherwise they resolve against the config's folder.\n\n```go\ntype Response struct {\n\tStatus       string\n\tCode         int\n\tContentType  string\n\tBody         TypeRef\n\tIsRaw        bool\n\tIsStream     bool\n\tConstructor  TypeRef\n\tHasStatusArg bool\n}\n```\nResponse is one response of an operation. Status is its key as the spec writes it, such as 200, 2XX or default. Code is the status code the generated code reads from that key: the key when it is a number, the first digit times 100 when it has three characters and starts with a digit, else 0. ContentType and Body are those of its JSON body, else of its first one; Body is empty without one. IsRaw is set when the body has no schema: any, a string or bytes. Constructor is the function that makes the response data of this status, empty without a server: it takes the status first when HasStatusArg is set, which is when the key is no number, then the body when there is one. With IsStream it takes an iter.Seq of Body, one frame.\n\n```go\ntype Result struct {\n\tFiles       []File\n\tDiagnostics []Diagnostic\n}\n```\nResult is what Generate made: every file with its content, and what the spec and generation reported, sorted by position.\n\n```go\nfunc Generate(ctx context.Context, cfg *config.Config, opts ...Option) (*Result, error)\n```\nGenerate reads the spec cfg names, or the one WithSpec gives, and returns the files to write. cfg must come from config.Load or config.Parse, which fill in the defaults.\n\n```go\ntype Severity int\n```\n\n```go\nconst (\n\tSeverityInfo Severity = iota\n\tSeverityWarning\n\tSeverityError\n)\n```\n\n```go\nfunc (s Severity) String() string\n```\n\n```go\ntype TypeRef struct {\n\tName       string\n\tPackage    string\n\tImportPath string\n}\n```\nTypeRef is a Go type, or a function such as a response constructor. Name is the type as the package that declares it writes it, Package and ImportPath are those of the identifier in it. With an ImportPath, Name is an identifier, or a pointer, slice, array, map or channel around one, such as \\[]Pet, and Package, when set, is the package's name; without one the type needs no import, Name is written as it is, such as func() any, and Package is empty. A type the generator declares has neither when the output is one package outside a module.\n\n```go\nfunc (t TypeRef) Elem() TypeRef\n```\nElem is the type t points to, in the package of t: Pet for \\*Pet. It is empty when t is no pointer.\n\n```go\ntype WriteAction int\n```\n\n```go\nconst (\n\tActionWrite WriteAction = iota\n\tActionSkip\n)\n```\n\n```go\nfunc (a WriteAction) String() string\n```\n\n```go\ntype WriteOptions struct {\n\tDryRun bool\n}\n```\nWriteOptions control Write. DryRun reports what Write would do and writes nothing.\n\n```go\ntype WriteReport struct {\n\tPath   string\n\tAction WriteAction\n}\n```\nWriteReport says what Write did with one file.\n\n```go\nfunc Write(res *Result, opts WriteOptions) ([]WriteReport, error)\n```\nWrite writes the files of res, making folders as needed. A scaffold that exists is skipped unless its IsOverwritten is set. It stops at the first failure and returns the reports of the files before it.\n\n","path":"github.com/mockzilla/mockzilla-codegen/pkg/codegen","name":"codegen","synopsis":"Package codegen generates Go models, HTTP servers, clients and MCP tools from OpenAPI specs.","isRedistributable":true}
