1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//! ## Overview
//!
//! > Prior to v0.4.0 this crate was named [json-query].
//!
//! This rust crate provides access to [jq] 1.6 via the `libjq` C API (rather than
//! "shelling out").
//!
//! By leveraging [jq] we can extract data from json strings using `jq`'s dsl.
//!
//! This crate requires Rust **1.32** or above.
//!
//! ## Usage
//!
//! The interface provided by this crate is very basic. You supply a jq program
//! string and a string to run the program over.
//!
//! ```rust
//! use jq_rs;
//! // ...
//!
//! let res = jq_rs::run(".name", r#"{"name": "test"}"#);
//! assert_eq!(res.unwrap(), "\"test\"\n".to_string());
//! ```
//!
//! In addition to running one-off programs with `jq_rs::run()`, you can also
//! use `jq_rs::compile()` to compile a jq program and reuse it with
//! different inputs.
//!
//! ```rust
//! use jq_rs;
//!
//! let tv_shows = r#"[
//!     {"title": "Twilight Zone"},
//!     {"title": "X-Files"},
//!     {"title": "The Outer Limits"}
//! ]"#;
//!
//! let movies = r#"[
//!     {"title": "The Omen"},
//!     {"title": "Amityville Horror"},
//!     {"title": "The Thing"}
//! ]"#;
//!
//! let mut program = jq_rs::compile("[.[].title] | sort").unwrap();
//!
//! assert_eq!(
//!     &program.run(tv_shows).unwrap(),
//!     "[\"The Outer Limits\",\"Twilight Zone\",\"X-Files\"]\n"
//! );
//!
//! assert_eq!(
//!     &program.run(movies).unwrap(),
//!     "[\"Amityville Horror\",\"The Omen\",\"The Thing\"]\n",
//! );
//! ```
//!
//! ## A Note on Performance
//!
//! While the benchmarks are far from exhaustive, they indicate that much of the
//! runtime of a simple jq program goes to the compilation. In fact, the compilation
//! is _quite expensive_.
//!
//! ```text
//! run one off             time:   [48.594 ms 48.689 ms 48.800 ms]
//! Found 6 outliers among 100 measurements (6.00%)
//!   3 (3.00%) high mild
//!   3 (3.00%) high severe
//!
//! run pre-compiled        time:   [4.0351 us 4.0708 us 4.1223 us]
//! Found 15 outliers among 100 measurements (15.00%)
//!   6 (6.00%) high mild
//!   9 (9.00%) high severe
//! ```
//!
//! If you have a need to run the same jq program multiple times it is
//! _highly recommended_ to retain a pre-compiled `JqProgram` and reuse it.
//!
//! ## Handling Output
//!
//! The return values from jq are _strings_ since there is no certainty that the
//! output will be valid json. As such the output will need to be parsed if you want
//! to work with the actual data types being represented.
//!
//! In such cases you may want to pair this crate with [serde_json] or similar.
//!
//! For example, here we want to extract the numbers from a set of objects:
//!
//! ```rust
//! use jq_rs;
//! use serde_json::{self, json};
//!
//! // ...
//!
//! let data = json!({
//!     "movies": [
//!         { "title": "Coraline", "year": 2009 },
//!         { "title": "ParaNorman", "year": 2012 },
//!         { "title": "Boxtrolls", "year": 2014 },
//!         { "title": "Kubo and the Two Strings", "year": 2016 },
//!         { "title": "Missing Link", "year": 2019 }
//!     ]
//! });
//!
//! let query = "[.movies[].year]";
//! // program output as a json string...
//! let output = jq_rs::run(query, &data.to_string()).unwrap();
//! // ... parse via serde
//! let parsed: Vec<i64> = serde_json::from_str(&output).unwrap();
//!
//! assert_eq!(vec![2009, 2012, 2014, 2016, 2019], parsed);
//! ```
//!
//! Barely any of the options or flags available from the [jq] cli are exposed
//! currently.
//! Literally all that is provided is the ability to execute a _jq program_ on a blob
//! of json.
//! Please pardon my dust as I sort out the details.
//!
//! ## Linking to libjq
//!
//! This crate requires access to `libjq` at build and/or runtime depending on the
//! your choice.
//!
//! When the `bundled` feature is enabled (**off by default**) `libjq` is provided
//! and linked statically to your crate by [jq-sys] and [jq-src]. Using this feature
//! requires having autotools and gcc in `PATH` in order for the to build to work.
//!
//! Without the `bundled` feature, _you_ will need to ensure your crate
//! can link to `libjq` in order for the bindings to work.
//!
//! You can choose to compile `libjq` yourself, or perhaps install it via your
//! system's package manager.
//! See the [jq-sys building docs][jq-sys-building] for details on how to share
//! hints with the [jq-sys] crate on how to link.
//!
//! [jq]: https://github.com/stedolan/jq
//! [serde_json]: https://github.com/serde-rs/json
//! [jq-rs]: https://crates.io/crates/jq-rs
//! [json-query]: https://crates.io/crates/json-query
//! [jq-sys]: https://github.com/onelson/jq-sys
//! [jq-sys-building]: https://github.com/onelson/jq-sys#building
//! [jq-src]: https://github.com/onelson/jq-src

#![deny(missing_docs)]

extern crate jq_sys;
#[cfg(test)]
#[macro_use]
extern crate serde_json;

mod errors;
mod jq;

use std::ffi::CString;

pub use errors::{Error, Result};

