selector

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package selector extracts values from an HTTP response using the small selector language shared by `# @capture` and `# @assert`:

status                 response status code
statusText             e.g. "OK"
header.<name>          first value of a response header (case-insensitive)
header.<name>.#        how many values it has; header.<name>[1] the second
cookie.<name>          value of a cookie the response set (Set-Cookie)
body                   raw body
body.$                 whole body as JSON
body.$.<path>          JSONPath: body.$.items[0].id, body.$..id,
                       body.$.items[?(@.done == true)].id, [-1], [1:3],
                       .# and .length; see path.go
duration               round-trip time in milliseconds

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check

func Check(sel string) error

Check parses a selector without a response, so validate reports a bad one before anything is sent. The error names what could not be read.

func Leading

func Leading(s string) (sel, rest string)

Leading splits an expression such as `body.$.items[?(@.done == true)].id == 2` into the selector at its start and the rest, so spaces inside a bracket (a filter, a quoted key) stay part of the selector.

func Select

func Select(resp *Response, sel string) (value string, ok bool, err error)

Select evaluates sel against resp. ok=false means the selector was valid but nothing was there (missing header or JSON path).

Types

type Kind

type Kind int

Kind is the JSON type of a selected value. Status, duration and counts are numbers; headers, cookies, statusText and the raw body are strings; a body path has the type of what it selects, and a set of matches is an array.

const (
	KindString Kind = iota
	KindNumber
	KindBool
	KindNull
	KindArray
	KindObject
)

func (Kind) String

func (k Kind) String() string

type Path

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

Path is a parsed body path.

func ParsePath

func ParsePath(tail string) (*Path, error)

ParsePath parses the tail of a `body.$` selector: `.items[0].id`, `..id`, `["key"]`. An error names the construct it could not read.

func (*Path) Eval

func (p *Path) Eval(doc gjson.Result) (nodes []gjson.Result, count *int, many bool)

Eval evaluates the path on a JSON document. many says the path went through a wildcard, slice, filter or descent, so the matches are a set.

type Response

type Response struct {
	Status     int
	StatusText string
	Headers    http.Header
	Body       []byte
	Duration   time.Duration
}

Response is the subset of an HTTP response the selector language sees.

type UnknownError

type UnknownError struct{ Selector string }

UnknownError is a selector that is none of the known forms (as opposed to a known form whose body path cannot be read).

func (*UnknownError) Error

func (e *UnknownError) Error() string

type Value

type Value struct {
	Text string
	Kind Kind
}

Value is a selected value: its text as captures and assertions see it (a string unquoted, anything else as JSON), and its type.

func SelectValue

func SelectValue(resp *Response, sel string) (Value, bool, error)

SelectValue is Select with the value's JSON type, for type checks.

Jump to

Keyboard shortcuts

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