-
-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
103 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Formatting Expressions | ||
|
||
`From version v4.41+` | ||
|
||
You can put expressions into `.yq` files, use whitespace and comments to break up complex expressions and explain what's going on. | ||
|
||
## Using expression files and comments | ||
Given a sample.yaml file of: | ||
```yaml | ||
a: | ||
b: old | ||
``` | ||
And an 'update.yq' expression file of: | ||
```bash | ||
# This is a yq expression that updates the map | ||
# for several great reasons outlined here. | ||
|
||
.a.b = "new" # line comment here | ||
| .a.c = "frog" | ||
|
||
# Now good things will happen. | ||
``` | ||
then | ||
```bash | ||
yq --from-file update.yq sample.yml | ||
``` | ||
will output | ||
```yaml | ||
a: | ||
b: new | ||
c: frog | ||
``` | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Formatting Expressions | ||
|
||
`From version v4.41+` | ||
|
||
You can put expressions into `.yq` files, use whitespace and comments to break up complex expressions and explain what's going on. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package yqlib | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/mikefarah/yq/v4/test" | ||
) | ||
|
||
var formattingExpressionScenarios = []formatScenario{ | ||
{ | ||
description: "Using expression files and comments", | ||
input: "a:\n b: old", | ||
expression: "\n# This is a yq expression that updates the map\n# for several great reasons outlined here.\n\n.a.b = \"new\" # line comment here\n| .a.c = \"frog\"\n\n# Now good things will happen.\n", | ||
expected: "a:\n b: new\n c: frog\n", | ||
}, | ||
} | ||
|
||
func documentExpressionScenario(_ *testing.T, w *bufio.Writer, i interface{}) { | ||
s := i.(formatScenario) | ||
|
||
if s.skipDoc { | ||
return | ||
} | ||
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description)) | ||
|
||
if s.subdescription != "" { | ||
writeOrPanic(w, s.subdescription) | ||
writeOrPanic(w, "\n\n") | ||
} | ||
|
||
writeOrPanic(w, "Given a sample.yaml file of:\n") | ||
writeOrPanic(w, fmt.Sprintf("```yaml\n%v\n```\n", s.input)) | ||
|
||
writeOrPanic(w, "And an 'update.yq' expression file of:\n") | ||
writeOrPanic(w, fmt.Sprintf("```bash%v```\n", s.expression)) | ||
|
||
writeOrPanic(w, "then\n") | ||
writeOrPanic(w, "```bash\nyq --from-file update.yq sample.yml\n```\n") | ||
writeOrPanic(w, "will output\n") | ||
|
||
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewYamlEncoder(2, false, ConfiguredYamlPreferences)))) | ||
} | ||
|
||
func TestExpressionCommentScenarios(t *testing.T) { | ||
for _, tt := range formattingExpressionScenarios { | ||
test.AssertResultComplexWithContext(t, tt.expected, | ||
mustProcessFormatScenario(tt, NewYamlDecoder(ConfiguredYamlPreferences), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), | ||
tt.description) | ||
} | ||
genericScenarios := make([]interface{}, len(formattingExpressionScenarios)) | ||
for i, s := range formattingExpressionScenarios { | ||
genericScenarios[i] = s | ||
} | ||
documentScenarios(t, "usage", "formatting-expressions", genericScenarios, documentExpressionScenario) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters