From 38666f4db63fa619401be0e50b4bedb26ef44e69 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Mon, 26 Apr 2021 14:17:52 +1000 Subject: [PATCH] Added another style example for doc --- pkg/yqlib/doc/Style.md | 38 ++++++++++++++++++++++++++++++++ pkg/yqlib/operator_style_test.go | 17 ++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/pkg/yqlib/doc/Style.md b/pkg/yqlib/doc/Style.md index a5094629..a8a220c1 100644 --- a/pkg/yqlib/doc/Style.md +++ b/pkg/yqlib/doc/Style.md @@ -1,4 +1,42 @@ The style operator can be used to get or set the style of nodes (e.g. string style, yaml style) +## Update and set style of a particular node (simple) +Given a sample.yml file of: +```yaml +a: + b: thing + c: something +``` +then +```bash +yq eval '.a.b = "new" | .a.b style="double"' sample.yml +``` +will output +```yaml +a: + b: "new" + c: something +``` + +## Update and set style of a particular node using path variables +You can use a variable to re-use a path + +Given a sample.yml file of: +```yaml +a: + b: thing + c: something +``` +then +```bash +yq eval '.a.b as $x | $x = "new" | $x style="double"' sample.yml +``` +will output +```yaml +a: + b: "new" + c: something +``` + ## Set tagged style Given a sample.yml file of: ```yaml diff --git a/pkg/yqlib/operator_style_test.go b/pkg/yqlib/operator_style_test.go index fa777f26..988b8382 100644 --- a/pkg/yqlib/operator_style_test.go +++ b/pkg/yqlib/operator_style_test.go @@ -5,6 +5,23 @@ import ( ) var styleOperatorScenarios = []expressionScenario{ + { + description: "Update and set style of a particular node (simple)", + document: `a: {b: thing, c: something}`, + expression: `.a.b = "new" | .a.b style="double"`, + expected: []string{ + "D0, P[], (doc)::a: {b: \"new\", c: something}\n", + }, + }, + { + description: "Update and set style of a particular node using path variables", + subdescription: "You can use a variable to re-use a path", + document: `a: {b: thing, c: something}`, + expression: `.a.b as $x | $x = "new" | $x style="double"`, + expected: []string{ + "D0, P[], (doc)::a: {b: \"new\", c: something}\n", + }, + }, { description: "Set tagged style", document: `{a: cat, b: 5, c: 3.2, e: true}`,