Example #1
0
File: jq.go Project: wxf4150/jq-go
// compileJq compiles a program into the jq interpreter
func compileJq(jq *C.jq_state, src string) error {
	var msg *C.char
	C.set_err_cb(jq, &msg)
	defer C.set_err_cb(jq, nil)
	csrc := C.CString(src)
	defer C.free(unsafe.Pointer(csrc))
	// TODO: use a SyntaxError type to split up Error from Expr
	if C.jq_compile(jq, csrc) == 0 {
		return errors.New(C.GoString(msg))
	}
	return nil
}
Example #2
0
File: jq.go Project: vantroy/jq
// NewJq returns an initialized state with a compiled program
// program should be a valid jq program/filter
// see http://stedolan.github.io/jq/manual/#Basicfilters for more info
func newJqExecutor(program string) (*jqExecutor, error) {
	jq := &jqExecutor{
		state: C.jq_init(),
	}

	if jq.state == nil {
		return nil, errJqStateNil
	}

	pgm := C.CString(program)
	defer C.free(unsafe.Pointer(pgm))

	// Compiles a program into jq_state.
	if ok := C.jq_compile(jq.state, pgm); ok != 1 {
		jq.free()
		return nil, errInvalidProgram
	}

	return jq, nil
}
Example #3
0
File: jq.go Project: wxf4150/go-jq
func (jq *JQ) compile(program string) error {
	_ = C.jq_compile(jq.state, C.CString(program))
	return nil
}