Help us shape the future of Go! Take the 2026 Go Developer Survey

spec

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package spec parses the vendored PMOCK OpenAPI document and turns it into the CLI command catalog.

The OpenAPI file is the single source of truth: command paths, flags, descriptions, required fields and enum hints all come from it. The small overlay.yaml only supplies product decisions the spec cannot express (curated command names, aliases, risk level, hidden internal operations).

Index

Constants

View Source
const (
	TypeString      = "string"
	TypeInteger     = "integer"
	TypeNumber      = "number"
	TypeBoolean     = "boolean"
	TypeStringArray = "string[]"
	TypeIntArray    = "integer[]"
)

Flag types understood by the command builder and the request encoder.

Variables

View Source
var Methods = []string{"get", "post", "put", "delete", "patch"}

Methods lists the HTTP methods this CLI understands.

Functions

func Kebab

func Kebab(s string) string

Kebab converts camelCase / API field names to CLI flag names.

Types

type Catalog

type Catalog struct {
	Commands []*Command
}

Catalog is the full command surface.

func BuildCatalog

func BuildCatalog(doc *Document, overlay *Overlay) (*Catalog, error)

BuildCatalog merges the OpenAPI operations with the overlay and derives flags. The result is deterministic: commands are sorted by their full path.

type Command

type Command struct {
	Key         string // METHOD /path
	Method      string
	Path        string
	Summary     string
	Description string

	PathParts   []string
	Aliases     []string
	Risk        Risk
	Internal    bool
	Async       string
	OperateType string
	Examples    []string

	Flags []*Flag
	// ComplexRequired lists required body fields that cannot be a flag
	// (arrays of objects / nested objects); they must be supplied via --json.
	ComplexRequired []string

	Body           *Schema
	BodySchemaName string
}

Command is one generated CLI leaf command.

func (*Command) CanWait

func (c *Command) CanWait() bool

CanWait reports whether --wait is available for this command.

func (*Command) FullPath

func (c *Command) FullPath() string

FullPath is the space-joined command path.

func (*Command) HasBody

func (c *Command) HasBody() bool

HasBody reports whether the operation sends a JSON body.

func (*Command) HasJSONInput

func (c *Command) HasJSONInput() bool

HasJSONInput reports whether --json is meaningful for this command.

func (*Command) LongHelp

func (c *Command) LongHelp() string

LongHelp is the description shown by `<command> --help`.

func (*Command) Usage

func (c *Command) Usage() string

Usage is the last command-path segment.

type CommandOverride

type CommandOverride struct {
	Op          string   `yaml:"op"`
	Path        []string `yaml:"path"`
	Aliases     []string `yaml:"aliases,omitempty"`
	Risk        string   `yaml:"risk,omitempty"`
	Internal    bool     `yaml:"internal,omitempty"`
	Async       string   `yaml:"async,omitempty"`
	OperateType string   `yaml:"operate_type,omitempty"`
	Examples    []string `yaml:"examples,omitempty"`
}

CommandOverride overrides one operation. Op is "METHOD /path".

type Components

type Components struct {
	Schemas map[string]*Schema `yaml:"schemas"`
}

Components holds reusable schemas.

type Document

type Document struct {
	OpenAPI    string                           `yaml:"openapi"`
	Info       Info                             `yaml:"info"`
	Paths      map[string]map[string]*Operation `yaml:"paths"`
	Components Components                       `yaml:"components"`
}

Document is the subset of OpenAPI 3.0 used by this CLI.

func Load

func Load(r io.Reader) (*Document, error)

Load reads and validates the OpenAPI document.

func LoadFS

func LoadFS(fsys fs.FS, name string) (*Document, error)

LoadFS reads the document from an fs.FS.

func (*Document) BodySchema

func (d *Document) BodySchema(op *Operation) (*Schema, string)

BodySchema returns the resolved JSON body schema for an operation.

func (*Document) Operations

func (d *Document) Operations() []OperationRef

