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}`,