2017-03-11 07:15:15 -05:00
|
|
|
package ofxgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-10-02 09:21:56 -04:00
|
|
|
"github.com/aclindsa/xml"
|
2017-04-17 20:11:53 -04:00
|
|
|
"golang.org/x/text/currency"
|
2017-03-13 21:09:15 -04:00
|
|
|
"math/big"
|
2017-03-11 07:15:15 -05:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// Int provides helper methods to unmarshal int64 values from SGML/XML
|
2017-03-11 07:18:02 -05:00
|
|
|
type Int int64
|
2017-03-11 07:15:15 -05:00
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// UnmarshalXML handles unmarshalling an Int from an SGML/XML string. Leading
|
|
|
|
// and trailing whitespace is ignored.
|
2017-03-19 20:46:01 -04:00
|
|
|
func (i *Int) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|
|
|
var value string
|
|
|
|
|
|
|
|
err := d.DecodeElement(&value, &start)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
value = strings.TrimSpace(value)
|
|
|
|
|
|
|
|
i2, err := strconv.ParseInt(value, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*i = Int(i2)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// Equal returns true if the two Ints are equal in value
|
2017-03-30 07:04:54 -04:00
|
|
|
func (i Int) Equal(o Int) bool {
|
|
|
|
return i == o
|
|
|
|
}
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// Amount represents non-integer values (or at least values for fields that may
|
|
|
|
// not necessarily be integers)
|
2017-04-03 21:15:08 -04:00
|
|
|
type Amount struct {
|
|
|
|
big.Rat
|
|
|
|
}
|
2017-03-13 21:09:15 -04:00
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// UnmarshalXML handles unmarshalling an Amount from an SGML/XML string.
|
|
|
|
// Leading and trailing whitespace is ignored.
|
2017-03-13 21:09:15 -04:00
|
|
|
func (a *Amount) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
2017-03-11 07:15:15 -05:00
|
|
|
var value string
|
2017-03-13 21:09:15 -04:00
|
|
|
|
2017-03-11 07:15:15 -05:00
|
|
|
err := d.DecodeElement(&value, &start)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-13 21:09:15 -04:00
|
|
|
|
2017-03-19 20:46:01 -04:00
|
|
|
value = strings.TrimSpace(value)
|
|
|
|
|
2017-03-13 21:09:15 -04:00
|
|
|
// The OFX spec allows the start of the fractional amount to be delineated
|
|
|
|
// by a comma, so fix that up before attempting to parse it into big.Rat
|
|
|
|
value = strings.Replace(value, ",", ".", 1)
|
|
|
|
|
2017-04-03 21:15:08 -04:00
|
|
|
if _, ok := a.SetString(value); !ok {
|
|
|
|
return errors.New("Failed to parse OFX amount")
|
2017-03-11 07:15:15 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// String prints a string representation of an Amount
|
2017-03-13 21:09:15 -04:00
|
|
|
func (a Amount) String() string {
|
2017-04-03 21:15:08 -04:00
|
|
|
return strings.TrimRight(strings.TrimRight(a.FloatString(100), "0"), ".")
|
2017-03-13 21:09:15 -04:00
|
|
|
}
|
2017-03-11 21:13:06 -05:00
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// MarshalXML marshals an Amount to SGML/XML
|
2024-04-06 04:04:32 -04:00
|
|
|
func (a Amount) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
2017-03-13 21:09:15 -04:00
|
|
|
return e.EncodeElement(a.String(), start)
|
|
|
|
}
|
2017-03-11 21:13:06 -05:00
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// Equal returns true if two Amounts are equal in value
|
2017-03-30 07:04:54 -04:00
|
|
|
func (a Amount) Equal(o Amount) bool {
|
2017-04-03 21:15:08 -04:00
|
|
|
return (&a).Cmp(&o.Rat) == 0
|
2017-03-30 07:04:54 -04:00
|
|
|
}
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// Date represents OFX date/time values
|
2017-04-04 05:45:19 -04:00
|
|
|
type Date struct {
|
|
|
|
time.Time
|
|
|
|
}
|
2017-03-11 12:59:47 -05:00
|
|
|
|
2017-03-11 07:15:15 -05:00
|
|
|
var ofxDateFormats = []string{
|
|
|
|
"20060102150405.000",
|
|
|
|
"20060102150405",
|
|
|
|
"200601021504",
|
|
|
|
"2006010215",
|
|
|
|
"20060102",
|
|
|
|
}
|
2017-03-11 12:59:47 -05:00
|
|
|
var ofxDateZoneRegex = regexp.MustCompile(`^([+-]?[0-9]+)(\.([0-9]{2}))?(:([A-Z]+))?$`)
|
2017-03-11 07:15:15 -05:00
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// UnmarshalXML handles unmarshalling a Date from an SGML/XML string. It
|
|
|
|
// attempts to unmarshal the valid date formats in order of decreasing length
|
|
|
|
// and defaults to GMT if a time zone is not provided, as per the OFX spec.
|
|
|
|
// Leading and trailing whitespace is ignored.
|
2017-03-11 07:18:02 -05:00
|
|
|
func (od *Date) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
2017-03-11 12:59:47 -05:00
|
|
|
var value, zone, zoneFormat string
|
2017-03-11 07:15:15 -05:00
|
|
|
err := d.DecodeElement(&value, &start)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-19 20:46:01 -04:00
|
|
|
value = strings.SplitN(value, "]", 2)[0]
|
|
|
|
value = strings.TrimSpace(value)
|
|
|
|
|
2017-03-11 12:59:47 -05:00
|
|
|
// Split the time zone off, if any
|
|
|
|
split := strings.SplitN(value, "[", 2)
|
|
|
|
if len(split) == 2 {
|
|
|
|
value = split[0]
|
|
|
|
zoneFormat = " -0700"
|
|
|
|
zone = strings.TrimRight(split[1], "]")
|
|
|
|
|
|
|
|
matches := ofxDateZoneRegex.FindStringSubmatch(zone)
|
2017-03-11 07:15:15 -05:00
|
|
|
if matches == nil {
|
2017-03-11 12:59:47 -05:00
|
|
|
return errors.New("Invalid OFX Date timezone format: " + zone)
|
2017-03-11 07:15:15 -05:00
|
|
|
}
|
|
|
|
var err error
|
|
|
|
var zonehours, zoneminutes int
|
|
|
|
zonehours, err = strconv.Atoi(matches[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(matches[3]) > 0 {
|
2017-03-13 21:09:15 -04:00
|
|
|
zoneminutes, err = strconv.Atoi(matches[3])
|
2017-03-11 07:15:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
zoneminutes = zoneminutes * 60 / 100
|
|
|
|
}
|
2017-03-11 12:59:47 -05:00
|
|
|
zone = fmt.Sprintf(" %+03d%02d", zonehours, zoneminutes)
|
2017-03-13 21:09:15 -04:00
|
|
|
|
|
|
|
// Get the time zone name if it's there, default to GMT if the offset
|
|
|
|
// is 0 and a name isn't supplied
|
|
|
|
if len(matches[5]) > 0 {
|
|
|
|
zone = zone + " " + matches[5]
|
|
|
|
zoneFormat = zoneFormat + " MST"
|
|
|
|
} else if zonehours == 0 && zoneminutes == 0 {
|
|
|
|
zone = zone + " GMT"
|
|
|
|
zoneFormat = zoneFormat + " MST"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Default to GMT if no time zone was specified
|
|
|
|
zone = " +0000 GMT"
|
|
|
|
zoneFormat = " -0700 MST"
|
2017-03-11 07:15:15 -05:00
|
|
|
}
|
|
|
|
|
2017-03-13 21:09:15 -04:00
|
|
|
// Try all the date formats, from longest to shortest
|
2017-03-11 07:15:15 -05:00
|
|
|
for _, format := range ofxDateFormats {
|
2017-03-11 12:59:47 -05:00
|
|
|
t, err := time.Parse(format+zoneFormat, value+zone)
|
2017-03-11 07:15:15 -05:00
|
|
|
if err == nil {
|
2017-04-04 05:45:19 -04:00
|
|
|
od.Time = t
|
2017-03-11 07:15:15 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.New("OFX: Couldn't parse date:" + value)
|
|
|
|
}
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// String returns a string representation of the Date abiding by the OFX spec
|
2017-03-11 12:59:47 -05:00
|
|
|
func (od Date) String() string {
|
2017-04-04 05:45:19 -04:00
|
|
|
format := od.Format(ofxDateFormats[0])
|
|
|
|
zonename, zoneoffset := od.Zone()
|
2017-03-13 21:09:15 -04:00
|
|
|
if zoneoffset < 0 {
|
|
|
|
format += "[" + fmt.Sprintf("%+d", zoneoffset/3600)
|
|
|
|
} else {
|
|
|
|
format += "[" + fmt.Sprintf("%d", zoneoffset/3600)
|
|
|
|
}
|
|
|
|
fractionaloffset := (zoneoffset % 3600) / 36
|
2017-03-11 07:15:15 -05:00
|
|
|
if fractionaloffset > 0 {
|
|
|
|
format += "." + fmt.Sprintf("%02d", fractionaloffset)
|
|
|
|
} else if fractionaloffset < 0 {
|
|
|
|
format += "." + fmt.Sprintf("%02d", -fractionaloffset)
|
|
|
|
}
|
2017-03-13 21:09:15 -04:00
|
|
|
|
|
|
|
if len(zonename) > 0 {
|
|
|
|
return format + ":" + zonename + "]"
|
|
|
|
}
|
2017-04-13 10:18:07 -04:00
|
|
|
return format + "]"
|
2017-03-11 07:15:15 -05:00
|
|
|
}
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// MarshalXML marshals a Date to XML
|
2024-04-06 04:04:32 -04:00
|
|
|
func (od Date) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
2017-03-11 07:15:15 -05:00
|
|
|
return e.EncodeElement(od.String(), start)
|
|
|
|
}
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// Equal returns true if the two Dates represent the same time (time zones are
|
|
|
|
// accounted for when comparing, but are not required to match)
|
2017-03-30 07:04:54 -04:00
|
|
|
func (od Date) Equal(o Date) bool {
|
2017-04-04 05:45:19 -04:00
|
|
|
return od.Time.Equal(o.Time)
|
|
|
|
}
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// NewDate returns a new Date object with the provided date, time, and timezone
|
2017-04-04 05:45:19 -04:00
|
|
|
func NewDate(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) *Date {
|
|
|
|
return &Date{Time: time.Date(year, month, day, hour, min, sec, nsec, loc)}
|
|
|
|
}
|
|
|
|
|
|
|
|
var gmt = time.FixedZone("GMT", 0)
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// NewDateGMT returns a new Date object with the provided date and time in the
|
|
|
|
// GMT timezone
|
2017-04-04 05:45:19 -04:00
|
|
|
func NewDateGMT(year int, month time.Month, day, hour, min, sec, nsec int) *Date {
|
|
|
|
return &Date{Time: time.Date(year, month, day, hour, min, sec, nsec, gmt)}
|
2017-03-30 07:04:54 -04:00
|
|
|
}
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// String provides helper methods to unmarshal OFX string values from SGML/XML
|
2017-03-11 07:18:02 -05:00
|
|
|
type String string
|
2017-03-11 07:15:15 -05:00
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// UnmarshalXML handles unmarshalling a String from an SGML/XML string. Leading
|
|
|
|
// and trailing whitespace is ignored.
|
2017-03-11 07:18:02 -05:00
|
|
|
func (os *String) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
2017-03-11 07:15:15 -05:00
|
|
|
var value string
|
|
|
|
err := d.DecodeElement(&value, &start)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-11 07:18:02 -05:00
|
|
|
*os = String(strings.TrimSpace(value))
|
2017-03-11 07:15:15 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// String returns the string
|
2017-03-13 21:09:15 -04:00
|
|
|
func (os *String) String() string {
|
|
|
|
return string(*os)
|
|
|
|
}
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// Equal returns true if the two Strings are equal in value
|
2017-03-30 07:04:54 -04:00
|
|
|
func (os String) Equal(o String) bool {
|
|
|
|
return os == o
|
|
|
|
}
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// Boolean provides helper methods to unmarshal bool values from OFX SGML/XML
|
2017-03-11 07:18:02 -05:00
|
|
|
type Boolean bool
|
2017-03-11 07:15:15 -05:00
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// UnmarshalXML handles unmarshalling a Boolean from an SGML/XML string.
|
|
|
|
// Leading and trailing whitespace is ignored.
|
2017-03-11 07:18:02 -05:00
|
|
|
func (ob *Boolean) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
2017-03-11 07:15:15 -05:00
|
|
|
var value string
|
|
|
|
err := d.DecodeElement(&value, &start)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tmpob := strings.TrimSpace(value)
|
|
|
|
switch tmpob {
|
|
|
|
case "Y":
|
2017-03-11 07:18:02 -05:00
|
|
|
*ob = Boolean(true)
|
2017-03-11 07:15:15 -05:00
|
|
|
case "N":
|
2017-03-11 07:18:02 -05:00
|
|
|
*ob = Boolean(false)
|
2017-03-11 07:15:15 -05:00
|
|
|
default:
|
|
|
|
return errors.New("Invalid OFX Boolean")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// MarshalXML marshals a Boolean to XML
|
2024-04-06 04:04:32 -04:00
|
|
|
func (ob Boolean) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
|
|
|
if ob {
|
2017-03-11 07:15:15 -05:00
|
|
|
return e.EncodeElement("Y", start)
|
|
|
|
}
|
|
|
|
return e.EncodeElement("N", start)
|
|
|
|
}
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// String returns a string representation of a Boolean value
|
2017-03-13 21:09:15 -04:00
|
|
|
func (ob *Boolean) String() string {
|
|
|
|
return fmt.Sprintf("%v", *ob)
|
|
|
|
}
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// Equal returns true if the two Booleans are the same
|
2017-03-30 07:04:54 -04:00
|
|
|
func (ob Boolean) Equal(o Boolean) bool {
|
|
|
|
return ob == o
|
|
|
|
}
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// UID represents an UID according to the OFX spec
|
2017-03-11 07:18:02 -05:00
|
|
|
type UID string
|
2017-03-11 07:15:15 -05:00
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// UnmarshalXML handles unmarshalling an UID from an SGML/XML string. Leading
|
|
|
|
// and trailing whitespace is ignored.
|
2017-04-03 19:44:25 -04:00
|
|
|
func (ou *UID) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|
|
|
var value string
|
|
|
|
err := d.DecodeElement(&value, &start)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*ou = UID(strings.TrimSpace(value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// RecommendedFormat returns true iff this UID meets the OFX specification's
|
|
|
|
// recommendation that UIDs follow the standard UUID 36-character format
|
2017-03-29 05:41:28 -04:00
|
|
|
func (ou UID) RecommendedFormat() (bool, error) {
|
2017-03-13 21:09:15 -04:00
|
|
|
if len(ou) != 36 {
|
2017-03-11 07:15:15 -05:00
|
|
|
return false, errors.New("UID not 36 characters long")
|
|
|
|
}
|
2017-03-13 21:09:15 -04:00
|
|
|
if ou[8] != '-' || ou[13] != '-' || ou[18] != '-' || ou[23] != '-' {
|
|
|
|
return false, errors.New("UID missing hyphens at the appropriate places")
|
|
|
|
}
|
2017-03-11 07:15:15 -05:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2017-04-18 19:46:23 -04:00
|
|
|
// Valid returns true, nil if the UID is valid. This is less strict than
|
|
|
|
// RecommendedFormat, and will always return true, nil if it does.
|
|
|
|
func (ou UID) Valid() (bool, error) {
|
|
|
|
if len(ou) == 0 || len(ou) > 36 {
|
|
|
|
return false, errors.New("UID invalid length")
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// Equal returns true if the two UIDs are the same
|
2017-03-30 07:04:54 -04:00
|
|
|
func (ou UID) Equal(o UID) bool {
|
|
|
|
return ou == o
|
|
|
|
}
|
|
|
|
|
2017-04-13 10:18:07 -04:00
|
|
|
// RandomUID creates a new randomly-generated UID
|
2017-03-11 07:18:02 -05:00
|
|
|
func RandomUID() (*UID, error) {
|
2017-03-11 07:15:15 -05:00
|
|
|
uidbytes := make([]byte, 16)
|
|
|
|
n, err := rand.Read(uidbytes[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if n != 16 {
|
|
|
|
return nil, errors.New("RandomUID failed to read 16 random bytes")
|
|
|
|
}
|
2017-03-11 07:18:02 -05:00
|
|
|
uid := UID(fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", uidbytes[:4], uidbytes[4:6], uidbytes[6:8], uidbytes[8:10], uidbytes[10:]))
|
2017-03-11 07:15:15 -05:00
|
|
|
return &uid, nil
|
|
|
|
}
|
2017-04-17 20:11:53 -04:00
|
|
|
|
|
|
|
// CurrSymbol represents an ISO-4217 currency
|
|
|
|
type CurrSymbol struct {
|
|
|
|
currency.Unit
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalXML handles unmarshalling a CurrSymbol from an SGML/XML string.
|
|
|
|
// Leading and trailing whitespace is ignored.
|
|
|
|
func (c *CurrSymbol) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|
|
|
var value string
|
|
|
|
|
|
|
|
err := d.DecodeElement(&value, &start)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
value = strings.TrimSpace(value)
|
|
|
|
|
|
|
|
unit, err := currency.ParseISO(value)
|
|
|
|
if err != nil {
|
2017-04-18 19:50:04 -04:00
|
|
|
return errors.New("Error parsing CurrSymbol:" + err.Error())
|
2017-04-17 20:11:53 -04:00
|
|
|
}
|
|
|
|
c.Unit = unit
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalXML marshals a CurrSymbol to SGML/XML
|
2024-04-06 04:04:32 -04:00
|
|
|
func (c CurrSymbol) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
2017-04-17 20:11:53 -04:00
|
|
|
return e.EncodeElement(c.String(), start)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal returns true if the two Currencies are the same
|
|
|
|
func (c CurrSymbol) Equal(o CurrSymbol) bool {
|
|
|
|
return c.String() == o.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid returns true, nil if the CurrSymbol is valid.
|
|
|
|
func (c CurrSymbol) Valid() (bool, error) {
|
|
|
|
if c.String() == "XXX" {
|
|
|
|
return false, fmt.Errorf("Invalid CurrSymbol: %s", c.Unit)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2017-04-18 19:50:04 -04:00
|
|
|
// NewCurrSymbol returns a new CurrSymbol given a three-letter ISO-4217
|
|
|
|
// currency symbol as a string
|
2017-04-17 20:11:53 -04:00
|
|
|
func NewCurrSymbol(s string) (*CurrSymbol, error) {
|
|
|
|
unit, err := currency.ParseISO(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("Error parsing string to create new CurrSymbol:" + err.Error())
|
|
|
|
}
|
|
|
|
return &CurrSymbol{unit}, nil
|
|
|
|
}
|