Documentation ¶
Overview ¶
Package jq provides go bindings for libjq providing a streaming filter of JSON documents.
This package provides a thin layer on top of stedolan's libjq -- it would likely be helpful to read through the wiki pages about it:
jv: the JSON value type https://github.com/stedolan/jq/wiki/C-API:-jv
Index ¶
- type Jq
- type Jv
- func JvArray() *Jv
- func JvFromBool(b bool) *Jv
- func JvFromFloat(n float64) *Jv
- func JvFromInterface(intf interface{}) (*Jv, error)
- func JvFromJSONBytes(b []byte) (*Jv, error)
- func JvFromJSONString(str string) (*Jv, error)
- func JvFromString(str string) *Jv
- func JvInvalid() *Jv
- func JvInvalidWithMessage(msg *Jv) *Jv
- func JvNull() *Jv
- func JvObject() *Jv
- func (jv *Jv) ArrayAppend(val *Jv) *Jv
- func (jv *Jv) ArrayGet(idx int) *Jv
- func (jv *Jv) ArrayLength() int
- func (jv *Jv) Copy() *Jv
- func (jv *Jv) Dump(flags JvPrintFlags) string
- func (jv *Jv) Free() *Jv
- func (jv *Jv) GetInvalidMessage() *Jv
- func (jv *Jv) GetInvalidMessageAsString() (string, bool)
- func (jv *Jv) IsValid() bool
- func (jv *Jv) Kind() JvKind
- func (jv *Jv) ObjectSet(key *Jv, val *Jv) *Jv
- func (jv *Jv) String() (string, error)
- func (jv *Jv) ToGoVal() interface{}
- type JvKind
- type JvPrintFlags
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Jq ¶
type Jq struct {
// contains filtered or unexported fields
}
Jq encapsulates the state needed to interface with the libjq C library
func (*Jq) Close ¶
func (jq *Jq) Close()
Close the handle to libjq and free C resources.
If Start() has been called this will block until the input Channel it returns has been closed.
func (*Jq) Compile ¶ added in v0.1.0
Compile the program and make it ready to Execute()
Only a single program can be compiled on a Jq object at once. Calling this again a second time will replace the current program.
args is a list of key/value pairs to bind as variables into the program, and must be an array type even if empty. Each element of the array should be an object with a "name" and "value" properties. Name should exclude the "$" sign. For example this is `[ {"name": "n", "value": 1 } ]` would then be `$n` in the program.
func (*Jq) Execute ¶ added in v0.1.0
Execute will run the Compiled() program against a single input and return the results.
Using this interface directly is not thread-safe -- it is up to the caller to ensure that this is not called from two goroutines concurrently.
func (*Jq) Start ¶
Start will compile `program` and return a three channels: input, output and error. Sending a jq.Jv* to input cause the program to be run to it and one-or-more results returned as jq.Jv* on the output channel, or one or more error values sent to the error channel. When you are done sending values close the input channel.
args is a list of key/value pairs to bind as variables into the program, and must be an array type even if empty. Each element of the array should be an object with a "name" and "value" properties. Name should exclude the "$" sign. For example this is `[ {"name": "n", "value": 1 } ]` would then be `$n` in the programm.
This function is not reentereant -- in that you cannot and should not call Start again until you have closed the previous input channel.
If there is a problem compiling the JQ program then the errors will be reported on error channel before any input is read so makle sure you account for this case.
Any jq.Jv* values passed to the input channel will be owned by the channel. If you want to keep them afterwards ensure you Copy() them before passing to the channel
type Jv ¶
type Jv struct {
// contains filtered or unexported fields
}
Jv represents a JSON value from libjq.
The go wrapper uses the same memory management semantics as the underlying C library, so you should familiarize yourself with https://github.com/stedolan/jq/wiki/C-API:-jv#memory-management. In summary this package and all JQ functions operate on the assumption that any jv value you pass to a function is then owned by that function -- if you do not wish this to be the case call Copy() on it first.
func JvFromBool ¶ added in v0.1.0
JvFromBool returns a new jv of "true" or "false" kind depending on the given boolean value
func JvFromFloat ¶ added in v0.1.0
JvFromFloat returns a new jv number-typed value containing the given float value.
func JvFromInterface ¶ added in v0.1.0
func JvFromJSONBytes ¶
JvFromJSONBytes takes a utf-8 byte sequence containing JSON and returns the jv representation of it.
func JvFromJSONString ¶
JvFromJSONString takes a JSON string and returns the jv representation of it.
func JvFromString ¶
JvFromString returns a new jv string-typed value containing the given go string.
func JvInvalid ¶
func JvInvalid() *Jv
JvInvalid returns an invalid jv object without an error property
func JvInvalidWithMessage ¶
JvInvalidWithMessage creates an "invalid" jv with the given error message.
msg can be a string or an object
Consumes `msg`
func (*Jv) ArrayAppend ¶
ArrayAppend appends a single value to the end of the array.
If jv is not an array this will cause an assertion.
Consumes the invocant
func (*Jv) ArrayGet ¶
ArrayGet returns the element at the given array index.
If the index is out of bounds it will return an Invalid Jv object (with no error message set).
`idx` cannot be negative.
Consumes the invocant
func (*Jv) ArrayLength ¶
ArrayLength returns the number of elements in the array.
Consumes the invocant
func (*Jv) Copy ¶
Copy returns a *Jv so that the original won't get freed.
Does not consume the invocant.
func (*Jv) Dump ¶
func (jv *Jv) Dump(flags JvPrintFlags) string
Dump produces a human readable version of the string with the requested formatting.
Consumes the invocant
func (*Jv) Free ¶
Free this reference to a Jv value.
Don't call this more than once per jv - might not actually free the memory as libjq uses reference counting. To make this more like the libjq interface we return a nil pointer.
func (*Jv) GetInvalidMessage ¶
GetInvalidMessage returns the message associcated
func (*Jv) GetInvalidMessageAsString ¶
GetInvalidMessageAsString gets the error message for this Jv. If there is none it will return ("", false). Otherwise it will return the message as a string and true, converting non-string values if necessary. If you want the message in it's native Jv type use `GetInvalidMessage()`
Consumes the invocant.
func (*Jv) IsValid ¶
IsValid returns true if this Jv represents a valid JSON type, or false if it is unitiaizlied or if it represents an error type
Does not consume the invocant.
func (*Jv) Kind ¶
Kind returns a JvKind saying what type this jv contains.
Does not consume the invocant.
func (*Jv) ObjectSet ¶
ObjectSet will add val to the object under the given key.
This is the equivalent of `jv[key] = val`.
Consumes invocant and both key and val
type JvKind ¶
type JvKind int
JvKind represents the type of value that a `Jv` contains.
const ( JV_KIND_INVALID JvKind = C.JV_KIND_INVALID JV_KIND_NULL JvKind = C.JV_KIND_NULL JV_KIND_FALSE JvKind = C.JV_KIND_FALSE JV_KIND_TRUE JvKind = C.JV_KIND_TRUE JV_KIND_NUMBER JvKind = C.JV_KIND_NUMBER JV_KIND_STRING JvKind = C.JV_KIND_STRING JV_KIND_ARRAY JvKind = C.JV_KIND_ARRAY JV_KIND_OBJECT JvKind = C.JV_KIND_OBJECT )
type JvPrintFlags ¶
type JvPrintFlags int
const ( JvPrintNone JvPrintFlags = 0 // None of the below JvPrintPretty JvPrintFlags = C.JV_PRINT_PRETTY // Print across multiple lines JvPrintAscii JvPrintFlags = C.JV_PRINT_ASCII // Escape non-ascii printable characters JvPrintColour JvPrintFlags = C.JV_PRINT_COLOUR // Include ANSI color escapes based on data types JvPrintSorted JvPrintFlags = C.JV_PRINT_SORTED // Sort output keys JvPrintInvalid JvPrintFlags = C.JV_PRINT_INVALID // Print invalid as "<invalid>" JvPrintRefCount JvPrintFlags = C.JV_PRINT_REFCOUNT // Display refcount of objects in in parenthesis JvPrintTab JvPrintFlags = C.JV_PRINT_TAB // Indent with tabs, JvPrintIsATty JvPrintFlags = C.JV_PRINT_ISATTY // JvPrintSpace0 JvPrintFlags = C.JV_PRINT_SPACE0 // Indent with zero extra chars beyond the parent bracket JvPrintSpace1 JvPrintFlags = C.JV_PRINT_SPACE1 // Indent with zero extra chars beyond the parent bracket JvPrintSpace2 JvPrintFlags = C.JV_PRINT_SPACE2 // Indent with zero extra chars beyond the parent bracket )