Skip to content

Commit

Permalink
expr/const: Always wrap strings when printing them
Browse files Browse the repository at this point in the history
This seems more logical, and is more in line with what jq does anyways.
  • Loading branch information
slotThe committed Aug 11, 2024
1 parent bf4a3a5 commit 6fc723c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ $ cat simple.json | rq 'filter (get "age" | (>= 42)) | map (\x -> { x.name: x.ag
[{"John Doe":43},{"Bob":42}]

$ cargo metadata --format-version=1 | rq '.packages | map .name'
[ahash, allocator-api2, anyhow, ariadne, cc, cfg-if, chumsky, hashbrown, libc, once_cell, proc-macro2, psm, quote, rq, stacker, syn, unicode-ident, unicode-width, version_check, winapi, winapi-i686-pc-windows-gnu, winapi-x86_64-pc-windows-gnu, yansi, zerocopy, zerocopy-derive]
["ahash", "aho-corasick", "allocator-api2", "anyhow", "ariadne", "cc", "cfg-if", "chumsky", "hashbrown", "libc", "memchr", "once_cell", "proc-macro2", "psm", "quote", "regex-automata", "regex-syntax", "rq", "serde", "serde_derive", "stacker", "syn", "unicode-ident", "unicode-width", "version_check", "winapi", "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", "yansi", "zerocopy", "zerocopy-derive"]
```

## The expression language
Expand Down
2 changes: 1 addition & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Display for Const {
Const::Num(n) => write!(f, "{n}"),
Const::Bool(b) => write!(f, "{b}"),
Const::Null => write!(f, "null"),
Const::String(s) => write!(f, "{s}"),
Const::String(s) => write!(f, "\"{s}\""),
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,13 @@ mod test {
cli_test!(
Command::new("cargo").args(["metadata", "--format-version=1"]),
".packages | map .name",
"[ahash, aho-corasick, allocator-api2, anyhow, ariadne, cc, cfg-if, chumsky, \
hashbrown, libc, memchr, once_cell, proc-macro2, psm, quote, regex-automata, \
regex-syntax, rq, serde, serde_derive, stacker, syn, unicode-ident, \
unicode-width, version_check, winapi, winapi-i686-pc-windows-gnu, \
winapi-x86_64-pc-windows-gnu, yansi, zerocopy, zerocopy-derive]"
"[\"ahash\", \"aho-corasick\", \"allocator-api2\", \"anyhow\", \"ariadne\", \
\"cc\", \"cfg-if\", \"chumsky\", \"hashbrown\", \"libc\", \"memchr\", \
\"once_cell\", \"proc-macro2\", \"psm\", \"quote\", \"regex-automata\", \
\"regex-syntax\", \"rq\", \"serde\", \"serde_derive\", \"stacker\", \"syn\", \
\"unicode-ident\", \"unicode-width\", \"version_check\", \"winapi\", \
\"winapi-i686-pc-windows-gnu\", \"winapi-x86_64-pc-windows-gnu\", \"yansi\", \
\"zerocopy\", \"zerocopy-derive\"]"
)
}

Expand All @@ -199,7 +201,7 @@ mod test {
cli_test!(
Command::new("echo").arg(SIMPLE),
"filter (get \"age\" | (>= 42)) | map (|x| { x.name: x.age })",
"[{John Doe: 43}, {Bob: 42}]"
"[{\"John Doe\": 43}, {\"Bob\": 42}]"
)
}

Expand Down
6 changes: 1 addition & 5 deletions src/util/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ pub fn flatten(json: &Expr) -> Vec<String> {
fn flatten_worker(json: &Expr, mut prefix: String, res: &mut Vec<String>) {
match json {
Expr::Const(c) => {
let wrap = match c {
Const::String(_) => "\"",
_ => "",
};
prefix.push_str(&format!(" = {wrap}{c}{wrap};"));
prefix.push_str(&format!(" = {c};"));
res.push(prefix);
},
Expr::Arr(xs) => {
Expand Down

0 comments on commit 6fc723c

Please sign in to comment.