From 0cf3adf5dd69d7b0f7903bf8d58f21354790e519 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 26 Sep 2023 14:43:08 +1000 Subject: [PATCH] Adding another xml test; spelling fixes --- debian/changelog | 2 +- how-it-works.md | 2 +- pkg/yqlib/doc/operators/pick.md | 4 ++-- pkg/yqlib/doc/usage/xml.md | 19 +++++++++++++++++++ pkg/yqlib/encoder_xml.go | 2 +- pkg/yqlib/encoder_yaml.go | 1 - pkg/yqlib/lexer.go | 2 +- pkg/yqlib/operator_pick_test.go | 12 ++++++------ pkg/yqlib/xml_test.go | 7 +++++++ project-words.txt | 6 ------ 10 files changed, 38 insertions(+), 19 deletions(-) diff --git a/debian/changelog b/debian/changelog index f0a4d512..e9ce1202 100644 --- a/debian/changelog +++ b/debian/changelog @@ -127,7 +127,7 @@ yq (4.9.6) focal; urgency=medium * Added darwin/arm64 build, thanks @alecthomas * Incremented docker alpine base version, thanks @da6d6i7-bronga - * Bug fix: multine expression + * Bug fix: multiline expression * Bug fix: special character -- Roberto Mier Escandon Tue, 29 Jun 2021 21:32:14 +0200 diff --git a/how-it-works.md b/how-it-works.md index 51a41fcd..f77a4cd9 100644 --- a/how-it-works.md +++ b/how-it-works.md @@ -29,7 +29,7 @@ It pipes the current, lets call it 'root' context through the `lhs` expression o cat ``` -Sidenote: this node holds not only its value 'cat', but comments and metadata too, including path and parent information. +Side note: this node holds not only its value 'cat', but comments and metadata too, including path and parent information. The `=` operator then pipes the 'root' context through the `rhs` expression of `.b` to return the node diff --git a/pkg/yqlib/doc/operators/pick.md b/pkg/yqlib/doc/operators/pick.md index 3b477699..fbe9bd95 100644 --- a/pkg/yqlib/doc/operators/pick.md +++ b/pkg/yqlib/doc/operators/pick.md @@ -13,7 +13,7 @@ myMap: cat: meow dog: bark thing: hamster - hamster: squeek + hamster: squeak ``` then ```bash @@ -22,7 +22,7 @@ yq '.myMap |= pick(["hamster", "cat", "goat"])' sample.yml will output ```yaml myMap: - hamster: squeek + hamster: squeak cat: meow ``` diff --git a/pkg/yqlib/doc/usage/xml.md b/pkg/yqlib/doc/usage/xml.md index 89bee794..2d469450 100644 --- a/pkg/yqlib/doc/usage/xml.md +++ b/pkg/yqlib/doc/usage/xml.md @@ -128,6 +128,25 @@ zoo: - cat ``` +## Parse xml: force all as an array +Because of the way yq works, when updating everything you need to update the children before the parents. By default `..` will match parents first, so we reverse that before updating. + +Given a sample.xml file of: +```xml +boing +``` +then +```bash +yq -oy '([..] | reverse | .[]) |= [] + .' sample.xml +``` +will output +```yaml +- zoo: + - thing: + - frog: + - boing +``` + ## Parse xml: attributes Attributes are converted to fields, with the default attribute prefix '+'. Use '--xml-attribute-prefix` to set your own. diff --git a/pkg/yqlib/encoder_xml.go b/pkg/yqlib/encoder_xml.go index 345e825e..2e48bd99 100644 --- a/pkg/yqlib/encoder_xml.go +++ b/pkg/yqlib/encoder_xml.go @@ -233,7 +233,7 @@ func (e *xmlEncoder) encodeComment(encoder *xml.Encoder, commentStr string) erro commentStr = chompRegexp.ReplaceAllString(commentStr, "") log.Debugf("chompRegexp [%v]", commentStr) commentStr = xmlEncodeMultilineCommentRegex.ReplaceAllString(commentStr, "$1$2") - log.Debugf("processed multine [%v]", commentStr) + log.Debugf("processed multiline [%v]", commentStr) // if the first line is non blank, add a space if commentStr[0] != '\n' && commentStr[0] != ' ' { commentStr = " " + commentStr diff --git a/pkg/yqlib/encoder_yaml.go b/pkg/yqlib/encoder_yaml.go index 75b64c04..e2a9943c 100644 --- a/pkg/yqlib/encoder_yaml.go +++ b/pkg/yqlib/encoder_yaml.go @@ -38,7 +38,6 @@ func (ye *yamlEncoder) PrintDocumentSeparator(writer io.Writer) error { } func (ye *yamlEncoder) PrintLeadingContent(writer io.Writer, content string) error { - // log.Debug("headcommentwas [%v]", content) reader := bufio.NewReader(strings.NewReader(content)) for { diff --git a/pkg/yqlib/lexer.go b/pkg/yqlib/lexer.go index 259dbd33..d118fa0f 100644 --- a/pkg/yqlib/lexer.go +++ b/pkg/yqlib/lexer.go @@ -175,7 +175,7 @@ func handleToken(tokens []*token, index int, postProcessedTokens []*token) (toke if index != len(tokens)-1 && currentToken.CheckForPostTraverse && tokens[index+1].TokenType == openCollect { - log.Debug(" adding traverArray because next is opencollect") + log.Debug(" adding traverseArray because next is opencollect") op := &Operation{OperationType: traverseArrayOpType} postProcessedTokens = append(postProcessedTokens, &token{TokenType: operationToken, Operation: op}) } diff --git a/pkg/yqlib/operator_pick_test.go b/pkg/yqlib/operator_pick_test.go index 25542385..fe92f3ad 100644 --- a/pkg/yqlib/operator_pick_test.go +++ b/pkg/yqlib/operator_pick_test.go @@ -8,28 +8,28 @@ var pickOperatorScenarios = []expressionScenario{ { description: "Pick keys from map", subdescription: "Note that the order of the keys matches the pick order and non existent keys are skipped.", - document: "myMap: {cat: meow, dog: bark, thing: hamster, hamster: squeek}\n", + document: "myMap: {cat: meow, dog: bark, thing: hamster, hamster: squeak}\n", expression: `.myMap |= pick(["hamster", "cat", "goat"])`, expected: []string{ - "D0, P[], (doc)::myMap: {hamster: squeek, cat: meow}\n", + "D0, P[], (doc)::myMap: {hamster: squeak, cat: meow}\n", }, }, { description: "Pick keys from map", skipDoc: true, - document: "!things myMap: {cat: meow, dog: bark, thing: hamster, hamster: squeek}\n", + document: "!things myMap: {cat: meow, dog: bark, thing: hamster, hamster: squeak}\n", expression: `.myMap |= pick(["hamster", "cat", "goat"])`, expected: []string{ - "D0, P[], (doc)::!things myMap: {hamster: squeek, cat: meow}\n", + "D0, P[], (doc)::!things myMap: {hamster: squeak, cat: meow}\n", }, }, { description: "Pick keys from map with comments", skipDoc: true, - document: "# abc\nmyMap: {cat: meow, dog: bark, thing: hamster, hamster: squeek}\n# xyz\n", + document: "# abc\nmyMap: {cat: meow, dog: bark, thing: hamster, hamster: squeak}\n# xyz\n", expression: `.myMap |= pick(["hamster", "cat", "goat"])`, expected: []string{ - "D0, P[], (doc)::# abc\nmyMap: {hamster: squeek, cat: meow}\n# xyz\n", + "D0, P[], (doc)::# abc\nmyMap: {hamster: squeak, cat: meow}\n# xyz\n", }, }, { diff --git a/pkg/yqlib/xml_test.go b/pkg/yqlib/xml_test.go index fdf50b98..0f74ab10 100644 --- a/pkg/yqlib/xml_test.go +++ b/pkg/yqlib/xml_test.go @@ -363,6 +363,13 @@ var xmlScenarios = []formatScenario{ expression: ".zoo.animal |= ([] + .)", expected: "zoo:\n animal:\n - cat\n", }, + { + description: "Parse xml: force all as an array", + subdescription: "Because of the way yq works, when updating everything you need to update the children before the parents. By default `..` will match parents first, so we reverse that before updating.", + input: "boing", + expression: "([..] | reverse | .[]) |= [] + .", + expected: "- zoo:\n - thing:\n - frog:\n - boing\n", + }, { description: "Parse xml: attributes", subdescription: "Attributes are converted to fields, with the default attribute prefix '+'. Use '--xml-attribute-prefix` to set your own.", diff --git a/project-words.txt b/project-words.txt index 00afd650..cbbad841 100644 --- a/project-words.txt +++ b/project-words.txt @@ -96,7 +96,6 @@ gota goversion GOVERSION haha -headcommentwas hellno herbygillot hexdump @@ -138,7 +137,6 @@ mitchellh mktemp multidoc multimaint -multine myenv myenvnonexisting myfile @@ -161,7 +159,6 @@ Oneshot opencollect opstack orderedmap -original osarch overridign pacman @@ -199,11 +196,9 @@ shellvars shortfunc shortpipe shunit -Sidenote snapcraft somevalue splt -squeek srcdir stackoverflow stiched @@ -227,7 +222,6 @@ timezones Timezones tojson Tokenvalue -traver tsvd Tuan tzdata