From b4b2e1217a216bfb8253e0685d844fda0d45eaa7 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Wed, 7 Jul 2021 19:53:33 +1000 Subject: [PATCH] Added another |= example --- pkg/yqlib/doc/Assign (Update).md | 20 +++++++++++++++++++- pkg/yqlib/operator_assign_test.go | 10 +++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/pkg/yqlib/doc/Assign (Update).md b/pkg/yqlib/doc/Assign (Update).md index 28983bcf..3cceb25e 100644 --- a/pkg/yqlib/doc/Assign (Update).md +++ b/pkg/yqlib/doc/Assign (Update).md @@ -28,6 +28,24 @@ a: g: foof ``` +## Double elements in an array +Given a sample.yml file of: +```yaml +- 1 +- 2 +- 3 +``` +then +```bash +yq eval '.[] |= . * 2' sample.yml +``` +will output +```yaml +- 2 +- 4 +- 6 +``` + ## Update node from another file Note this will also work when the second file is a scalar (string/number) @@ -75,7 +93,7 @@ c: fieldC ``` then ```bash -yq eval '(.a, .c) |= "potatoe"' sample.yml +yq eval '(.a, .c) = "potatoe"' sample.yml ``` will output ```yaml diff --git a/pkg/yqlib/operator_assign_test.go b/pkg/yqlib/operator_assign_test.go index e9dd7485..a1de311d 100644 --- a/pkg/yqlib/operator_assign_test.go +++ b/pkg/yqlib/operator_assign_test.go @@ -36,6 +36,14 @@ var assignOperatorScenarios = []expressionScenario{ "D0, P[], (doc)::{a: {g: foof}}\n", }, }, + { + description: "Double elements in an array", + document: `[1,2,3]`, + expression: `.[] |= . * 2`, + expected: []string{ + "D0, P[], (doc)::[2, 4, 6]\n", + }, + }, { description: "Update node from another file", subdescription: "Note this will also work when the second file is a scalar (string/number)", @@ -57,7 +65,7 @@ var assignOperatorScenarios = []expressionScenario{ { description: "Updated multiple paths", document: `{a: fieldA, b: fieldB, c: fieldC}`, - expression: `(.a, .c) |= "potatoe"`, + expression: `(.a, .c) = "potatoe"`, expected: []string{ "D0, P[], (doc)::{a: potatoe, b: fieldB, c: potatoe}\n", },