parsers

package
v0.0.0-...-b1083d3 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

parsers is an interface that Colibri can use to parse the content of responses.

Index

Constants

View Source
const (
	XPathExpr = "xpath"

	CSSelector = "css"

	RegularExpr = "regular"
)
View Source
const HTMLRegexp = `^text\/html`

HTMLRegexp contains a regular expression that matches the HTML MIME type.

View Source
const JSONRegexp = `^application\/(json|x-json|([a-z]+\+json))`

JSONRegexp contains a regular expression that matches the JSON MIME type.

View Source
const TextRegexp = `^text\/plain`

TextRegexp contains a regular expression that matches the MIME type plain text.

View Source
const XMLRegexp = `(?i)((application|image|message|model)/((\w|\.|-)+\+?)?|text/)(wb)?xml`

XMLRegexp contains a regular expression that matches the XML MIME type.

Variables

View Source
var (
	// ErrNotMatch is returned when Content-Tyepe does not match Paser.
	ErrNotMatch = errors.New("Content-Type does not match")

	// ErrExprType is returned when the expression type is not supported by the node.
	ErrExprType = errors.New("ExprType not compatible with node")
)

Functions

func Set

func Set[T Node](parsers *Parsers, expr string, parserFunc func(colibri.Response) (T, error)) error

Set adds a parser with its regular expression corresponding to the parsers.

Types

type HTMLNode

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

func ParseHTML

func ParseHTML(resp colibri.Response) (*HTMLNode, error)

func (*HTMLNode) CSSFind

func (html *HTMLNode) CSSFind(expr string) (Node, error)

func (*HTMLNode) CSSFindAll

func (html *HTMLNode) CSSFindAll(expr string) ([]Node, error)

func (*HTMLNode) Find

func (html *HTMLNode) Find(expr, exprType string) (Node, error)

func (*HTMLNode) FindAll

func (html *HTMLNode) FindAll(expr, exprType string) ([]Node, error)

func (*HTMLNode) Value

func (html *HTMLNode) Value() any

func (*HTMLNode) XPathFind

func (html *HTMLNode) XPathFind(expr string) (Node, error)

func (*HTMLNode) XPathFindAll

func (html *HTMLNode) XPathFindAll(expr string) ([]Node, error)

type JSONode

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

func ParseJSON

func ParseJSON(resp colibri.Response) (*JSONode, error)

func (*JSONode) Find

func (json *JSONode) Find(expr, exprType string) (Node, error)

func (*JSONode) FindAll

func (json *JSONode) FindAll(expr, exprType string) ([]Node, error)

func (*JSONode) Value

func (json *JSONode) Value() any

type Node

type Node interface {
	// Find finds the first child node that matches the expression.
	Find(expr, exprType string) (Node, error)

	// FindAll finds all child nodes that match the expression.
	FindAll(expr, exprType string) ([]Node, error)

	// Value returns the value of the node.
	Value() any
}

type Parsers

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

Parsers is used to parse the content of the answers. When a regular expression matches the content type of the response, the content of the response is parsed with the parser corresponding to the regular expression.

func New

func New() (*Parsers, error)

New returns a new default parser to parse HTML, XHML, JSON, and plain text. See the colibri.Parser interface.

func (*Parsers) Clear

func (parsers *Parsers) Clear()

func (*Parsers) Match

func (parsers *Parsers) Match(contentType string) bool

Match returns true if the content-type is supported.

func (*Parsers) Parse

func (parsers *Parsers) Parse(rules *colibri.Rules, resp colibri.Response) (map[string]any, error)

Parse parses the response based on the rules.

type TextNode

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

func ParseText

func ParseText(resp colibri.Response) (*TextNode, error)

func (*TextNode) Find

func (text *TextNode) Find(expr, exprType string) (Node, error)

func (*TextNode) FindAll

func (text *TextNode) FindAll(expr, exprType string) ([]Node, error)

func (*TextNode) Value

func (text *TextNode) Value() any

type XMLNode

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

func ParseXML

func ParseXML(resp colibri.Response) (*XMLNode, error)

func (*XMLNode) Find

func (xml *XMLNode) Find(expr, exprType string) (Node, error)

func (*XMLNode) FindAll

func (xml *XMLNode) FindAll(expr, exprType string) ([]Node, error)

func (*XMLNode) Value

func (xml *XMLNode) Value() any

Jump to

Keyboard shortcuts

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