spec

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsOpenAPI

func IsOpenAPI(src []byte, detectLines int) bool

IsOpenAPI reports whether src looks like an OpenAPI/Swagger spec: a YAML document whose first detectLines lines contain a top-level openapi:/swagger: key.

func PointerSegments

func PointerSegments(pointer string) []string

PointerSegments splits a JSON pointer into its decoded path segments (RFC 6901: ~1 -> '/', ~0 -> '~').

func ResolveRefPath

func ResolveRefPath(fromPath, file string) string

ResolveRefPath resolves an external ref file (relative to fromPath) to an absolute path.

func URIToPath

func URIToPath(uri string) string

URIToPath is the exported form of uriToPath for use by the LSP layer.

Types

type Component

type Component struct {
	Kind     string
	Name     string
	Pointer  string         // e.g. "/components/schemas/User"
	KeyRange protocol.Range // range of the name key
}

Component is a components.<kind>.<name> definition.

type Context

type Context struct {
	Kind    ContextKind
	Pointer string  // JSON pointer of the target/definition
	File    string  // external file (for a ref) if any
	Ref     *RefUse // populated when Kind == ContextRef
}

Context is the navigation target resolved at a cursor position.

type ContextKind

type ContextKind int

ContextKind classifies what the cursor is sitting on.

const (
	ContextNone ContextKind = iota
	ContextRef
	ContextComponent
)

type Document

type Document struct {
	URI    string
	IsSpec bool // true when the file looks like an OpenAPI/Swagger spec

	Operations []Operation
	Components []Component
	Refs       []RefUse
	PathRefs   []PathRef
	// contains filtered or unexported fields
}

Document is a parsed YAML file with an OpenAPI navigation index.

func Parse

func Parse(uri string, src []byte) (*Document, error)

Parse parses src and builds the navigation index. On a YAML syntax error it returns a Document with an empty index plus the error, so callers can keep a previous good index if they prefer.

func (*Document) ContextAt

func (d *Document) ContextAt(p protocol.Position) Context

ContextAt resolves the navigation context at an LSP position.

func (*Document) InternalRefsTo

func (d *Document) InternalRefsTo(pointer string) []protocol.Range

InternalRefsTo returns the ranges of internal $refs in this document that point at the given pointer.

func (*Document) IsPathItem

func (d *Document) IsPathItem() bool

IsPathItem reports whether the document's root is a path item (its top-level keys include HTTP methods). Such fragments are surfaced as operations, not as schema-like symbols.

func (*Document) PathItemMethods

func (d *Document) PathItemMethods(pointer string) []MethodEntry

PathItemMethods returns the HTTP-method entries of the path item at pointer (or the document root when pointer is ""), each with its key range. It lets the workspace expand a path-item-level $ref into METHOD /path symbols.

func (*Document) PointerAt

func (d *Document) PointerAt(p protocol.Position) string

PointerAt returns the JSON pointer of the mapping key at an LSP position, or "" if the cursor is not on a key. This lets find-references work from any definition key, including in referenced files that are not specs themselves (e.g. a top-level schema in schemas/User.yaml).

func (*Document) Resolve

func (d *Document) Resolve(pointer string) (protocol.Range, bool)

Resolve returns the definition range for an internal JSON pointer.

func (*Document) TopLevelDefs

func (d *Document) TopLevelDefs() []Component

TopLevelDefs returns the named definitions of a non-spec fragment file for the document outline: one entry per top-level mapping key, or — when the file is a single schema body — one entry named after the file.

type MethodEntry

type MethodEntry struct {
	Method      string // upper-cased HTTP method
	OperationID string
	Summary     string
	Range       protocol.Range
}

MethodEntry is one HTTP-method key of a path item, with its key range.

type Operation

type Operation struct {
	Method      string // upper-cased HTTP method
	Path        string
	OperationID string
	Summary     string
	KeyRange    protocol.Range // range of the method key (get:/post:/…)
	PathRange   protocol.Range // range of the parent path key (/users/{id}:)
}

Operation is a single METHOD /path entry under paths.

type PathRef

type PathRef struct {
	Path      string
	File      string         // external file part; empty for internal refs
	Pointer   string         // JSON pointer incl. leading '/'; empty for whole-file
	PathRange protocol.Range // range of the path key (/users:)
}

