mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
wip
This commit is contained in:
parent
277ba1fe5d
commit
2529a4708a
@ -63,14 +63,14 @@ func (dec *xmlDecoder) createSequence(nodes []*xmlNode) (*yaml.Node, error) {
|
|||||||
|
|
||||||
func (dec *xmlDecoder) createMap(n *xmlNode) (*yaml.Node, error) {
|
func (dec *xmlDecoder) createMap(n *xmlNode) (*yaml.Node, error) {
|
||||||
log.Debug("createMap: headC: %v, footC: %v", n.HeadComment, n.FootComment)
|
log.Debug("createMap: headC: %v, footC: %v", n.HeadComment, n.FootComment)
|
||||||
yamlNode := &yaml.Node{Kind: yaml.MappingNode, HeadComment: n.HeadComment}
|
yamlNode := &yaml.Node{Kind: yaml.MappingNode, HeadComment: n.HeadComment, FootComment: n.FootComment}
|
||||||
|
|
||||||
if len(n.Data) > 0 {
|
if len(n.Data) > 0 {
|
||||||
label := dec.contentPrefix
|
label := dec.contentPrefix
|
||||||
yamlNode.Content = append(yamlNode.Content, createScalarNode(label, label), createScalarNode(n.Data, n.Data))
|
yamlNode.Content = append(yamlNode.Content, createScalarNode(label, label), createScalarNode(n.Data, n.Data))
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, keyValuePair := range n.Children {
|
for _, keyValuePair := range n.Children {
|
||||||
label := keyValuePair.K
|
label := keyValuePair.K
|
||||||
children := keyValuePair.V
|
children := keyValuePair.V
|
||||||
labelNode := createScalarNode(label, label)
|
labelNode := createScalarNode(label, label)
|
||||||
@ -88,9 +88,6 @@ func (dec *xmlDecoder) createMap(n *xmlNode) (*yaml.Node, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if i == len(n.Children)-1 {
|
|
||||||
valueNode.FootComment = n.FootComment
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
yamlNode.Content = append(yamlNode.Content, labelNode, valueNode)
|
yamlNode.Content = append(yamlNode.Content, labelNode, valueNode)
|
||||||
}
|
}
|
||||||
@ -105,6 +102,7 @@ func (dec *xmlDecoder) convertToYamlNode(n *xmlNode) (*yaml.Node, error) {
|
|||||||
scalar := createScalarNode(n.Data, n.Data)
|
scalar := createScalarNode(n.Data, n.Data)
|
||||||
log.Debug("scalar headC: %v, footC: %v", n.HeadComment, n.FootComment)
|
log.Debug("scalar headC: %v, footC: %v", n.HeadComment, n.FootComment)
|
||||||
scalar.LineComment = n.HeadComment
|
scalar.LineComment = n.HeadComment
|
||||||
|
scalar.FootComment = n.FootComment
|
||||||
|
|
||||||
return scalar, nil
|
return scalar, nil
|
||||||
}
|
}
|
||||||
@ -221,10 +219,21 @@ func (dec *xmlDecoder) decodeXml(root *xmlNode) error {
|
|||||||
elem = elem.parent
|
elem = elem.parent
|
||||||
case xml.Comment:
|
case xml.Comment:
|
||||||
|
|
||||||
commentStr := trimNonGraphic(string(xml.CharData(se)))
|
commentStr := string(xml.CharData(se))
|
||||||
if elem.state == "started" {
|
if elem.state == "started" {
|
||||||
log.Debug("got a foot comment for %v: %v", elem.label, commentStr)
|
log.Debug("got a foot comment for %v: %v", elem.label, commentStr)
|
||||||
elem.n.FootComment = commentStr
|
// elem.n.FootComment = elem.n.FootComment + commentStr
|
||||||
|
// put the comment on the foot of the last child
|
||||||
|
if len(elem.n.Children) > 0 {
|
||||||
|
|
||||||
|
child := elem.n.Children[len(elem.n.Children)-1]
|
||||||
|
log.Debug("putting it here: %v", child.K)
|
||||||
|
child.V[0].FootComment = child.V[0].FootComment + commentStr
|
||||||
|
} else {
|
||||||
|
log.Debug("putting it on the element")
|
||||||
|
elem.n.FootComment = elem.n.FootComment + commentStr
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
log.Debug("got a head comment for %v: %v", elem.label, commentStr)
|
log.Debug("got a head comment for %v: %v", elem.label, commentStr)
|
||||||
elem.n.HeadComment = joinFilter([]string{elem.n.HeadComment, commentStr})
|
elem.n.HeadComment = joinFilter([]string{elem.n.HeadComment, commentStr})
|
||||||
|
@ -22,224 +22,6 @@ XML nodes that have attributes then plain content, e.g:
|
|||||||
|
|
||||||
The content of the node will be set as a field in the map with the key "+content". Use the `--xml-content-name` flag to change this.
|
The content of the node will be set as a field in the map with the key "+content". Use the `--xml-content-name` flag to change this.
|
||||||
|
|
||||||
## Parse xml: simple
|
|
||||||
Given a sample.xml file of:
|
|
||||||
```xml
|
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<cat>meow</cat>
|
|
||||||
```
|
|
||||||
then
|
|
||||||
```bash
|
|
||||||
yq e -p=xml '.' sample.xml
|
|
||||||
```
|
|
||||||
will output
|
|
||||||
```yaml
|
|
||||||
cat: meow
|
|
||||||
```
|
|
||||||
|
|
||||||
## Parse xml: array
|
|
||||||
Consecutive nodes with identical xml names are assumed to be arrays.
|
|
||||||
|
|
||||||
Given a sample.xml file of:
|
|
||||||
```xml
|
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<animal>1</animal>
|
|
||||||
<animal>2</animal>
|
|
||||||
```
|
|
||||||
then
|
|
||||||
```bash
|
|
||||||
yq e -p=xml '.' sample.xml
|
|
||||||
```
|
|
||||||
will output
|
|
||||||
```yaml
|
|
||||||
animal:
|
|
||||||
- "1"
|
|
||||||
- "2"
|
|
||||||
```
|
|
||||||
|
|
||||||
## Parse xml: attributes
|
|
||||||
Attributes are converted to fields, with the attribute prefix.
|
|
||||||
|
|
||||||
Given a sample.xml file of:
|
|
||||||
```xml
|
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<cat legs="4">
|
|
||||||
<legs>7</legs>
|
|
||||||
</cat>
|
|
||||||
```
|
|
||||||
then
|
|
||||||
```bash
|
|
||||||
yq e -p=xml '.' sample.xml
|
|
||||||
```
|
|
||||||
will output
|
|
||||||
```yaml
|
|
||||||
cat:
|
|
||||||
+legs: "4"
|
|
||||||
legs: "7"
|
|
||||||
```
|
|
||||||
|
|
||||||
## Parse xml: attributes with content
|
|
||||||
Content is added as a field, using the content name
|
|
||||||
|
|
||||||
Given a sample.xml file of:
|
|
||||||
```xml
|
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<cat legs="4">meow</cat>
|
|
||||||
```
|
|
||||||
then
|
|
||||||
```bash
|
|
||||||
yq e -p=xml '.' sample.xml
|
|
||||||
```
|
|
||||||
will output
|
|
||||||
```yaml
|
|
||||||
cat:
|
|
||||||
+content: meow
|
|
||||||
+legs: "4"
|
|
||||||
```
|
|
||||||
|
|
||||||
## Parse xml: with comments
|
|
||||||
A best attempt is made to preserve comments.
|
|
||||||
|
|
||||||
Given a sample.xml file of:
|
|
||||||
```xml
|
|
||||||
|
|
||||||
<!-- before cat -->
|
|
||||||
<cat>
|
|
||||||
<!-- in cat before -->
|
|
||||||
<x>3<!-- multi
|
|
||||||
line comment
|
|
||||||
for x --></x>
|
|
||||||
<y>
|
|
||||||
<!-- in y before -->
|
|
||||||
<d><!-- in d before -->4<!-- in d after --></d>
|
|
||||||
<!-- in y after -->
|
|
||||||
</y>
|
|
||||||
<!-- in_cat_after -->
|
|
||||||
</cat>
|
|
||||||
<!-- after cat -->
|
|
||||||
|
|
||||||
```
|
|
||||||
then
|
|
||||||
```bash
|
|
||||||
yq e -p=xml '.' sample.xml
|
|
||||||
```
|
|
||||||
will output
|
|
||||||
```yaml
|
|
||||||
# before cat
|
|
||||||
cat:
|
|
||||||
# in cat before
|
|
||||||
x: "3" # multi
|
|
||||||
# line comment
|
|
||||||
# for x
|
|
||||||
y:
|
|
||||||
# in y before
|
|
||||||
d: "4" # in d before in d after
|
|
||||||
# in y after
|
|
||||||
|
|
||||||
# after cat
|
|
||||||
```
|
|
||||||
|
|
||||||
## Encode xml: simple
|
|
||||||
Given a sample.yml file of:
|
|
||||||
```yaml
|
|
||||||
cat: purrs
|
|
||||||
```
|
|
||||||
then
|
|
||||||
```bash
|
|
||||||
yq e -o=xml '.' sample.yml
|
|
||||||
```
|
|
||||||
will output
|
|
||||||
```xml
|
|
||||||
<cat>purrs</cat>
|
|
||||||
```
|
|
||||||
|
|
||||||
## Encode xml: array
|
|
||||||
Given a sample.yml file of:
|
|
||||||
```yaml
|
|
||||||
pets:
|
|
||||||
cat:
|
|
||||||
- purrs
|
|
||||||
- meows
|
|
||||||
```
|
|
||||||
then
|
|
||||||
```bash
|
|
||||||
yq e -o=xml '.' sample.yml
|
|
||||||
```
|
|
||||||
will output
|
|
||||||
```xml
|
|
||||||
<pets>
|
|
||||||
<cat>purrs</cat>
|
|
||||||
<cat>meows</cat>
|
|
||||||
</pets>
|
|
||||||
```
|
|
||||||
|
|
||||||
## Encode xml: attributes
|
|
||||||
Fields with the matching xml-attribute-prefix are assumed to be attributes.
|
|
||||||
|
|
||||||
Given a sample.yml file of:
|
|
||||||
```yaml
|
|
||||||
cat:
|
|
||||||
+name: tiger
|
|
||||||
meows: true
|
|
||||||
|
|
||||||
```
|
|
||||||
then
|
|
||||||
```bash
|
|
||||||
yq e -o=xml '.' sample.yml
|
|
||||||
```
|
|
||||||
will output
|
|
||||||
```xml
|
|
||||||
<cat name="tiger">
|
|
||||||
<meows>true</meows>
|
|
||||||
</cat>
|
|
||||||
```
|
|
||||||
|
|
||||||
## Encode xml: attributes with content
|
|
||||||
Fields with the matching xml-content-name is assumed to be content.
|
|
||||||
|
|
||||||
Given a sample.yml file of:
|
|
||||||
```yaml
|
|
||||||
cat:
|
|
||||||
+name: tiger
|
|
||||||
+content: cool
|
|
||||||
|
|
||||||
```
|
|
||||||
then
|
|
||||||
```bash
|
|
||||||
yq e -o=xml '.' sample.yml
|
|
||||||
```
|
|
||||||
will output
|
|
||||||
```xml
|
|
||||||
<cat name="tiger">cool</cat>
|
|
||||||
```
|
|
||||||
|
|
||||||
## Encode xml: comments
|
|
||||||
A best attempt is made to copy comments to xml.
|
|
||||||
|
|
||||||
Given a sample.yml file of:
|
|
||||||
```yaml
|
|
||||||
# above_cat
|
|
||||||
cat: # inline_cat
|
|
||||||
# above_array
|
|
||||||
array: # inline_array
|
|
||||||
- val1 # inline_val1
|
|
||||||
# above_val2
|
|
||||||
- val2 # inline_val2
|
|
||||||
# below_cat
|
|
||||||
|
|
||||||
```
|
|
||||||
then
|
|
||||||
```bash
|
|
||||||
yq e -o=xml '.' sample.yml
|
|
||||||
```
|
|
||||||
will output
|
|
||||||
```xml
|
|
||||||
<!-- above_cat inline_cat--><cat><!-- above_array inline_array-->
|
|
||||||
<array><!-- inline_val1-->val1</array>
|
|
||||||
<array><!-- above_val2 inline_val2-->val2</array>
|
|
||||||
</cat><!-- below_cat-->
|
|
||||||
```
|
|
||||||
|
|
||||||
## Round trip: with comments
|
## Round trip: with comments
|
||||||
A best effort is made, but comment positions and white space are not preserved perfectly.
|
A best effort is made, but comment positions and white space are not preserved perfectly.
|
||||||
|
|
||||||
@ -252,6 +34,7 @@ Given a sample.xml file of:
|
|||||||
<x>3<!-- multi
|
<x>3<!-- multi
|
||||||
line comment
|
line comment
|
||||||
for x --></x>
|
for x --></x>
|
||||||
|
<!-- before y -->
|
||||||
<y>
|
<y>
|
||||||
<!-- in y before -->
|
<!-- in y before -->
|
||||||
<d><!-- in d before -->4<!-- in d after --></d>
|
<d><!-- in d before -->4<!-- in d after --></d>
|
||||||
@ -268,17 +51,19 @@ yq e -p=xml '.' sample.xml
|
|||||||
```
|
```
|
||||||
will output
|
will output
|
||||||
```yaml
|
```yaml
|
||||||
# before cat
|
# before cat
|
||||||
cat:
|
cat:
|
||||||
# in cat before
|
# in cat before
|
||||||
x: "3" # multi
|
x: "3" # multi
|
||||||
# line comment
|
# line comment
|
||||||
# for x
|
# for x
|
||||||
y:
|
# before y
|
||||||
# in y before
|
|
||||||
d: "4" # in d before in d after
|
|
||||||
# in y after
|
|
||||||
|
|
||||||
# after cat
|
y:
|
||||||
|
# in y before
|
||||||
|
d: "4" # in d before in d after
|
||||||
|
# in y after
|
||||||
|
|
||||||
|
# after cat
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -49,11 +49,12 @@ func (e *xmlEncoder) Encode(writer io.Writer, node *yaml.Node) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case yaml.DocumentNode:
|
case yaml.DocumentNode:
|
||||||
|
log.Debugf("ENCODING DOCUMENT NODE")
|
||||||
err := e.encodeComment(encoder, headAndLineComment(node))
|
err := e.encodeComment(encoder, headAndLineComment(node))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// this used to call encode...
|
log.Debugf("OK NOW THE ACTUAL")
|
||||||
err = e.encodeTopLevelMap(encoder, unwrapDoc(node))
|
err = e.encodeTopLevelMap(encoder, unwrapDoc(node))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -78,16 +79,23 @@ func (e *xmlEncoder) Encode(writer io.Writer, node *yaml.Node) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *xmlEncoder) encodeTopLevelMap(encoder *xml.Encoder, node *yaml.Node) error {
|
func (e *xmlEncoder) encodeTopLevelMap(encoder *xml.Encoder, node *yaml.Node) error {
|
||||||
|
err := e.encodeComment(encoder, headAndLineComment(node))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
for i := 0; i < len(node.Content); i += 2 {
|
for i := 0; i < len(node.Content); i += 2 {
|
||||||
key := node.Content[i]
|
key := node.Content[i]
|
||||||
value := node.Content[i+1]
|
value := node.Content[i+1]
|
||||||
|
|
||||||
start := xml.StartElement{Name: xml.Name{Local: key.Value}}
|
start := xml.StartElement{Name: xml.Name{Local: key.Value}}
|
||||||
|
log.Debugf("comments of key %v", key.Value)
|
||||||
err := e.encodeComment(encoder, headAndLineComment(key))
|
err := e.encodeComment(encoder, headAndLineComment(key))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Debugf("recursing")
|
||||||
|
|
||||||
err = e.doEncode(encoder, value, start)
|
err = e.doEncode(encoder, value, start)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -97,7 +105,7 @@ func (e *xmlEncoder) encodeTopLevelMap(encoder *xml.Encoder, node *yaml.Node) er
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return e.encodeComment(encoder, footComment(node))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *xmlEncoder) encodeStart(encoder *xml.Encoder, node *yaml.Node, start xml.StartElement) error {
|
func (e *xmlEncoder) encodeStart(encoder *xml.Encoder, node *yaml.Node, start xml.StartElement) error {
|
||||||
@ -141,6 +149,7 @@ func (e *xmlEncoder) doEncode(encoder *xml.Encoder, node *yaml.Node, start xml.S
|
|||||||
|
|
||||||
func (e *xmlEncoder) encodeComment(encoder *xml.Encoder, commentStr string) error {
|
func (e *xmlEncoder) encodeComment(encoder *xml.Encoder, commentStr string) error {
|
||||||
if commentStr != "" {
|
if commentStr != "" {
|
||||||
|
log.Debugf("encoding comment %v", commentStr)
|
||||||
var comment xml.Comment = []byte(commentStr)
|
var comment xml.Comment = []byte(commentStr)
|
||||||
err := encoder.EncodeToken(comment)
|
err := encoder.EncodeToken(comment)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -151,6 +160,7 @@ func (e *xmlEncoder) encodeComment(encoder *xml.Encoder, commentStr string) erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *xmlEncoder) encodeArray(encoder *xml.Encoder, node *yaml.Node, start xml.StartElement) error {
|
func (e *xmlEncoder) encodeArray(encoder *xml.Encoder, node *yaml.Node, start xml.StartElement) error {
|
||||||
|
e.encodeComment(encoder, headAndLineComment(node))
|
||||||
for i := 0; i < len(node.Content); i++ {
|
for i := 0; i < len(node.Content); i++ {
|
||||||
value := node.Content[i]
|
value := node.Content[i]
|
||||||
err := e.doEncode(encoder, value, start.Copy())
|
err := e.doEncode(encoder, value, start.Copy())
|
||||||
@ -158,10 +168,12 @@ func (e *xmlEncoder) encodeArray(encoder *xml.Encoder, node *yaml.Node, start xm
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
e.encodeComment(encoder, footComment(node))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *xmlEncoder) encodeMap(encoder *xml.Encoder, node *yaml.Node, start xml.StartElement) error {
|
func (e *xmlEncoder) encodeMap(encoder *xml.Encoder, node *yaml.Node, start xml.StartElement) error {
|
||||||
|
log.Debug("its a map")
|
||||||
|
|
||||||
//first find all the attributes and put them on the start token
|
//first find all the attributes and put them on the start token
|
||||||
for i := 0; i < len(node.Content); i += 2 {
|
for i := 0; i < len(node.Content); i += 2 {
|
||||||
@ -201,11 +213,19 @@ func (e *xmlEncoder) encodeMap(encoder *xml.Encoder, node *yaml.Node, start xml.
|
|||||||
}
|
}
|
||||||
} else if key.Value == e.contentName {
|
} else if key.Value == e.contentName {
|
||||||
// directly encode the contents
|
// directly encode the contents
|
||||||
|
err = e.encodeComment(encoder, headAndLineComment(value))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
var charData xml.CharData = []byte(value.Value)
|
var charData xml.CharData = []byte(value.Value)
|
||||||
err = encoder.EncodeToken(charData)
|
err = encoder.EncodeToken(charData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
err = e.encodeComment(encoder, footComment(value))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
err = e.encodeComment(encoder, footComment(key))
|
err = e.encodeComment(encoder, footComment(key))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -100,7 +100,6 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if p.firstTimePrinting {
|
if p.firstTimePrinting {
|
||||||
log.Debugf("its my first time *blush*")
|
|
||||||
node := matchingNodes.Front().Value.(*CandidateNode)
|
node := matchingNodes.Front().Value.(*CandidateNode)
|
||||||
p.previousDocIndex = node.Document
|
p.previousDocIndex = node.Document
|
||||||
p.previousFileIndex = node.FileIndex
|
p.previousFileIndex = node.FileIndex
|
||||||
|
@ -65,6 +65,7 @@ var inputXmlWithComments = `
|
|||||||
<x>3<!-- multi
|
<x>3<!-- multi
|
||||||
line comment
|
line comment
|
||||||
for x --></x>
|
for x --></x>
|
||||||
|
<!-- before y -->
|
||||||
<y>
|
<y>
|
||||||
<!-- in y before -->
|
<!-- in y before -->
|
||||||
<d><!-- in d before -->4<!-- in d after --></d>
|
<d><!-- in d before -->4<!-- in d after --></d>
|
||||||
@ -89,14 +90,14 @@ cat:
|
|||||||
# after cat
|
# after cat
|
||||||
`
|
`
|
||||||
|
|
||||||
var expectedRoundtripXmlWithComments = `<!-- before cat--><cat><!--in cat before-->
|
var expectedRoundtripXmlWithComments = `<!-- before cat --><cat><!-- in cat before -->
|
||||||
<x><!--multi
|
<x><!-- multi
|
||||||
line comment
|
line comment
|
||||||
for x-->3</x>
|
for x -->3</x><!-- before y -->
|
||||||
<y><!--in y before-->
|
<y><!-- in y before -->
|
||||||
<d><!--in d before in d after-->4</d><!--in y after-->
|
<d><!-- in d before in d after -->4</d><!-- in y after -->
|
||||||
</y><!--in_cat_after-->
|
</y><!-- in_cat_after -->
|
||||||
</cat><!--after cat-->
|
</cat><!-- after cat -->
|
||||||
`
|
`
|
||||||
|
|
||||||
var yamlWithComments = `# above_cat
|
var yamlWithComments = `# above_cat
|
||||||
@ -116,75 +117,75 @@ var expectedXmlWithComments = `<!-- above_cat inline_cat--><cat><!-- above_array
|
|||||||
`
|
`
|
||||||
|
|
||||||
var xmlScenarios = []xmlScenario{
|
var xmlScenarios = []xmlScenario{
|
||||||
{
|
// {
|
||||||
description: "Parse xml: simple",
|
// description: "Parse xml: simple",
|
||||||
input: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<cat>meow</cat>",
|
// input: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<cat>meow</cat>",
|
||||||
expected: "D0, P[], (doc)::cat: meow\n",
|
// expected: "D0, P[], (doc)::cat: meow\n",
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
description: "Parse xml: array",
|
// description: "Parse xml: array",
|
||||||
subdescription: "Consecutive nodes with identical xml names are assumed to be arrays.",
|
// subdescription: "Consecutive nodes with identical xml names are assumed to be arrays.",
|
||||||
input: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animal>1</animal>\n<animal>2</animal>",
|
// input: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animal>1</animal>\n<animal>2</animal>",
|
||||||
expected: "D0, P[], (doc)::animal:\n - \"1\"\n - \"2\"\n",
|
// expected: "D0, P[], (doc)::animal:\n - \"1\"\n - \"2\"\n",
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
description: "Parse xml: attributes",
|
// description: "Parse xml: attributes",
|
||||||
subdescription: "Attributes are converted to fields, with the attribute prefix.",
|
// subdescription: "Attributes are converted to fields, with the attribute prefix.",
|
||||||
input: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<cat legs=\"4\">\n <legs>7</legs>\n</cat>",
|
// input: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<cat legs=\"4\">\n <legs>7</legs>\n</cat>",
|
||||||
expected: "D0, P[], (doc)::cat:\n +legs: \"4\"\n legs: \"7\"\n",
|
// expected: "D0, P[], (doc)::cat:\n +legs: \"4\"\n legs: \"7\"\n",
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
description: "Parse xml: attributes with content",
|
// description: "Parse xml: attributes with content",
|
||||||
subdescription: "Content is added as a field, using the content name",
|
// subdescription: "Content is added as a field, using the content name",
|
||||||
input: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<cat legs=\"4\">meow</cat>",
|
// input: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<cat legs=\"4\">meow</cat>",
|
||||||
expected: "D0, P[], (doc)::cat:\n +content: meow\n +legs: \"4\"\n",
|
// expected: "D0, P[], (doc)::cat:\n +content: meow\n +legs: \"4\"\n",
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
description: "Parse xml: with comments",
|
// description: "Parse xml: with comments",
|
||||||
subdescription: "A best attempt is made to preserve comments.",
|
// subdescription: "A best attempt is made to preserve comments.",
|
||||||
input: inputXmlWithComments,
|
// input: inputXmlWithComments,
|
||||||
expected: expectedDecodeYamlWithComments,
|
// expected: expectedDecodeYamlWithComments,
|
||||||
scenarioType: "decode",
|
// scenarioType: "decode",
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
description: "Encode xml: simple",
|
// description: "Encode xml: simple",
|
||||||
input: "cat: purrs",
|
// input: "cat: purrs",
|
||||||
expected: "<cat>purrs</cat>\n",
|
// expected: "<cat>purrs</cat>\n",
|
||||||
scenarioType: "encode",
|
// scenarioType: "encode",
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
description: "Encode xml: array",
|
// description: "Encode xml: array",
|
||||||
input: "pets:\n cat:\n - purrs\n - meows",
|
// input: "pets:\n cat:\n - purrs\n - meows",
|
||||||
expected: "<pets>\n <cat>purrs</cat>\n <cat>meows</cat>\n</pets>\n",
|
// expected: "<pets>\n <cat>purrs</cat>\n <cat>meows</cat>\n</pets>\n",
|
||||||
scenarioType: "encode",
|
// scenarioType: "encode",
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
description: "Encode xml: attributes",
|
// description: "Encode xml: attributes",
|
||||||
subdescription: "Fields with the matching xml-attribute-prefix are assumed to be attributes.",
|
// subdescription: "Fields with the matching xml-attribute-prefix are assumed to be attributes.",
|
||||||
input: "cat:\n +name: tiger\n meows: true\n",
|
// input: "cat:\n +name: tiger\n meows: true\n",
|
||||||
expected: "<cat name=\"tiger\">\n <meows>true</meows>\n</cat>\n",
|
// expected: "<cat name=\"tiger\">\n <meows>true</meows>\n</cat>\n",
|
||||||
scenarioType: "encode",
|
// scenarioType: "encode",
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
skipDoc: true,
|
// skipDoc: true,
|
||||||
input: "cat:\n ++name: tiger\n meows: true\n",
|
// input: "cat:\n ++name: tiger\n meows: true\n",
|
||||||
expected: "<cat +name=\"tiger\">\n <meows>true</meows>\n</cat>\n",
|
// expected: "<cat +name=\"tiger\">\n <meows>true</meows>\n</cat>\n",
|
||||||
scenarioType: "encode",
|
// scenarioType: "encode",
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
description: "Encode xml: attributes with content",
|
// description: "Encode xml: attributes with content",
|
||||||
subdescription: "Fields with the matching xml-content-name is assumed to be content.",
|
// subdescription: "Fields with the matching xml-content-name is assumed to be content.",
|
||||||
input: "cat:\n +name: tiger\n +content: cool\n",
|
// input: "cat:\n +name: tiger\n +content: cool\n",
|
||||||
expected: "<cat name=\"tiger\">cool</cat>\n",
|
// expected: "<cat name=\"tiger\">cool</cat>\n",
|
||||||
scenarioType: "encode",
|
// scenarioType: "encode",
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
description: "Encode xml: comments",
|
// description: "Encode xml: comments",
|
||||||
subdescription: "A best attempt is made to copy comments to xml.",
|
// subdescription: "A best attempt is made to copy comments to xml.",
|
||||||
input: yamlWithComments,
|
// input: yamlWithComments,
|
||||||
expected: expectedXmlWithComments,
|
// expected: expectedXmlWithComments,
|
||||||
scenarioType: "encode",
|
// scenarioType: "encode",
|
||||||
},
|
// },
|
||||||
{
|
{
|
||||||
description: "Round trip: with comments",
|
description: "Round trip: with comments",
|
||||||
subdescription: "A best effort is made, but comment positions and white space are not preserved perfectly.",
|
subdescription: "A best effort is made, but comment positions and white space are not preserved perfectly.",
|
||||||
|
@ -65,6 +65,6 @@ func AssertResultWithContext(t *testing.T, expectedValue interface{}, actualValu
|
|||||||
t.Helper()
|
t.Helper()
|
||||||
if expectedValue != actualValue {
|
if expectedValue != actualValue {
|
||||||
t.Error(context)
|
t.Error(context)
|
||||||
t.Error(": expected <", expectedValue, "> but got <", actualValue, ">")
|
t.Error(": expected <\n", strings.ReplaceAll(fmt.Sprintf("%v", expectedValue), " ", "@"), ">\n but got <\n", strings.ReplaceAll(fmt.Sprintf("%v", actualValue), " ", "@"), ">\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user