mirror of
https://github.com/aclindsa/ofxgo.git
synced 2025-07-01 11:48:38 -04:00
Add the ability to marshal a Response to SGML/XML and test it
This allows for ofxgo to be used to create well-formatted OFX from poor OFX, or even be used to generate OFX from other formats for easier importing into financial management software. Test this functionality by adding "round trip" testing to all existing tests - ensure that responses' content is the same after a round trip of marshalling and unmarshalling them.
This commit is contained in:
40
seclist.go
40
seclist.go
@ -221,6 +221,7 @@ func (i StockInfo) SecurityType() string {
|
||||
// SecurityList is a container for Security objects containaing information
|
||||
// about securities
|
||||
type SecurityList struct {
|
||||
XMLName xml.Name `xml:"SECLIST"`
|
||||
Securities []Security
|
||||
}
|
||||
|
||||
@ -290,3 +291,42 @@ func (r *SecurityList) UnmarshalXML(d *xml.Decoder, start xml.StartElement) erro
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalXML handles marshalling a SecurityList to an SGML/XML string
|
||||
func (r *SecurityList) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
||||
secListElement := xml.StartElement{Name: xml.Name{Local: "SECLIST"}}
|
||||
if err := e.EncodeToken(secListElement); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, s := range r.Securities {
|
||||
start := xml.StartElement{Name: xml.Name{Local: s.SecurityType()}}
|
||||
switch sec := s.(type) {
|
||||
case DebtInfo:
|
||||
if err := e.EncodeElement(&sec, start); err != nil {
|
||||
return err
|
||||
}
|
||||
case MFInfo:
|
||||
if err := e.EncodeElement(&sec, start); err != nil {
|
||||
return err
|
||||
}
|
||||
case OptInfo:
|
||||
if err := e.EncodeElement(&sec, start); err != nil {
|
||||
return err
|
||||
}
|
||||
case OtherInfo:
|
||||
if err := e.EncodeElement(&sec, start); err != nil {
|
||||
return err
|
||||
}
|
||||
case StockInfo:
|
||||
if err := e.EncodeElement(&sec, start); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return errors.New("Invalid SECLIST child type: " + sec.SecurityType())
|
||||
}
|
||||
}
|
||||
if err := e.EncodeToken(secListElement.End()); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user