/// Run a jq program on a blob of json data.
///
/// In the case of failure to run the program, feedback from the jq api will be
/// available in the supplied `String` value.
/// Failures can occur for a variety of reasons, but mostly you'll see them as
/// a result of bad jq program syntax, or invalid json data.
pub fn run(program: &str, data: &str) -> Result<String> {
    compile(program)?.run(data)
}

/// A pre-compiled jq program which can be run against different inputs.
pub struct JqProgram {
    jq: jq::Jq,
}

impl JqProgram {
    /// Runs a json string input against a pre-compiled jq program.
    pub fn run(&mut self, data: &str) -> Result<String> {
        if data.trim().is_empty() {
            // During work on #4, #7, the parser test which allows us to avoid a memory
            // error shows that an empty input just yields an empty response BUT our
            // implementation would yield a parse error.
            return Ok("".into());
        }
        let input = CString::new(data)?;
        self.jq.execute(input)
    }
}

/// Compile a jq program then reuse it, running several inputs against it.
pub fn compile(program: &str) -> Result<JqProgram> {
    let prog = CString::new(program)?;
    Ok(JqProgram {
        jq: jq::Jq::compile_program(prog)?,
    })
}

#[cfg(test)]
mod test {

    use super::{compile, run, Error};
    use matches::assert_matches;
    use serde_json;

    #[test]
    fn reuse_compiled_program() {
        let query = r#"if . == 0 then "zero" elif . == 1 then "one" else "many" end"#;
        let mut prog = compile(&query).unwrap();
        assert_eq!(prog.run("2").unwrap(), "\"many\"\n");
        assert_eq!(prog.run("1").unwrap(), "\"one\"\n");
        assert_eq!(prog.run("0").unwrap(), "\"zero\"\n");
    }

    #[test]
    fn jq_state_is_not_global() {
        let input = r#"{"id": 123, "name": "foo"}"#;
        let query1 = r#".name"#;
        let query2 = r#".id"#;

        // Basically this test is just to check that the state pointers returned by
        // `jq::init()` are completely independent and don't share any global state.
        let mut prog1 = compile(&query1).unwrap();
        let mut prog2 = compile(&query2).unwrap();

        assert_eq!(prog1.run(input).unwrap(), "\"foo\"\n");
        assert_eq!(prog2.run(input).unwrap(), "123\n");
        assert_eq!(prog1.run(input).unwrap(), "\"foo\"\n");
        assert_eq!(prog2.run(input).unwrap(), "123\n");
    }

    fn get_movies() -> serde_json::Value {
        json!({
            "movies": [
                { "title": "Coraline", "year": 2009 },
                { "title": "ParaNorman", "year": 2012 },
                { "title": "Boxtrolls", "year": 2014 },
                { "title": "Kubo and the Two Strings", "year": 2016 },
                { "title": "Missing Link", "year": 2019 }
            ]
        })
    }

    #[test]
    fn identity_nothing() {
        assert_eq!(run(".", "").unwrap(), "".to_string());
    }

    #[test]
    fn identity_empty() {
        assert_eq!(run(".", "{}").unwrap(), "{}\n".to_string());
    }

    #[test]
    fn extract_dates() {
        let data = get_movies();
        let query = "[.movies[].year]";
        let output = run(query, &data.to_string()).unwrap();
        let parsed: Vec<i64> = serde_json::from_str(&output).unwrap();
        assert_eq!(vec![2009, 2012, 2014, 2016, 2019], parsed);
    }

    #[test]
    fn extract_name() {
        let res = run(".name", r#"{"name": "test"}"#);
        assert_eq!(res.unwrap(), "\"test\"\n".to_string());
    }

    #[test]
    fn unpack_array() {
        let res = run(".[]", "[1,2,3]");
        assert_eq!(res.unwrap(), "1\n2\n3\n".to_string());
    }

    #[test]
    fn compile_error() {
        let res = run(". aa12312me  dsaafsdfsd", "{\"name\": \"test\"}");
        assert_matches!(res, Err(Error::InvalidProgram));
    }

    #[test]
    fn parse_error() {
        let res = run(".", "{1233 invalid json ahoy : est\"}");
        assert_matches!(res, Err(Error::System { .. }));
    }

    #[test]
    fn just_open_brace() {
        let res = run(".", "{");
        assert_matches!(res, Err(Error::System { .. }));
    }

    #[test]
    fn just_close_brace() {
        let res = run(".", "}");
        assert_matches!(res, Err(Error::System { .. }));
    }

    #[test]
    fn total_garbage() {
        let data = r#"
        {
            moreLike: "an object literal but also bad"
            loveToDangleComma: true,
        }"#;

        let res = run(".", data);
        assert_matches!(res, Err(Error::System { .. }));
    }

    pub mod mem_errors {
        //! Attempting run a program resulting in bad field access has been
        //! shown to sometimes trigger a use after free or double free memory
        //! error.
        //!
        //! Technically the program and inputs are both valid, but the
        //! evaluation of the program causes bad memory access to happen.
        //!
        //! https://github.com/onelson/json-query/issues/4

        use super::*;

        #[test]
        fn missing_field_access() {
            let prog = ".[] | .hello";
            let data = "[1,2,3]";
            let res = run(prog, data);
            assert_matches!(res, Err(Error::System { .. }));
        }

        #[test]
        fn missing_field_access_compiled() {
            let mut prog = compile(".[] | .hello").unwrap();
            let data = "[1,2,3]";
            let res = prog.run(data);
            assert_matches!(res, Err(Error::System { .. }));
        }
    }
}