syntaxedit

package
v1.5.0 Latest Latest
Warning

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

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

Documentation

Overview

Package syntaxedit reads the entries of a request file as a model a form can show, and edits the file one element at a time: every edit replaces only the bytes of its target, keeping comments, blank lines and formatting elsewhere, and is checked by parsing the result. Every offset of the API is in UTF-16 code units, as the language server's.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalid = errors.New("syntaxedit: invalid edit")

ErrInvalid reports an edit refused because its result does not read as asked (a value that would start a comment, break a line, ...).

Functions

func EntryAt

func EntryAt(models []EntryModel, offset int) int

EntryAt returns the 1-based index of the entry at offset (UTF-16) of the source the models were built from: the entry whose range holds it, or the last entry starting before it; 0 before the first entry.

Types

type EntryModel

type EntryModel struct {
	// Index is the entry's 1-based position.
	Index int
	// Range spans the entry, from its method to the end of its last
	// line (comment lines above the method excluded).
	Range       Range
	Method      string
	MethodRange Range
	URL         string
	URLRange    Range
	// URLQuery are the params of the URL's own query string (nil: it has
	// none); URLQueryKept says why MoveURLQuery would refuse to move them
	// to [Query] ("": it would not).
	URLQuery     []URLParam
	URLQueryKept string
	// HasResponse tells whether the entry has an expected response.
	HasResponse bool
	Status      string
	StatusRange Range
	// HasBody tells whether the request has a body; Body is its source
	// text.
	HasBody   bool
	Body      string
	BodyRange Range
	// Rows are the rows of every section present, by section.
	Rows map[Section][]Row
}

EntryModel is an entry as a form shows it. Text values are source text, as written.

func Model

func Model(name string, src []byte) ([]EntryModel, error)

Model parses src (a file named name, for its dialect) into the model of its entries.

type LoginSpec

type LoginSpec struct {
	// Request is the login request; its Response status defaults to 200.
	Request syntax.EntrySpec
	// Capture is the variable the token is captured in, from the response
	// at JSONPath (e.g. "$.access_token").
	Capture  string
	JSONPath string
	// Redact makes the capture a secret.
	Redact bool
}

