Documentation ¶
Index ¶
- Constants
- Variables
- func AddChild(parent, n *Node)
- func IsJSON(n *Node) bool
- func IsJSONArr(n *Node) bool
- func IsJSONObj(n *Node) bool
- func IsJSONProp(n *Node) bool
- func IsJSONRoot(n *Node) bool
- func IsJSONValue(n *Node) bool
- func IsJSONValueBool(n *Node) bool
- func IsJSONValueNull(n *Node) bool
- func IsJSONValueNum(n *Node) bool
- func IsJSONValueStr(n *Node) bool
- func IsXML(n *Node) bool
- func J2NodeToInterface(n *Node, useJSONType bool) interface{}
- func JSONify1(n *Node) string
- func JSONify2(n *Node) string
- func MatchAny(n *Node, expr *xpath.Expr) bool
- func QueryIter(n *Node, expr *xpath.Expr) *xpath.NodeIterator
- func RemoveAndReleaseTree(n *Node)
- type JSONStreamReader
- type JSONType
- type Node
- func CreateJSONNode(ntype NodeType, data string, jtype JSONType) *Node
- func CreateNode(ntype NodeType, data string) *Node
- func CreateXMLNode(ntype NodeType, data string, xmlSpecific XMLSpecific) *Node
- func MatchAll(n *Node, exprStr string, flags ...uint) ([]*Node, error)
- func MatchSingle(n *Node, exprStr string, flags ...uint) (*Node, error)
- type NodeType
- type XMLSpecific
- type XMLStreamReader
Constants ¶
const ( // DisableXPathCache disables caching xpath compilation when MatchAll/MatchSingle // are called. Useful when caller knows the xpath string isn't cache-able (such as // containing unique IDs, timestamps, etc.) which would otherwise cause the xpath // compilation cache grow unbounded. DisableXPathCache = uint(1) << iota )
Variables ¶
var ( // ErrNoMatch is returned when not a single matched node can be found. ErrNoMatch = errors.New("no match") // ErrMoreThanExpected is returned when more than expected matched nodes are found. ErrMoreThanExpected = errors.New("more than expected matched") )
Functions ¶
func IsJSONProp ¶
IsJSONProp checks if a given Node is of JSONProp type.
func IsJSONRoot ¶
IsJSONRoot checks if a given Node is of JSONRoot type.
func IsJSONValue ¶
IsJSONValue checks if a given Node is of any JSON value types.
func IsJSONValueBool ¶
IsJSONValueBool checks if a given Node is of JSONValueBool type.
func IsJSONValueNull ¶
IsJSONValueNull checks if a given Node is of JSONValueNull type.
func IsJSONValueNum ¶
IsJSONValueNum checks if a given Node is of JSONValueNum type.
func IsJSONValueStr ¶
IsJSONValueStr checks if a given Node is of JSONValueStr type.
func J2NodeToInterface ¶
J2NodeToInterface translate an *idr.Node and its subtree into a JSON-marshaling friendly interface{}.
func MatchAny ¶
MatchAny returns true if the xpath query 'expr' against an IDR tree rooted at 'n' yields any result.
func QueryIter ¶
func QueryIter(n *Node, expr *xpath.Expr) *xpath.NodeIterator
QueryIter initiates an xpath query specified by 'expr' against an IDR tree rooted at 'n'.
func RemoveAndReleaseTree ¶
func RemoveAndReleaseTree(n *Node)
RemoveAndReleaseTree removes a node and its subtree from an IDR tree it is in and release the resources (Node allocation) associated with the node and its subtree.
Types ¶
type JSONStreamReader ¶
type JSONStreamReader struct {
// contains filtered or unexported fields
}
JSONStreamReader is a streaming JSON to *Node reader.
func NewJSONStreamReader ¶
func NewJSONStreamReader(r io.Reader, xpathStr string) (*JSONStreamReader, error)
NewJSONStreamReader creates a new instance of JSON streaming reader.
func (*JSONStreamReader) AtLine ¶
func (sp *JSONStreamReader) AtLine() int
AtLine returns the **rough** line number of the current JSON decoder.
func (*JSONStreamReader) Read ¶
func (sp *JSONStreamReader) Read() (*Node, error)
Read returns a *Node that matches the xpath streaming criteria.
func (*JSONStreamReader) Release ¶
func (sp *JSONStreamReader) Release(n *Node)
Release releases the *Node (and its subtree) that Read() has previously returned. Note even if Release is not explicitly called, next Read() call will still release the current streaming target node.
type JSONType ¶
type JSONType uint
JSONType is the type of JSON-specific Node. Note multiple JSONType can be bit-wise OR'ed together.
const ( // JSONRoot is the type for the root Node in a JSON IDR tree. JSONRoot JSONType = 1 << iota // JSONObj is the type for a Node in a JSON IDR tree whose value is an object. JSONObj // JSONArr is the type for a Node in a JSON IDR tree whose value is an array. JSONArr // JSONProp is the type for a Node in a JSON IDR tree who is a property. JSONProp // JSONValueStr is the type for a Node in a JSON IDR tree who is a string value. JSONValueStr // JSONValueNum is the type for a Node in a JSON IDR tree who is a numeric value. JSONValueNum // JSONValueBool is the type for a Node in a JSON IDR tree who is a boolean value. JSONValueBool // JSONValueNull is the type for a Node in a JSON IDR tree who is a null value. JSONValueNull )
func JSONTypeOf ¶
JSONTypeOf returns the JSONType of a Node. Note if the Node isn't of JSON, this function will panic.
type Node ¶
type Node struct { // ID uniquely identifies a Node, whether it's newly created or recycled and reused from // the node allocation cache. Previously we sometimes used a *Node's pointer address as a // unique ID which isn't sufficiently unique any more given the introduction of using // sync.Pool for node allocation caching. ID int64 Parent, FirstChild, LastChild, PrevSibling, NextSibling *Node Type NodeType Data string FormatSpecific interface{} }
Node represents a node of element/data in an IDR (intermediate data representation) ingested and created by the omniparser. Credit: this is by and large a copy and some adaptation from https://github.com/antchfx/xmlquery/blob/master/node.go. The reasons we want to have our own struct:
- one struct to represent XML/JSON/EDI/CSV/txt/etc. Vs antchfx's work have one struct (in each repo) for each format.
- Node allocation recycling.
- more stability
func CreateJSONNode ¶
CreateJSONNode creates a JSON Node.
func CreateNode ¶
CreateNode creates a generic *Node.
func CreateXMLNode ¶
func CreateXMLNode(ntype NodeType, data string, xmlSpecific XMLSpecific) *Node
CreateXMLNode creates an XML Node.
func MatchAll ¶
MatchAll returns all the matched nodes by an xpath query 'exprStr' against an IDR tree rooted at 'n'.
func MatchSingle ¶
MatchSingle returns one and only one matched node by an xpath query 'exprStr' against an IDR tree rooted at 'n'. If no matching node is found, ErrNoMatch is returned; if more than one matching nodes are found, ErrMoreThanExpected is returned.
type NodeType ¶
type NodeType uint
NodeType is the type of Node in an IDR.
const ( // DocumentNode is the type of the root Node in an IDR tree. DocumentNode NodeType = iota // ElementNode is the type of element Node in an IDR tree. ElementNode // TextNode is the type of text/data Node in an IDR tree. TextNode // AttributeNode is the type of attribute Node in an IDR tree. AttributeNode )
type XMLSpecific ¶
XMLSpecific contains XML IDR Node specific information such as namespace.
func XMLSpecificOf ¶
func XMLSpecificOf(n *Node) XMLSpecific
XMLSpecificOf returns the XMLSpecific field of a Node. Note if the Node isn't of XML, this function will panic.
type XMLStreamReader ¶
type XMLStreamReader struct {
// contains filtered or unexported fields
}
XMLStreamReader is a streaming XML to *Node reader.
func NewXMLStreamReader ¶
func NewXMLStreamReader(r io.Reader, xpathStr string) (*XMLStreamReader, error)
NewXMLStreamReader creates a new instance of XML streaming reader.
func (*XMLStreamReader) AtLine ¶
func (sp *XMLStreamReader) AtLine() int
AtLine returns the **rough** line number of the current XML decoder.
func (*XMLStreamReader) Read ¶
func (sp *XMLStreamReader) Read() (n *Node, err error)
Read returns a *Node that matches the xpath streaming criteria.
func (*XMLStreamReader) Release ¶
func (sp *XMLStreamReader) Release(n *Node)
Release releases the *Node (and its subtree) that Read() has previously returned. Note even if Release is not explicitly called, next Read() call will still release the current streaming target node.