Operations returns all HTTP operations in a deterministic order.

func (*Document) Resolve

func (d *Document) Resolve(s *Schema) *Schema

Resolve follows $ref chains and returns the concrete schema. It never returns nil for a non-nil input; an unresolvable ref is returned unchanged so callers can decide whether that is fatal.

type Flag

type Flag struct {
	Name        string // kebab-case CLI name, without --
	Field       string // JSON / query field name
	In          string // query, path, or body
	Type        string
	Description string
	Required    bool
	Enum        []any
	Default     any
}

Flag is one generated command-line flag.

func (*Flag) Help

func (f *Flag) Help() string

Help renders the flag description shown by --help.

type Info

type Info struct {
	Title       string `yaml:"title"`
	Description string `yaml:"description"`
	Version     string `yaml:"version"`
}

Info is the OpenAPI info block.

type MediaType

type MediaType struct {
	Schema *Schema `yaml:"schema"`
}

MediaType wraps a schema.

type Operation

type Operation struct {
	Summary     string              `yaml:"summary"`
	Description string              `yaml:"description"`
	Tags        []string            `yaml:"tags"`
	Parameters  []Parameter         `yaml:"parameters"`
	RequestBody *RequestBody        `yaml:"requestBody"`
	Responses   map[string]Response `yaml:"responses"`
	Deprecated  bool                `yaml:"deprecated"`
}

Operation is one HTTP operation.

type OperationRef

type OperationRef struct {
	Method string
	Path   string
	Op     *Operation
}

OperationRef is an operation plus its HTTP coordinates.

func (OperationRef) Key

func (r OperationRef) Key() string

Key is the stable identifier used by overlay.yaml.

type Overlay

type Overlay struct {
	Commands []CommandOverride `yaml:"commands"`
}

Overlay supplies the product decisions that the OpenAPI document cannot express: the user-facing command path, aliases, risk level, and whether an operation is an internal building block that should stay out of `--help`.

func LoadOverlay

func LoadOverlay(r io.Reader) (*Overlay, error)

LoadOverlay parses overlay.yaml.

func LoadOverlayFS

func LoadOverlayFS(fsys fs.FS, name string) (*Overlay, error)

LoadOverlayFS parses overlay.yaml from an fs.FS.

type Parameter

type Parameter struct {
	Name        string  `yaml:"name"`
	In          string  `yaml:"in"`
	Description string  `yaml:"description"`
	Required    bool    `yaml:"required"`
	Schema      *Schema `yaml:"schema"`
}

Parameter is a path or query parameter. Header parameters (the doc generator's placeholder `chameleon`) are parsed but ignored by the catalog.

type RequestBody

type RequestBody struct {
	Required bool                 `yaml:"required"`
	Content  map[string]MediaType `yaml:"content"`
}

RequestBody describes a JSON request body.

type Response

type Response struct {
	Description string               `yaml:"description"`
	Content     map[string]MediaType `yaml:"content"`
}

Response is an HTTP response.

type Risk

type Risk string

Risk drives confirmation and warning behaviour.

const (
	RiskRead        Risk = "read"
	RiskWrite       Risk = "write"
	RiskCostly      Risk = "costly"
	RiskDestructive Risk = "destructive"
)

type Schema

type Schema struct {
	Ref         string             `yaml:"$ref"`
	Type        string             `yaml:"type"`
	Format      string             `yaml:"format"`
	Description string             `yaml:"description"`
	Required    []string           `yaml:"required"`
	Properties  map[string]*Schema `yaml:"properties"`
	Items       *Schema            `yaml:"items"`
	Enum        []any              `yaml:"enum"`
	Example     any                `yaml:"example"`
	Default     any                `yaml:"default"`
	Nullable    bool               `yaml:"nullable"`
}

Schema is the subset of JSON Schema used by the PMOCK spec. It covers primitives, arrays and $ref objects; the spec uses no allOf/oneOf/anyOf.

Jump to

Keyboard shortcuts

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