1
0
mirror of https://github.com/aclindsa/ofxgo.git synced 2025-07-03 20:38:39 -04:00

Generalize response parsing code

This removes the many decodeXXXMessageSet() functions and replaces them
with a large map and a single generic decodeMessageSet() function. Also
change Responses to satisfy the Message interface as pointer types
(instead of the raw types), add the full set of top-level message sets
(though most of them still lack any message-parsing ability), adjust the
message set names to more closely mirror their OFX names, and fixup
tests and the command-line client to match the above changes.
This commit is contained in:
2017-03-31 11:54:43 -04:00
parent d822179446
commit f185d78d29
18 changed files with 171 additions and 301 deletions

View File

@ -50,16 +50,16 @@ type SecListResponse struct {
// SECLISTRS is always empty, so we don't parse it here. The actual securities list will be in a top-level element parallel to SECLISTTRNRS
}
func (r SecListResponse) Name() string {
func (r *SecListResponse) Name() string {
return "SECLISTTRNRS"
}
func (r SecListResponse) Valid() (bool, error) {
func (r *SecListResponse) Valid() (bool, error) {
// TODO implement
return true, nil
}
func (r SecListResponse) Type() messageType {
func (r *SecListResponse) Type() messageType {
return SecListRs
}
@ -175,16 +175,16 @@ type SecurityList struct {
Securities []Security
}
func (r SecurityList) Name() string {
func (r *SecurityList) Name() string {
return "SECLIST"
}
func (r SecurityList) Valid() (bool, error) {
func (r *SecurityList) Valid() (bool, error) {
// TODO implement
return true, nil
}
func (r SecurityList) Type() messageType {
func (r *SecurityList) Type() messageType {
return SecListRs
}
@ -236,35 +236,3 @@ func (r *SecurityList) UnmarshalXML(d *xml.Decoder, start xml.StartElement) erro
}
}
}
func decodeSecuritiesMessageSet(d *xml.Decoder, start xml.StartElement) ([]Message, error) {
var msgs []Message
for {
tok, err := nextNonWhitespaceToken(d)
if err != nil {
return nil, err
} else if end, ok := tok.(xml.EndElement); ok && end.Name.Local == start.Name.Local {
// If we found the end of our starting element, we're done parsing
return msgs, nil
} else if startElement, ok := tok.(xml.StartElement); ok {
switch startElement.Name.Local {
case "SECLISTTRNRS":
var info SecListResponse
if err := d.DecodeElement(&info, &startElement); err != nil {
return nil, err
}
msgs = append(msgs, Message(info))
case "SECLIST":
var info SecurityList
if err := d.DecodeElement(&info, &startElement); err != nil {
return nil, err
}
msgs = append(msgs, Message(info))
default:
return nil, errors.New("Unsupported securities response tag: " + startElement.Name.Local)
}
} else {
return nil, errors.New("Didn't find an opening element")
}
}
}