Skip to content

Commit

Permalink
feedback around xml Node decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
skotambkar committed Aug 20, 2020
1 parent 7f83194 commit eadd63b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
10 changes: 5 additions & 5 deletions xml/xml_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ type NodeDecoder struct {
StartEl xml.StartElement
}

// NewNodeDecoder returns a ptr to an initialized XMLNodeDecoder
func NewNodeDecoder(decoder *xml.Decoder, startEl xml.StartElement) *NodeDecoder {
return &NodeDecoder{
// WrapNodeDecoder returns an initialized XMLNodeDecoder
func WrapNodeDecoder(decoder *xml.Decoder, startEl xml.StartElement) NodeDecoder {
return NodeDecoder{
Decoder: decoder,
StartEl: startEl,
}
Expand All @@ -24,7 +24,7 @@ func NewNodeDecoder(decoder *xml.Decoder, startEl xml.StartElement) *NodeDecoder
// Token on a Node Decoder returns a xml StartElement. It returns a boolean that indicates the
// a token is the node decoder's end node token; and an error which indicates any error
// that occurred while retrieving the start element
func (d *NodeDecoder) Token() (t xml.StartElement, done bool, err error) {
func (d NodeDecoder) Token() (t xml.StartElement, done bool, err error) {
for {
token, e := d.Decoder.Token()
if e != nil {
Expand All @@ -50,7 +50,7 @@ func (d *NodeDecoder) Token() (t xml.StartElement, done bool, err error) {
// Value provides an abstraction to retrieve char data value within an xml element.
// The method will return an error if it encounters a nested xml element instead of char data.
// This method should only be used to retrieve simple type or blob shape values as []byte.
func (d *NodeDecoder) Value() (c []byte, done bool, err error) {
func (d NodeDecoder) Value() (c []byte, done bool, err error) {
t, e := d.Decoder.Token()
if e != nil {
return c, done, e
Expand Down
6 changes: 3 additions & 3 deletions xml/xml_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestXMLNodeDecoder_Token(t *testing.T) {
t.Fatalf("expected error to contain %v, found %v", e, a.Error())
}
}
nodeDecoder := NewNodeDecoder(xmlDecoder, st)
nodeDecoder := WrapNodeDecoder(xmlDecoder, st)
token, done, err := nodeDecoder.Token()
if err != nil {
if len(c.expectedError) == 0 {
Expand Down Expand Up @@ -101,7 +101,7 @@ func TestXMLNodeDecoder_TokenExample(t *testing.T) {
}

// nodeDecoder will track <Struct> tag as root node of the document
nodeDecoder := NewNodeDecoder(xmlDecoder, st)
nodeDecoder := WrapNodeDecoder(xmlDecoder, st)

// Retrieves <Response> tag
token, done, err := nodeDecoder.Token()
Expand Down Expand Up @@ -185,7 +185,7 @@ func TestXMLNodeDecoder_Value(t *testing.T) {
t.Fatalf("expected error to contain %v, found %v", e, a.Error())
}
}
nodeDecoder := NewNodeDecoder(xmlDecoder, st)
nodeDecoder := WrapNodeDecoder(xmlDecoder, st)
token, done, err := nodeDecoder.Value()
if err != nil {
if len(c.expectedError) == 0 {
Expand Down
11 changes: 8 additions & 3 deletions xml/xml_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func GetResponseErrorCode(r io.Reader, noErrorWrapping bool) (string, error) {
}

if noErrorWrapping {
var errResponse errorWrapper
var errResponse errorBody
err := xml.Unmarshal(rb, &errResponse)
if err != nil {
return "", fmt.Errorf("error while fetching xml error response code: %w", err)
Expand All @@ -30,10 +30,15 @@ func GetResponseErrorCode(r io.Reader, noErrorWrapping bool) (string, error) {
return errResponse.Err.Code, nil
}

// errorResponse represents the outer error response body
// i.e. <ErrorResponse>...</ErrorResponse>
type errorResponse struct {
Err errorWrapper `xml:"Error"`
Err errorBody `xml:"Error"`
}

type errorWrapper struct {
// errorBody represents the inner error body is wrapped by <ErrorResponse> tag
// eg. if error response is <ErrorResponse><Error>...</Error><ErrorResponse>
// here errorBody represents <Error>...</Error>
type errorBody struct {
Code string `xml:"Code"`
}

0 comments on commit eadd63b

Please sign in to comment.