Skip to content

Commit 8c3a8c0

Browse files
committed
Fixes xml decode missing tags #1284
1 parent b9a1ef8 commit 8c3a8c0

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

pkg/yqlib/decoder_xml.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (dec *xmlDecoder) Init(reader io.Reader) {
4343
}
4444

4545
func (dec *xmlDecoder) createSequence(nodes []*xmlNode) (*yaml.Node, error) {
46-
yamlNode := &yaml.Node{Kind: yaml.SequenceNode}
46+
yamlNode := &yaml.Node{Kind: yaml.SequenceNode, Tag: "!!seq"}
4747
for _, child := range nodes {
4848
yamlChild, err := dec.convertToYamlNode(child)
4949
if err != nil {
@@ -64,7 +64,7 @@ func (dec *xmlDecoder) processComment(c string) string {
6464

6565
func (dec *xmlDecoder) createMap(n *xmlNode) (*yaml.Node, error) {
6666
log.Debug("createMap: headC: %v, footC: %v", n.HeadComment, n.FootComment)
67-
yamlNode := &yaml.Node{Kind: yaml.MappingNode}
67+
yamlNode := &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map"}
6868

6969
if len(n.Data) > 0 {
7070
label := dec.contentName

pkg/yqlib/operator_pick.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ func pickOperator(d *dataTreeNavigator, context Context, expressionNode *Express
6767
node := unwrapDoc(candidate.Node)
6868

6969
var replacement *yaml.Node
70-
if node.Tag == "!!map" {
70+
if node.Kind == yaml.MappingNode {
7171
replacement = pickMap(node, indicesToPick)
72-
} else if node.Tag == "!!seq" {
72+
} else if node.Kind == yaml.SequenceNode {
7373
replacement, err = pickSequence(node, indicesToPick)
7474
if err != nil {
7575
return Context{}, err

pkg/yqlib/operator_pick_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ var pickOperatorScenarios = []expressionScenario{
1414
"D0, P[], (doc)::myMap: {hamster: squeek, cat: meow}\n",
1515
},
1616
},
17+
{
18+
description: "Pick keys from map",
19+
skipDoc: true,
20+
document: "!things myMap: {cat: meow, dog: bark, thing: hamster, hamster: squeek}\n",
21+
expression: `.myMap |= pick(["hamster", "cat", "goat"])`,
22+
expected: []string{
23+
"D0, P[], (doc)::!things myMap: {hamster: squeek, cat: meow}\n",
24+
},
25+
},
1726
{
1827
description: "Pick keys from map with comments",
1928
skipDoc: true,

pkg/yqlib/xml_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,22 @@ var xmlScenarios = []formatScenario{
292292
expected: "<cat>purrs</cat>\n",
293293
scenarioType: "encode",
294294
},
295+
{
296+
description: "includes map tags",
297+
skipDoc: true,
298+
input: "<cat>purrs</cat>\n",
299+
expression: `tag`,
300+
expected: "!!map\n",
301+
scenarioType: "decode",
302+
},
303+
{
304+
description: "includes array tags",
305+
skipDoc: true,
306+
input: "<cat>purrs</cat><cat>purrs</cat>\n",
307+
expression: `.cat | tag`,
308+
expected: "!!seq\n",
309+
scenarioType: "decode",
310+
},
295311
{
296312
description: "Encode xml: array",
297313
input: "pets:\n cat:\n - purrs\n - meows",

0 commit comments

Comments
 (0)