Skip to content

Exact, fallible destructuring #3307

@musjj

Description

@musjj

There's currently no way to assert if the structure of an object exactly matches a pattern, even with the alternate destructuring ?//:

$ jq -r '. as [$foo] | $foo' <<<'["foo"]'
foo

$ jq -r '. as [$foo] | $foo' <<<'["foo", "bar"]'
foo

$ jq -r '. as [$foo,$bar] ?// [$bar] | $bar' <<<'["foo"]'
null

It would be helpful if there's a destructuring operator that will throw an error if there are no exact matches. Perhaps it could look something like:

$ jq -r '. is [$foo] | $foo' <<<'["foo", "bar"]'
jq: error (at <stdin>:1): Cannot destructure array with pattern [$foo]

$ jq -r '. is [$foo,$bar] ?// [$bar] | $bar' <<<'["foo"]'
foo

$ jq -r '. is [$foo,$bar] ?// [$bar] | $bar' <<<'["foo","bar","baz"]'
jq: error (at <stdin>:1): Cannot destructure array with pattern [$bar]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions