oasdoc

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package oasdoc edits an OpenAPI document as a YAML node tree, addressed by JSON pointers.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrParse              = errors.New("parse spec")
	ErrNotObject          = errors.New("spec root is not an object")
	ErrManyDocuments      = errors.New("spec holds more than one YAML document")
	ErrUnsupportedVersion = errors.New("unsupported OpenAPI version")
	ErrPointer            = errors.New("invalid JSON pointer")
	ErrNotFound           = errors.New("JSON pointer not found")
	ErrMarshal            = errors.New("marshal spec")
)

Functions

func Child

func Child(n *yaml.Node, key string) *yaml.Node

Child returns the value under key when n is a mapping, following an alias, or nil.

func Clone

func Clone(n *yaml.Node) *yaml.Node

Clone copies n deeply, keeping lines and columns. An alias becomes a copy of its target.

func DeleteChild

func DeleteChild(n *yaml.Node, key string) bool

DeleteChild removes key from mapping n and reports whether it was there.

func DeleteChildren

func DeleteChildren(n *yaml.Node, drop func(key string, value *yaml.Node) bool) bool

DeleteChildren removes, in one pass, every entry of mapping n that drop picks, and reports whether it removed any.

func Escape

func Escape(token string) string

Escape encodes one reference token: ~ becomes ~0 and / becomes ~1.

func NewMapping

func NewMapping() *yaml.Node

func NewString

func NewString(value string) *yaml.Node

func SetChild

func SetChild(n *yaml.Node, key string, value *yaml.Node)

SetChild puts value under key in mapping n, appending the key when it is missing.

func Split

func Split(ptr string) ([]string, error)

Split turns a pointer into unescaped tokens; a # prefix marks a $ref fragment, also percent-decoded.

func Unescape

func Unescape(token string) string

func UnescapeSlashes

func UnescapeSlashes(data []byte) []byte

UnescapeSlashes turns \/ in a JSON spec into /: YAML 1.2 allows it, the YAML parser does not.

func Visit

func Visit(n *yaml.Node, ptr string, k Kind, fn func(ptr string, n *yaml.Node, k Kind) bool)

Visit calls fn on n, an object of kind k at ptr, then on every object under it in document order. Only places the OpenAPI grammar gives a kind are visited: never example values or extensions. fn returning false skips the children of that node.

Types

type Doc

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

Doc is a parsed spec. JSON input is read as YAML and written back as block YAML.

func Parse

func Parse(data []byte, file string) (*Doc, error)

Parse reads a spec. file is only used in errors and positions.

func (*Doc) CopyLayout

func (d *Doc) CopyLayout(from *Doc)

CopyLayout makes d marshal with the indentation and sequence style of from.

func (*Doc) Delete

func (d *Doc) Delete(ptr string) bool

Delete removes the node at ptr and reports whether there was one. The root cannot be deleted.

func (*Doc) File

func (d *Doc) File() string

func (*Doc) Get

func (d *Doc) Get(ptr string) *yaml.Node

Get returns the node at ptr, or nil when there is none or ptr is invalid.

func (*Doc) Marshal

func (d *Doc) Marshal() ([]byte, error)

Marshal keeps the source indentation; the encoder rewrites scalar tags and quotes in place.

func (*Doc) Positions

func (d *Doc) Positions() map[string]diag.Origin

Positions maps the pointer of every node to where it starts. A mapping entry starts at its key, unless its value came from another file. Nodes made in memory have no line and are left out, so lookups fall back to an ancestor.

func (*Doc) Refs

func (d *Doc) Refs() []Ref

Refs lists every $ref with a string value, in document order.

func (*Doc) Root

func (d *Doc) Root() *yaml.Node

func (*Doc) Set

func (d *Doc) Set(ptr string, value *yaml.Node) error

Set puts value at ptr. A missing last key is added to its mapping; "-" appends to a sequence.

func (*Doc) SetFile

func (d *Doc) SetFile(n *yaml.Node, file string)

SetFile records that n and everything under it came from file, for Positions.

func (*Doc) Version

func (d *Doc) Version() (spec.Version, error)

Version reads the openapi field. Swagger 2.0 and anything outside 3.0 to 3.2 is unsupported.

func (*Doc) Walk

func (d *Doc) Walk(fn func(ptr string, n *yaml.Node) bool)

Walk visits nodes depth-first with their pointers; false skips children, aliases are not followed.

type Kind

type Kind int

Kind is the OpenAPI object a node stands for, known from where it sits.

const (
	KindNone Kind = iota
	KindDocument
	KindComponents
	KindPaths
	KindPathItem
	KindOperation
	KindParameter
	KindRequestBody
	KindMediaType
	KindEncoding
	KindResponses
	KindResponse
	KindHeader
	KindSchema
	KindExample
	KindLink
	KindCallback
	KindSecurityScheme
)

func SectionKind

func SectionKind(section string) Kind

SectionKind is the kind of the objects under components/section, or KindNone.

func (Kind) Section

func (k Kind) Section() string

Section is the components key that holds objects of kind k, or "" when there is none.

type Ref

type Ref struct {
	Owner string
	Value string
}

Ref is one $ref in the document. Owner is the pointer of the object that holds it.

Jump to

Keyboard shortcuts

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