From 78af68f4363fc9ee7d2428374673d88603b4803d Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 10 Jun 2021 08:31:26 +1000 Subject: [PATCH] Added nested traversal examples --- pkg/yqlib/doc/Traverse (Read).md | 31 ++++++++++++++++++++++++ pkg/yqlib/operator_traverse_path_test.go | 16 ++++++++++++ 2 files changed, 47 insertions(+) diff --git a/pkg/yqlib/doc/Traverse (Read).md b/pkg/yqlib/doc/Traverse (Read).md index b949ba45..56dca175 100644 --- a/pkg/yqlib/doc/Traverse (Read).md +++ b/pkg/yqlib/doc/Traverse (Read).md @@ -63,6 +63,22 @@ will output frog ``` +## Multiple special characters +Given a sample.yml file of: +```yaml +a: + "key.withdots": + "another.key": apple +``` +then +```bash +yq eval '.a["key.withdots"]["another.key"]' sample.yml +``` +will output +```yaml +apple +``` + ## Keys with spaces Use quotes with brackets around path elements with special characters @@ -211,6 +227,21 @@ will output 1 ``` +## Traversing nested arrays by index +Given a sample.yml file of: +```yaml +- [] +- - cat +``` +then +```bash +yq eval '.[1][0]' sample.yml +``` +will output +```yaml +cat +``` + ## Maps with numeric keys Given a sample.yml file of: ```yaml diff --git a/pkg/yqlib/operator_traverse_path_test.go b/pkg/yqlib/operator_traverse_path_test.go index ce0928de..cafc7398 100644 --- a/pkg/yqlib/operator_traverse_path_test.go +++ b/pkg/yqlib/operator_traverse_path_test.go @@ -77,6 +77,14 @@ var traversePathOperatorScenarios = []expressionScenario{ "D0, P[{}], (!!str)::frog\n", }, }, + { + description: "Multiple special characters", + document: `a: {"key.withdots": {"another.key": apple}}`, + expression: `.a["key.withdots"]["another.key"]`, + expected: []string{ + "D0, P[a key.withdots another.key], (!!str)::apple\n", + }, + }, { description: "Keys with spaces", subdescription: "Use quotes with brackets around path elements with special characters", @@ -244,6 +252,14 @@ var traversePathOperatorScenarios = []expressionScenario{ "D0, P[0], (!!int)::1\n", }, }, + { + description: "Traversing nested arrays by index", + document: `[[], [cat]]`, + expression: `.[1][0]`, + expected: []string{ + "D0, P[1 0], (!!str)::cat\n", + }, + }, { description: "Maps with numeric keys", document: `{2: cat}`,