...

Source file src/github.com/aws/smithy-go/testing/xml/sort.go

Documentation: github.com/aws/smithy-go/testing/xml

     1  package xml
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/xml"
     6  	"io"
     7  	"strings"
     8  )
     9  
    10  type xmlAttrSlice []xml.Attr
    11  
    12  func (x xmlAttrSlice) Len() int {
    13  	return len(x)
    14  }
    15  
    16  func (x xmlAttrSlice) Less(i, j int) bool {
    17  	spaceI, spaceJ := x[i].Name.Space, x[j].Name.Space
    18  	localI, localJ := x[i].Name.Local, x[j].Name.Local
    19  	valueI, valueJ := x[i].Value, x[j].Value
    20  
    21  	spaceCmp := strings.Compare(spaceI, spaceJ)
    22  	localCmp := strings.Compare(localI, localJ)
    23  	valueCmp := strings.Compare(valueI, valueJ)
    24  
    25  	if spaceCmp == -1 || (spaceCmp == 0 && (localCmp == -1 || (localCmp == 0 && valueCmp == -1))) {
    26  		return true
    27  	}
    28  
    29  	return false
    30  }
    31  
    32  func (x xmlAttrSlice) Swap(i, j int) {
    33  	x[i], x[j] = x[j], x[i]
    34  }
    35  
    36  // SortXML sorts the reader's XML elements
    37  func SortXML(r io.Reader, ignoreIndentation bool) (string, error) {
    38  	var buf bytes.Buffer
    39  	d := xml.NewDecoder(r)
    40  	root, err := XMLToStruct(d, nil, ignoreIndentation)
    41  	if err != nil {
    42  		return buf.String(), err
    43  	}
    44  
    45  	e := xml.NewEncoder(&buf)
    46  	err = StructToXML(e, root, true)
    47  	return buf.String(), err
    48  }
    49  

View as plain text