idr

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2022 License: MIT Imports: 16 Imported by: 7

Documentation

Index

Constants

View Source
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

View Source
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 AddChild

func AddChild(parent, n *Node)

AddChild adds 'n' as the new last child to 'parent'.

func IsJSON

func IsJSON(n *Node) bool

IsJSON checks if a Node is of JSON.

func IsJSONArr

func IsJSONArr(n *Node) bool

IsJSONArr checks if a given Node is of JSONArr type.

func IsJSONObj

func IsJSONObj(n *Node) bool

IsJSONObj checks if a given Node is of JSONObj type.

func IsJSONProp

func IsJSONProp(n *Node) bool

IsJSONProp checks if a given Node is of JSONProp type.

func IsJSONRoot

func IsJSONRoot(n *Node) bool

IsJSONRoot checks if a given Node is of JSONRoot type.

func IsJSONValue

func IsJSONValue(n *Node) bool

IsJSONValue checks if a given Node is of any JSON value types.

func IsJSONValueBool

func IsJSONValueBool(n *Node) bool

IsJSONValueBool checks if a given Node is of JSONValueBool type.

func IsJSONValueNull

func IsJSONValueNull(n *Node) bool

IsJSONValueNull checks if a given Node is of JSONValueNull type.

func IsJSONValueNum

func IsJSONValueNum(n *Node) bool

IsJSONValueNum checks if a given Node is of JSONValueNum type.

func IsJSONValueStr

func IsJSONValueStr(n *Node) bool

IsJSONValueStr checks if a given Node is of JSONValueStr type.

func IsXML

func IsXML(n *Node) bool

IsXML checks if a Node is of XML.

func J2NodeToInterface

func J2NodeToInterface(n *Node, useJSONType bool) interface{}

J2NodeToInterface translate an *idr.Node and its subtree into a JSON-marshaling friendly interface{}.

func JSONify1

func JSONify1(n *Node) string

JSONify1 json marshals a *Node verbatim. Mostly used in test for snapshotting.

func JSONify2

func JSONify2(n *Node) string

JSONify2 JSON marshals a *Node into a minified JSON string.

func MatchAny

func MatchAny(n *Node, expr *xpath.Expr) bool

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

func JSONTypeOf(n *Node) JSONType

JSONTypeOf returns the JSONType of a Node. Note if the Node isn't of JSON, this function will panic.

func (JSONType) String

func (jt JSONType) String() string

String converts JSONType to a string.

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

func CreateJSONNode(ntype NodeType, data string, jtype JSONType) *Node

CreateJSONNode creates a JSON Node.

func CreateNode

func CreateNode(ntype NodeType, data string) *Node

CreateNode creates a generic *Node.

func CreateXMLNode

func CreateXMLNode(ntype NodeType, data string, xmlSpecific XMLSpecific) *Node

CreateXMLNode creates an XML Node.

func MatchAll

func MatchAll(n *Node, exprStr string, flags ...uint) ([]*Node, error)

MatchAll returns all the matched nodes by an xpath query 'exprStr' against an IDR tree rooted at 'n'.

func MatchSingle

func MatchSingle(n *Node, exprStr string, flags ...uint) (*Node, error)

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.

func (*Node) InnerText

func (n *Node) InnerText() string

InnerText returns a Node's children's texts concatenated. Note (in an XML IDR tree) none of the AttributeNode's text will be included.

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
)

func (NodeType) String

func (nt NodeType) String() string

String converts NodeType to a string.

type XMLSpecific

type XMLSpecific struct {
	NamespacePrefix string
	NamespaceURI    string
}

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.

Jump to

Keyboard shortcuts

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