From 2362451fda54e5a83c170e02be75b438d3ad2ab9 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 30 Sep 2022 11:22:58 +1000 Subject: [PATCH] Added comment operator examples --- pkg/yqlib/doc/operators/comment-operators.md | 80 ++++++++++++++++++++ pkg/yqlib/operator_comments_test.go | 67 ++++++++++++++++ 2 files changed, 147 insertions(+) diff --git a/pkg/yqlib/doc/operators/comment-operators.md b/pkg/yqlib/doc/operators/comment-operators.md index 0cbc547a..51dc17b3 100644 --- a/pkg/yqlib/doc/operators/comment-operators.md +++ b/pkg/yqlib/doc/operators/comment-operators.md @@ -66,6 +66,86 @@ a: cat # cat b: dog # dog ``` +## Where is the comment - map key example +The underlying yaml parser can assign comments in a document to surprising nodes. Use an expression like this to find where you comment is. 'p' indicates the path, 'isKey' is if the node is a map key (as opposed to a map value). +From this, you can see the 'hello-world-comment' is actually on the 'hello' key + +Given a sample.yml file of: +```yaml +hello: # hello-world-comment + message: world +``` +then +```bash +yq '[... | {"p": path | join("."), "isKey": is_key, "hc": headComment, "lc": lineComment, "fc": footComment}]' sample.yml +``` +will output +```yaml +- p: "" + isKey: false + hc: "" + lc: "" + fc: "" +- p: hello + isKey: true + hc: "" + lc: hello-world-comment + fc: "" +- p: hello + isKey: false + hc: "" + lc: "" + fc: "" +- p: hello.message + isKey: true + hc: "" + lc: "" + fc: "" +- p: hello.message + isKey: false + hc: "" + lc: "" + fc: "" +``` + +## Where is the comment - array example +The underlying yaml parser can assign comments in a document to surprising nodes. Use an expression like this to find where you comment is. 'p' indicates the path, 'isKey' is if the node is a map key (as opposed to a map value). +From this, you can see the 'under-name-comment' is actually on the first child + +Given a sample.yml file of: +```yaml +name: + # under-name-comment + - first-array-child +``` +then +```bash +yq '[... | {"p": path | join("."), "isKey": is_key, "hc": headComment, "lc": lineComment, "fc": footComment}]' sample.yml +``` +will output +```yaml +- p: "" + isKey: false + hc: "" + lc: "" + fc: "" +- p: name + isKey: true + hc: "" + lc: "" + fc: "" +- p: name + isKey: false + hc: "" + lc: "" + fc: "" +- p: name.0 + isKey: false + hc: under-name-comment + lc: "" + fc: "" +``` + ## Set head comment Given a sample.yml file of: ```yaml diff --git a/pkg/yqlib/operator_comments_test.go b/pkg/yqlib/operator_comments_test.go index 8c6f1238..6b8dcb61 100644 --- a/pkg/yqlib/operator_comments_test.go +++ b/pkg/yqlib/operator_comments_test.go @@ -4,6 +4,55 @@ import ( "testing" ) +var expectedWhereIsMyCommentMapKey = `D0, P[], (!!seq)::- p: "" + isKey: false + hc: "" + lc: "" + fc: "" +- p: hello + isKey: true + hc: "" + lc: hello-world-comment + fc: "" +- p: hello + isKey: false + hc: "" + lc: "" + fc: "" +- p: hello.message + isKey: true + hc: "" + lc: "" + fc: "" +- p: hello.message + isKey: false + hc: "" + lc: "" + fc: "" +` + +var expectedWhereIsMyCommentArray = `D0, P[], (!!seq)::- p: "" + isKey: false + hc: "" + lc: "" + fc: "" +- p: name + isKey: true + hc: "" + lc: "" + fc: "" +- p: name + isKey: false + hc: "" + lc: "" + fc: "" +- p: name.0 + isKey: false + hc: under-name-comment + lc: "" + fc: "" +` + var commentOperatorScenarios = []expressionScenario{ { description: "Set line comment", @@ -56,6 +105,24 @@ var commentOperatorScenarios = []expressionScenario{ "D0, P[], (!!map)::a: cat # cat\n# cat\n\n# cat\nb: dog # dog\n# dog\n\n# dog\n", }, }, + { + description: "Where is the comment - map key example", + subdescription: "The underlying yaml parser can assign comments in a document to surprising nodes. Use an expression like this to find where you comment is. 'p' indicates the path, 'isKey' is if the node is a map key (as opposed to a map value).\nFrom this, you can see the 'hello-world-comment' is actually on the 'hello' key", + document: "hello: # hello-world-comment\n message: world", + expression: `[... | {"p": path | join("."), "isKey": is_key, "hc": headComment, "lc": lineComment, "fc": footComment}]`, + expected: []string{ + expectedWhereIsMyCommentMapKey, + }, + }, + { + description: "Where is the comment - array example", + subdescription: "The underlying yaml parser can assign comments in a document to surprising nodes. Use an expression like this to find where you comment is. 'p' indicates the path, 'isKey' is if the node is a map key (as opposed to a map value).\nFrom this, you can see the 'under-name-comment' is actually on the first child", + document: "name:\n # under-name-comment\n - first-array-child", + expression: `[... | {"p": path | join("."), "isKey": is_key, "hc": headComment, "lc": lineComment, "fc": footComment}]`, + expected: []string{ + expectedWhereIsMyCommentArray, + }, + }, { description: "Set head comment", document: `a: cat`,