Skip to content

Commit

Permalink
Adding another xml test; spelling fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Sep 26, 2023
1 parent 05ad1da commit 0cf3adf
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 19 deletions.
2 changes: 1 addition & 1 deletion debian/changelog
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]> Tue, 29 Jun 2021 21:32:14 +0200
Expand Down
2 changes: 1 addition & 1 deletion how-it-works.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions pkg/yqlib/doc/operators/pick.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ myMap:
cat: meow
dog: bark
thing: hamster
hamster: squeek
hamster: squeak
```
then
```bash
Expand All @@ -22,7 +22,7 @@ yq '.myMap |= pick(["hamster", "cat", "goat"])' sample.yml
will output
```yaml
myMap:
hamster: squeek
hamster: squeak
cat: meow
```
Expand Down
19 changes: 19 additions & 0 deletions pkg/yqlib/doc/usage/xml.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<zoo><thing><frog>boing</frog></thing></zoo>
```
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.

Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/encoder_xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion pkg/yqlib/encoder_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/yqlib/operator_pick_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
{
Expand Down
7 changes: 7 additions & 0 deletions pkg/yqlib/xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<zoo><thing><frog>boing</frog></thing></zoo>",
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.",
Expand Down
6 changes: 0 additions & 6 deletions project-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ gota
goversion
GOVERSION
haha
headcommentwas
hellno
herbygillot
hexdump
Expand Down Expand Up @@ -138,7 +137,6 @@ mitchellh
mktemp
multidoc
multimaint
multine
myenv
myenvnonexisting
myfile
Expand All @@ -161,7 +159,6 @@ Oneshot
opencollect
opstack
orderedmap
original
osarch
overridign
pacman
Expand Down Expand Up @@ -199,11 +196,9 @@ shellvars
shortfunc
shortpipe
shunit
Sidenote
snapcraft
somevalue
splt
squeek
srcdir
stackoverflow
stiched
Expand All @@ -227,7 +222,6 @@ timezones
Timezones
tojson
Tokenvalue
traver
tsvd
Tuan
tzdata
Expand Down

0 comments on commit 0cf3adf

Please sign in to comment.