cborquery

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: MIT Imports: 11 Imported by: 1

README

cborquery

Overview

This is a XPath query package for parsing and querying Concise Binary Object Representation (CBOR) messages using the xpath package. The query package is writing in pure go.

cborquery helps you easily and flexibly extract data from CBOR messages using XPath queries without using pre-defined object structures.

Install Package
go get github.com/srebhan/cborquery

Get Started

Below is some example code to show how to use the package. Here we expect a message similar to the following JSON data

{
    "person":{
        "name":"John",
        "age":31,
        "female":false,
        "city":null,
        "hobbies":[
            "coding",
            "eating",
            "football"
        ]
    }
}

Using the xpath syntax you are able to access specific fields of a CBOR message

package main

import (
	"fmt"
	"strings"

	"github.com/srebhan/cborquery"
)

func main() {
    // Get a CBOR message from somewhere
    buf, err := os.ReadFile("mymessage.cbor")
	if err != nil {
		panic(err)
	}

    // Parse the message to be able to query the data
	doc, err := cborquery.Parse(bytes.NewBuffer(s))
	if err != nil {
		panic(err)
	}

	// xpath query
	age := cborquery.FindOne(doc, "age")
	// or
	age = cborquery.FindOne(doc, "person/age")
	fmt.Printf("%#v[%T]\n", age.Value(), age.Value())

	hobbies := cborquery.FindOne(doc, "//hobbies")
	fmt.Printf("%#v\n", hobbies.Value())
	firstHobby := cborquery.FindOne(doc, "//hobbies/*[1]")
	fmt.Printf("%#v\n", firstHobby.Value())
}

Iterating over the content

package main

import (
	"fmt"
	"strings"

	"github.com/srebhan/cborquery"
)

func main() {
    // Get a CBOR message from somewhere
    buf, err := os.ReadFile("mymessage.cbor")
	if err != nil {
		panic(err)
	}

    doc, err := cborquery.Parse(strings.NewReader(s))
	if err != nil {
		panic(err)
	}
	// iterate all json objects from child ndoes.
	for _, n := range doc.ChildNodes() {
		fmt.Printf("%s: %v[%T]\n", n.Data, n.Value(), n.Value())
	}
}

For more information on XPath and supported features and function see https://github.com/antchfx/xpath

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DisableSelectorCache = false

DisableSelectorCache will disable caching for the query selector if value is true.

View Source
var SelectorCacheMaxEntries = 50

SelectorCacheMaxEntries allows how many selector object can be caching. Default is 50. Will disable caching if SelectorCacheMaxEntries <= 0.

Functions

This section is empty.

Types

type Node

type Node struct {
	Parent, PrevSibling, NextSibling, FirstChild, LastChild *Node

	Type NodeType
	Name string
	// contains filtered or unexported fields
}

A Node consists of a NodeType and some Name (tag name for element nodes, content for text) and are part of a tree of Nodes.

func Find

func Find(top *Node, expr string) []*Node

Find is like QueryAll but will panics if `expr` cannot be parsed.

func FindOne

func FindOne(top *Node, expr string) *Node

FindOne is like Query but will panics if `expr` cannot be parsed.

func LoadURL

func LoadURL(url string) (*Node, error)

LoadURL loads the document from the specified URL.

func Parse

func Parse(r io.Reader) (*Node, error)

Parse CBOR document.

func Query

func Query(top *Node, expr string) (*Node, error)

Query searches the Node that matches by the specified XPath expr, and returns first element of matched.

func QueryAll

func QueryAll(top *Node, expr string) ([]*Node, error)

QueryAll searches the Node that matches by the specified XPath expr. Return an error if the expression `expr` cannot be parsed.

func QuerySelector

func QuerySelector(top *Node, selector *xpath.Expr) *Node

QuerySelector returns the first matched XML Node by the specified XPath selector.

func QuerySelectorAll

func QuerySelectorAll(top *Node, selector *xpath.Expr) []*Node

QuerySelectorAll searches all of the Node that matches the specified XPath selectors.

func (*Node) ChildNodes

func (n *Node) ChildNodes() []*Node

ChildNodes gets all child nodes of the node.

func (*Node) InnerText deprecated

func (n *Node) InnerText() string

InnerText will gets the value of the node and all its child nodes.

Deprecated: Use Value() to get object value.

func (*Node) OutputXML

func (n *Node) OutputXML() string

OutputXML prints the XML string.

func (*Node) SelectElement

func (n *Node) SelectElement(name string) *Node

SelectElement finds the first of child elements with the specified name.

func (*Node) Value

func (n *Node) Value() interface{}

Gets the object value.

type NodeNavigator

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

NodeNavigator is for navigating the document.

func CreateXPathNavigator

func CreateXPathNavigator(top *Node) *NodeNavigator

CreateXPathNavigator creates a new xpath.NodeNavigator for the specified html.Node.

func (*NodeNavigator) Copy

func (a *NodeNavigator) Copy() xpath.NodeNavigator

func (*NodeNavigator) Current

func (a *NodeNavigator) Current() *Node

func (*NodeNavigator) GetValue

func (a *NodeNavigator) GetValue() interface{}

func (*NodeNavigator) LocalName

func (a *NodeNavigator) LocalName() string

func (*NodeNavigator) MoveTo

func (a *NodeNavigator) MoveTo(other xpath.NodeNavigator) bool

func (*NodeNavigator) MoveToChild

func (a *NodeNavigator) MoveToChild() bool

func (*NodeNavigator) MoveToFirst

func (a *NodeNavigator) MoveToFirst() bool

func (*NodeNavigator) MoveToNext

func (a *NodeNavigator) MoveToNext() bool

func (*NodeNavigator) MoveToNextAttribute

func (x *NodeNavigator) MoveToNextAttribute() bool

func (*NodeNavigator) MoveToParent

func (a *NodeNavigator) MoveToParent() bool

func (*NodeNavigator) MoveToPrevious

func (a *NodeNavigator) MoveToPrevious() bool

func (*NodeNavigator) MoveToRoot

func (a *NodeNavigator) MoveToRoot()

func (*NodeNavigator) NodeType

func (a *NodeNavigator) NodeType() xpath.NodeType

func (*NodeNavigator) Prefix

func (a *NodeNavigator) Prefix() string

func (*NodeNavigator) String

func (a *NodeNavigator) String() string

func (*NodeNavigator) Value

func (a *NodeNavigator) Value() string

type NodeType

type NodeType uint

A NodeType is the type of a Node.

const (
	// DocumentNode is a document object that, as the root of the document tree,
	// provides access to the entire XML document.
	DocumentNode NodeType = iota
	// ElementNode is an element.
	ElementNode
	// TextNode is the text content of a node.
	TextNode
)

Directories

Path Synopsis
testcases

Jump to

Keyboard shortcuts

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