Adding another xml test; spelling fixes

This commit is contained in:
Mike Farah 2023-09-26 14:43:08 +10:00
parent 05ad1dad90
commit 0cf3adf5dd
10 changed files with 38 additions and 19 deletions

2
debian/changelog vendored
View File

@ -127,7 +127,7 @@ yq (4.9.6) focal; urgency=medium
* Added darwin/arm64 build, thanks @alecthomas * Added darwin/arm64 build, thanks @alecthomas
* Incremented docker alpine base version, thanks @da6d6i7-bronga * Incremented docker alpine base version, thanks @da6d6i7-bronga
* Bug fix: multine expression * Bug fix: multiline expression
* Bug fix: special character * Bug fix: special character
-- Roberto Mier Escandon <rmescandon@gmail.com> Tue, 29 Jun 2021 21:32:14 +0200 -- Roberto Mier Escandon <rmescandon@gmail.com> Tue, 29 Jun 2021 21:32:14 +0200

View File

@ -29,7 +29,7 @@ It pipes the current, lets call it 'root' context through the `lhs` expression o
cat 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 The `=` operator then pipes the 'root' context through the `rhs` expression of `.b` to return the node

View File

@ -13,7 +13,7 @@ myMap:
cat: meow cat: meow
dog: bark dog: bark
thing: hamster thing: hamster
hamster: squeek hamster: squeak
``` ```
then then
```bash ```bash
@ -22,7 +22,7 @@ yq '.myMap |= pick(["hamster", "cat", "goat"])' sample.yml
will output will output
```yaml ```yaml
myMap: myMap:
hamster: squeek hamster: squeak
cat: meow cat: meow
``` ```

View File

@ -128,6 +128,25 @@ zoo:
- cat - 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
<zoo><thing><frog>boing</frog></thing></zoo>
```
then
```bash
yq -oy '([..] | reverse | .[]) |= [] + .' sample.xml
```
will output
```yaml
- zoo:
- thing:
- frog:
- boing
```
## Parse xml: attributes ## Parse xml: attributes
Attributes are converted to fields, with the default attribute prefix '+'. Use '--xml-attribute-prefix` to set your own. Attributes are converted to fields, with the default attribute prefix '+'. Use '--xml-attribute-prefix` to set your own.

View File

@ -233,7 +233,7 @@ func (e *xmlEncoder) encodeComment(encoder *xml.Encoder, commentStr string) erro
commentStr = chompRegexp.ReplaceAllString(commentStr, "") commentStr = chompRegexp.ReplaceAllString(commentStr, "")
log.Debugf("chompRegexp [%v]", commentStr) log.Debugf("chompRegexp [%v]", commentStr)
commentStr = xmlEncodeMultilineCommentRegex.ReplaceAllString(commentStr, "$1$2") 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 the first line is non blank, add a space
if commentStr[0] != '\n' && commentStr[0] != ' ' { if commentStr[0] != '\n' && commentStr[0] != ' ' {
commentStr = " " + commentStr commentStr = " " + commentStr

View File

@ -38,7 +38,6 @@ func (ye *yamlEncoder) PrintDocumentSeparator(writer io.Writer) error {
} }
func (ye *yamlEncoder) PrintLeadingContent(writer io.Writer, content string) error { func (ye *yamlEncoder) PrintLeadingContent(writer io.Writer, content string) error {
// log.Debug("headcommentwas [%v]", content)
reader := bufio.NewReader(strings.NewReader(content)) reader := bufio.NewReader(strings.NewReader(content))
for { for {

View File

@ -175,7 +175,7 @@ func handleToken(tokens []*token, index int, postProcessedTokens []*token) (toke
if index != len(tokens)-1 && currentToken.CheckForPostTraverse && if index != len(tokens)-1 && currentToken.CheckForPostTraverse &&
tokens[index+1].TokenType == openCollect { 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} op := &Operation{OperationType: traverseArrayOpType}
postProcessedTokens = append(postProcessedTokens, &token{TokenType: operationToken, Operation: op}) postProcessedTokens = append(postProcessedTokens, &token{TokenType: operationToken, Operation: op})
} }

View File

@ -8,28 +8,28 @@ var pickOperatorScenarios = []expressionScenario{
{ {
description: "Pick keys from map", description: "Pick keys from map",
subdescription: "Note that the order of the keys matches the pick order and non existent keys are skipped.", 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"])`, expression: `.myMap |= pick(["hamster", "cat", "goat"])`,
expected: []string{ 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", description: "Pick keys from map",
skipDoc: true, 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"])`, expression: `.myMap |= pick(["hamster", "cat", "goat"])`,
expected: []string{ 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", description: "Pick keys from map with comments",
skipDoc: true, 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"])`, expression: `.myMap |= pick(["hamster", "cat", "goat"])`,
expected: []string{ 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",
}, },
}, },
{ {

View File

@ -363,6 +363,13 @@ var xmlScenarios = []formatScenario{
expression: ".zoo.animal |= ([] + .)", expression: ".zoo.animal |= ([] + .)",
expected: "zoo:\n animal:\n - cat\n", 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: "<zoo><thing><frog>boing</frog></thing></zoo>",
expression: "([..] | reverse | .[]) |= [] + .",
expected: "- zoo:\n - thing:\n - frog:\n - boing\n",
},
{ {
description: "Parse xml: attributes", description: "Parse xml: attributes",
subdescription: "Attributes are converted to fields, with the default attribute prefix '+'. Use '--xml-attribute-prefix` to set your own.", subdescription: "Attributes are converted to fields, with the default attribute prefix '+'. Use '--xml-attribute-prefix` to set your own.",

View File

@ -96,7 +96,6 @@ gota
goversion goversion
GOVERSION GOVERSION
haha haha
headcommentwas
hellno hellno
herbygillot herbygillot
hexdump hexdump
@ -138,7 +137,6 @@ mitchellh
mktemp mktemp
multidoc multidoc
multimaint multimaint
multine
myenv myenv
myenvnonexisting myenvnonexisting
myfile myfile
@ -161,7 +159,6 @@ Oneshot
opencollect opencollect
opstack opstack
orderedmap orderedmap
original
osarch osarch
overridign overridign
pacman pacman
@ -199,11 +196,9 @@ shellvars
shortfunc shortfunc
shortpipe shortpipe
shunit shunit
Sidenote
snapcraft snapcraft
somevalue somevalue
splt splt
squeek
srcdir srcdir
stackoverflow stackoverflow
stiched stiched
@ -227,7 +222,6 @@ timezones
Timezones Timezones
tojson tojson
Tokenvalue Tokenvalue
traver
tsvd tsvd
Tuan Tuan
tzdata tzdata