From 00c2be541d7db3ad4a837b7cfafd39576829649a Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Wed, 11 Jan 2023 12:19:46 +1100 Subject: [PATCH] Added prepend array example --- pkg/yqlib/doc/operators/add.md | 17 +++++++++++++++++ pkg/yqlib/operator_add_test.go | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/pkg/yqlib/doc/operators/add.md b/pkg/yqlib/doc/operators/add.md index ecadb84f..469e13d8 100644 --- a/pkg/yqlib/doc/operators/add.md +++ b/pkg/yqlib/doc/operators/add.md @@ -86,6 +86,23 @@ will output a: ['dog', 'cat'] ``` +## Prepend to existing array +Given a sample.yml file of: +```yaml +a: + - dog +``` +then +```bash +yq '.a = ["cat"] + .a' sample.yml +``` +will output +```yaml +a: + - cat + - dog +``` + ## Add new object to array Given a sample.yml file of: ```yaml diff --git a/pkg/yqlib/operator_add_test.go b/pkg/yqlib/operator_add_test.go index b7452512..99a84845 100644 --- a/pkg/yqlib/operator_add_test.go +++ b/pkg/yqlib/operator_add_test.go @@ -101,6 +101,14 @@ var addOperatorScenarios = []expressionScenario{ "D0, P[], (doc)::a: ['dog', 'cat']\n", }, }, + { + description: "Prepend to existing array", + document: `a: [dog]`, + expression: `.a = ["cat"] + .a`, + expected: []string{ + "D0, P[], (doc)::a: [cat, dog]\n", + }, + }, { skipDoc: true, description: "Concatenate to existing array",