PathRef is a path-item-level $ref (paths./users: {$ref: ./paths/users.yaml}), where the whole path item — and thus its operations — lives in another file.

type Ref

type Ref struct {
	Value   string
	File    string // external file part; empty for internal refs
	Pointer string // JSON pointer incl. leading '/'; empty if none
}

Ref is a parsed $ref value, split into its file and JSON-pointer parts.

"#/components/schemas/User" -> {Pointer: "/components/schemas/User"}
"./schemas.yaml#/User"      -> {File: "./schemas.yaml", Pointer: "/User"}
"./schemas/User.yaml"       -> {File: "./schemas/User.yaml"}

func ParseRef

func ParseRef(value string) Ref

ParseRef splits a raw $ref string into file + pointer parts.

func (Ref) IsInternal

func (r Ref) IsInternal() bool

IsInternal reports whether the ref points within the current document.

type RefTarget

type RefTarget struct {
	Name  string         // last pointer segment, or the file stem for whole-file refs
	Kind  string         // inferred component kind ("schemas", …) or parent dir name
	URI   string         // file:// URI of the definition
	Range protocol.Range // range of the definition key (zero for whole-file refs)
}

RefTarget is a definition reachable via $ref from a spec, surfaced as a workspace symbol even when it lives in a file that is not itself a spec (e.g. a bare schemas/User.yaml fragment whose definitions are top-level keys, not entries under components:). Targets in files that are themselves specs are not returned — their symbols already come from the spec's own Operations/Components.

type RefUse

type RefUse struct {
	Raw     string
	File    string         // external file part; empty for internal refs
	Pointer string         // JSON pointer incl. leading '/'
	Range   protocol.Range // range of the $ref value
}

RefUse is a single $ref occurrence.

type Workspace

type Workspace struct {
	// contains filtered or unexported fields
}

Workspace holds every parsed YAML document keyed by absolute path. Open documents (fed by the editor) take precedence over on-disk versions.

func NewWorkspace

func NewWorkspace(roots []string) *Workspace

NewWorkspace creates a workspace rooted at the given directories.

func (*Workspace) Close

func (w *Workspace) Close(path string)

Close marks a document as no longer open; its last-parsed version is kept for cross-file resolution.

func (*Workspace) ExternalRefsTo

func (w *Workspace) ExternalRefsTo(targetPath, pointer string) []protocol.Location

ExternalRefsTo returns Locations of $refs across the workspace that point at (targetPath, pointer) via an external file reference.

func (*Workspace) Get

func (w *Workspace) Get(path string) *Document

Get returns a parsed document by path, if known.

func (*Workspace) GetOrLoad

func (w *Workspace) GetOrLoad(path string) *Document

GetOrLoad returns a document, parsing it from disk on demand.

func (*Workspace) Index

func (w *Workspace) Index()

Index walks the workspace roots and parses every YAML file it finds. Safe to call in a goroutine; it skips files already open in the editor.

func (*Workspace) OrphanComponents

func (w *Workspace) OrphanComponents() []RefTarget

OrphanComponents returns components defined in non-spec files (an explicit components: block, e.g. a shared components.yaml) regardless of whether any $ref targets them, so a schema can be found before the first ref is written. Spec files are excluded — their components come from the spec symbol pass.

func (*Workspace) Put

func (w *Workspace) Put(path string, doc *Document)

Put stores (or replaces) an editor-open document.

func (*Workspace) ReachableTargets

func (w *Workspace) ReachableTargets() []RefTarget

ReachableTargets walks the $ref graph starting from every known spec and returns the external definitions those specs point at, transitively. It is the source of workspace symbols for split specs whose schemas live in non-spec fragment files.

func (*Workspace) ResolveExternal

func (w *Workspace) ResolveExternal(fromPath string, ref Ref) (protocol.Location, bool)

ResolveExternal resolves an external $ref from fromPath to a Location.

func (*Workspace) SpecDocs

func (w *Workspace) SpecDocs() []*Document

SpecDocs returns all known documents that look like OpenAPI specs.

Jump to

Keyboard shortcuts

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