LoginSpec is an entry that logs in and captures a token from its JSON response, for the entries after it (a form's "add login" action, an OAuth2 import suggestion).

type Range

type Range struct{ Start, End int }

Range is a half-open range of UTF-16 code units [Start, End).

type Result

type Result struct {
	Source []byte
	Edits  []TextEdit
}

Result is an edited source and the edit that produced it from the original.

func AddAssert

func AddAssert(name string, src []byte, n int, assert string) (*Result, error)

AddAssert appends an assert (source text, e.g. `jsonpath "$.id" == 1`) to entry n.

func AddCapture

func AddCapture(name string, src []byte, n int, capture, query string) (*Result, error)

AddCapture appends a capture `name: query...` (source text) to entry n.

func AddEntry

func AddEntry(name string, src []byte, specs ...syntax.EntrySpec) (*Result, error)

AddEntry appends entries built from specs to the file.

func AddLoginEntry

func AddLoginEntry(name string, src []byte, before int, spec LoginSpec) (*Result, error)

AddLoginEntry inserts the login entry spec before entry before (1-based; past the last entry: at the end).

func AddRow

func AddRow(name string, src []byte, n int, section Section, key, value string) (*Result, error)

AddRow appends a row to section name of entry n, adding the section (and an `HTTP *` response for a response section) when missing.

func ApplyEdits

func ApplyEdits(name string, src []byte, rows []RowInsert) (*Result, error)

ApplyEdits adds rows to a file, each rendered and checked like AddRow (one more row in its section, the same entries), in order. It is the only way an edit made elsewhere (an import suggestion) reaches a file. The result has one edit from src.

func EnsureSection

func EnsureSection(name string, src []byte, n int, section Section) (*Result, error)

EnsureSection adds an empty section name to entry n when missing (and an `HTTP *` response for a response section).

func MoveURLQuery added in v1.5.0

func MoveURLQuery(name string, src []byte, n int) (*Result, error)

MoveURLQuery moves the query string of entry n's URL to Query rows, before the rows it has (the order they are sent in): the URL keeps its base, every `name=value` becomes a `name: value` row, the value percent-decoded (the engine encodes it again). It is refused when the URL has no query string or when a param would not be sent the same way: a #fragment, a piece that is not name=value, a value with a {{variable}} or a `;`, a bad % escape. The server reads the same params; an equivalent encoding may change (`+` is sent as %20, `,` as %2C), and empty pieces (`a=1&&b=2`, a trailing `&`) are dropped.

func RemoveEntry

func RemoveEntry(name string, src []byte, n int) (*Result, error)

RemoveEntry removes entry n with the comment lines directly above it, up to the next entry's own comment lines (the last entry: from the end of the previous one).

func RemoveRow

func RemoveRow(name string, src []byte, n int, section Section, i int) (*Result, error)

RemoveRow removes row i of section name of entry n, with its line. A section left empty keeps its header.

func RemoveSection

func RemoveSection(name string, src []byte, n int, section Section) (*Result, error)

RemoveSection removes section name of entry n, its header and rows (disabled ones too); a missing section is left as is.

func SetBody

func SetBody(name string, src []byte, n int, body string) (*Result, error)

SetBody sets the request body of entry n to body, source text (JSON, a multiline string, ...); an empty body removes it.

func SetMethod

func SetMethod(name string, src []byte, n int, method string) (*Result, error)

SetMethod replaces the method of entry n.

func SetRow

func SetRow(name string, src []byte, n int, section Section, i int, key, value string) (*Result, error)

SetRow replaces the key and value (source text) of row i (0-based, disabled rows counted) of section name of entry n. A disabled row stays disabled.

func SetStatus

func SetStatus(name string, src []byte, n int, status string) (*Result, error)

SetStatus sets the expected status of entry n ("*" or digits), adding an `HTTP <status>` response when the entry has none.

func SetURL

func SetURL(name string, src []byte, n int, url string) (*Result, error)

SetURL replaces the URL of entry n with url, source text.

func ToggleRow

func ToggleRow(name string, src []byte, n int, section Section, i int) (*Result, error)

ToggleRow disables row i of section name of entry n (it becomes a `# ` comment line) or enables a disabled one.

type Row

type Row struct {
	Key, Value string
	Disabled   bool
	// Range is the row's text, from its key to the end of its value.
	Range Range
}

Row is one row of a section. Key and Value are source text, as written (an assert has only a Value, its whole text). A disabled row is a `# key: value` comment line that reads as a row of its section.

type RowInsert

type RowInsert struct {
	Entry      int
	Section    Section
	Key, Value string
}

RowInsert is a row an edit made elsewhere (an import suggestion) adds to a file: Key and Value are source text, rendered like AddRow's.

type Section

type Section string

Section names a list of rows of an entry.

const (
	Headers         Section = "headers"
	Query           Section = "query"
	Form            Section = "form"
	Multipart       Section = "multipart"
	Cookies         Section = "cookies"
	BasicAuth       Section = "basic-auth"
	Options         Section = "options"
	ResponseHeaders Section = "response-headers"
	Captures        Section = "captures"
	Asserts         Section = "asserts"
	// Grpc is [SondeGrpc] (.sonde only): proto, import-path, protoset.
	Grpc Section = "grpc"
)

Sections. Request headers and response headers are rows without a section header.

type TextEdit

type TextEdit struct {
	Range   Range
	NewText string
}

TextEdit replaces Range of the original source with NewText.

type URLParam added in v1.5.0

type URLParam struct{ Key, Value string }

URLParam is a param of a URL's own query string (`?name=value&…`), as the Query row MoveURLQuery writes for it: source text, the value percent-decoded. A param that cannot move is listed as written in the URL.

Jump to

Keyboard shortcuts

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