1
0
mirror of https://github.com/aclindsa/ofxgo.git synced 2024-11-22 11:30:05 -05:00
ofxgo/types.go

278 lines
6.1 KiB
Go
Raw Normal View History

2017-03-11 07:15:15 -05:00
package ofxgo
import (
"crypto/rand"
"errors"
"fmt"
"github.com/aclindsa/go/src/encoding/xml"
"math/big"
2017-03-11 07:15:15 -05:00
"regexp"
"strconv"
"strings"
"time"
)
2017-03-11 07:18:02 -05:00
type Int int64
2017-03-11 07:15:15 -05: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
}
func (i Int) Equal(o Int) bool {
return i == o
}
type Amount big.Rat
func (a *Amount) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
2017-03-11 07:15:15 -05:00
var value string
var b big.Rat
2017-03-11 07:15:15 -05:00
err := d.DecodeElement(&value, &start)
if err != nil {
return err
}
value = strings.TrimSpace(value)
// 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)
if _, ok := b.SetString(value); !ok {
return errors.New("Failed to parse OFX amount into big.Rat")
2017-03-11 07:15:15 -05:00
}
*a = Amount(b)
2017-03-11 07:15:15 -05:00
return nil
}
func (a Amount) String() string {
var b big.Rat = big.Rat(a)
return strings.TrimRight(strings.TrimRight(b.FloatString(100), "0"), ".")
}
2017-03-11 21:13:06 -05:00
func (a *Amount) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(a.String(), start)
}
2017-03-11 21:13:06 -05:00
func (a Amount) Equal(o Amount) bool {
ratA := (*big.Rat)(&a)
return ratA.Cmp((*big.Rat)(&o)) == 0
}
type Date time.Time
2017-03-11 07:15:15 -05:00
var ofxDateFormats = []string{
"20060102150405.000",
"20060102150405",
"200601021504",
"2006010215",
"20060102",
}
var ofxDateZoneRegex = regexp.MustCompile(`^([+-]?[0-9]+)(\.([0-9]{2}))?(:([A-Z]+))?$`)
2017-03-11 07:15:15 -05:00
2017-03-11 07:18:02 -05:00
func (od *Date) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var value, zone, zoneFormat string
2017-03-11 07:15:15 -05:00
err := d.DecodeElement(&value, &start)
if err != nil {
return err
}
value = strings.SplitN(value, "]", 2)[0]
value = strings.TrimSpace(value)
// 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 {
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 {
zoneminutes, err = strconv.Atoi(matches[3])
2017-03-11 07:15:15 -05:00
if err != nil {
return err
}
zoneminutes = zoneminutes * 60 / 100
}
zone = fmt.Sprintf(" %+03d%02d", zonehours, zoneminutes)
// 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
}
// Try all the date formats, from longest to shortest
2017-03-11 07:15:15 -05:00
for _, format := range ofxDateFormats {
t, err := time.Parse(format+zoneFormat, value+zone)
2017-03-11 07:15:15 -05:00
if err == nil {
2017-03-11 07:18:02 -05:00
tmpod := Date(t)
2017-03-11 07:15:15 -05:00
*od = tmpod
return nil
}
}
return errors.New("OFX: Couldn't parse date:" + value)
}
func (od Date) String() string {
t := time.Time(od)
2017-03-11 07:15:15 -05:00
format := t.Format(ofxDateFormats[0])
zonename, zoneoffset := t.Zone()
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)
}
if len(zonename) > 0 {
return format + ":" + zonename + "]"
} else {
return format + "]"
}
2017-03-11 07:15:15 -05:00
}
2017-03-11 07:18:02 -05: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)
}
func (od Date) Equal(o Date) bool {
return time.Time(od).Equal(time.Time(o))
}
2017-03-11 07:18:02 -05:00
type String string
2017-03-11 07:15:15 -05:00
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
}
func (os *String) String() string {
return string(*os)
}
func (os String) Equal(o String) bool {
return os == o
}
2017-03-11 07:18:02 -05:00
type Boolean bool
2017-03-11 07:15:15 -05:00
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-03-11 07:18:02 -05:00
func (ob *Boolean) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
2017-03-11 07:15:15 -05:00
if *ob {
return e.EncodeElement("Y", start)
}
return e.EncodeElement("N", start)
}
func (ob *Boolean) String() string {
return fmt.Sprintf("%v", *ob)
}
func (ob Boolean) Equal(o Boolean) bool {
return ob == o
}
2017-03-11 07:18:02 -05:00
type UID string
2017-03-11 07:15:15 -05: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
}
// The OFX specification recommends that UIDs follow the standard UUID
// 36-character format
func (ou UID) RecommendedFormat() (bool, error) {
if len(ou) != 36 {
2017-03-11 07:15:15 -05:00
return false, errors.New("UID not 36 characters long")
}
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
}
func (ou UID) Equal(o UID) bool {
return ou == o
}
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
}