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

Remove 'Ofx' prefix on types

This commit is contained in:
Aaron Lindsay 2017-03-11 07:18:02 -05:00
parent 99cd8f7273
commit 689337d81d
5 changed files with 88 additions and 88 deletions

View File

@ -4,18 +4,18 @@ import (
"github.com/golang/go/src/encoding/xml" "github.com/golang/go/src/encoding/xml"
) )
type OfxAcctInfoRequest struct { type AcctInfoRequest struct {
XMLName xml.Name `xml:"ACCTINFOTRNRQ"` XMLName xml.Name `xml:"ACCTINFOTRNRQ"`
TrnUID OfxUID `xml:"TRNUID"` TrnUID UID `xml:"TRNUID"`
CltCookie OfxInt `xml:"CLTCOOKIE"` CltCookie Int `xml:"CLTCOOKIE"`
DtAcctup OfxDate `xml:"ACCTINFORQ>DTACCTUP"` DtAcctup Date `xml:"ACCTINFORQ>DTACCTUP"`
} }
func (r *OfxAcctInfoRequest) Name() string { func (r *AcctInfoRequest) Name() string {
return "ACCTINFOTRNRQ" return "ACCTINFOTRNRQ"
} }
func (r *OfxAcctInfoRequest) Valid() (bool, error) { func (r *AcctInfoRequest) Valid() (bool, error) {
if ok, err := r.TrnUID.Valid(); !ok { if ok, err := r.TrnUID.Valid(); !ok {
return false, err return false, err
} }

38
ofx.go
View File

@ -12,16 +12,16 @@ import (
"time" "time"
) )
type OfxMessage interface { type Message interface {
Name() string Name() string
Valid() (bool, error) Valid() (bool, error)
} }
type OfxRequest struct { type Request struct {
URL string URL string
Version string // String for OFX header, defaults to 203 Version string // String for OFX header, defaults to 203
Signon OfxSignonRequest //<SIGNONMSGSETV1> Signon SignonRequest //<SIGNONMSGSETV1>
Signup []OfxMessage //<SIGNUPMSGSETV1> Signup []Message //<SIGNUPMSGSETV1>
//<BANKMSGSETV1> //<BANKMSGSETV1>
//<CREDITCARDMSGSETV1> //<CREDITCARDMSGSETV1>
//<LOANMSGSETV1> //<LOANMSGSETV1>
@ -33,11 +33,11 @@ type OfxRequest struct {
//<SECLISTMSGSETV1> //<SECLISTMSGSETV1>
//<PRESDIRMSGSETV1> //<PRESDIRMSGSETV1>
//<PRESDLVMSGSETV1> //<PRESDLVMSGSETV1>
Profile []OfxMessage //<PROFMSGSETV1> Profile []Message //<PROFMSGSETV1>
//<IMAGEMSGSETV1> //<IMAGEMSGSETV1>
} }
func (oq *OfxRequest) marshalMessageSet(e *xml.Encoder, requests []OfxMessage, setname string) error { func (oq *Request) marshalMessageSet(e *xml.Encoder, requests []Message, setname string) error {
if len(requests) > 0 { if len(requests) > 0 {
messageSetElement := xml.StartElement{Name: xml.Name{Local: setname}} messageSetElement := xml.StartElement{Name: xml.Name{Local: setname}}
if err := e.EncodeToken(messageSetElement); err != nil { if err := e.EncodeToken(messageSetElement); err != nil {
@ -60,7 +60,7 @@ func (oq *OfxRequest) marshalMessageSet(e *xml.Encoder, requests []OfxMessage, s
return nil return nil
} }
func (oq *OfxRequest) Marshal() (*bytes.Buffer, error) { func (oq *Request) Marshal() (*bytes.Buffer, error) {
var b bytes.Buffer var b bytes.Buffer
if len(oq.Version) == 0 { if len(oq.Version) == 0 {
@ -127,8 +127,8 @@ NEWFILEUID:NONE
return &b, nil return &b, nil
} }
func (oq *OfxRequest) Request() (*OfxResponse, error) { func (oq *Request) Request() (*Response, error) {
oq.Signon.Dtclient = OfxDate(time.Now()) oq.Signon.Dtclient = Date(time.Now())
b, err := oq.Marshal() b, err := oq.Marshal()
if err != nil { if err != nil {
@ -152,7 +152,7 @@ func (oq *OfxRequest) Request() (*OfxResponse, error) {
xmlVersion = false xmlVersion = false
} }
var ofxresp OfxResponse var ofxresp Response
if err := ofxresp.Unmarshal(response.Body, xmlVersion); err != nil { if err := ofxresp.Unmarshal(response.Body, xmlVersion); err != nil {
return nil, err return nil, err
} }
@ -160,10 +160,10 @@ func (oq *OfxRequest) Request() (*OfxResponse, error) {
return &ofxresp, nil return &ofxresp, nil
} }
type OfxResponse struct { type Response struct {
Version string // String for OFX header, defaults to 203 Version string // String for OFX header, defaults to 203
Signon OfxSignonResponse //<SIGNONMSGSETV1> Signon SignonResponse //<SIGNONMSGSETV1>
Signup []OfxMessage //<SIGNUPMSGSETV1> Signup []Message //<SIGNUPMSGSETV1>
//<BANKMSGSETV1> //<BANKMSGSETV1>
//<CREDITCARDMSGSETV1> //<CREDITCARDMSGSETV1>
//<LOANMSGSETV1> //<LOANMSGSETV1>
@ -175,11 +175,11 @@ type OfxResponse struct {
//<SECLISTMSGSETV1> //<SECLISTMSGSETV1>
//<PRESDIRMSGSETV1> //<PRESDIRMSGSETV1>
//<PRESDLVMSGSETV1> //<PRESDLVMSGSETV1>
Profile []OfxMessage //<PROFMSGSETV1> Profile []Message //<PROFMSGSETV1>
//<IMAGEMSGSETV1> //<IMAGEMSGSETV1>
} }
func (or *OfxResponse) readSGMLHeaders(r *bufio.Reader) error { func (or *Response) readSGMLHeaders(r *bufio.Reader) error {
var seenHeader, seenVersion bool = false, false var seenHeader, seenVersion bool = false, false
for { for {
line, err := r.ReadString('\n') line, err := r.ReadString('\n')
@ -240,7 +240,7 @@ func (or *OfxResponse) readSGMLHeaders(r *bufio.Reader) error {
return nil return nil
} }
func (or *OfxResponse) readXMLHeaders(decoder *xml.Decoder) error { func (or *Response) readXMLHeaders(decoder *xml.Decoder) error {
tok, err := decoder.Token() tok, err := decoder.Token()
if err != nil { if err != nil {
return err return err
@ -308,7 +308,7 @@ func (or *OfxResponse) readXMLHeaders(decoder *xml.Decoder) error {
return nil return nil
} }
func (or *OfxResponse) Unmarshal(reader io.Reader, xmlVersion bool) error { func (or *Response) Unmarshal(reader io.Reader, xmlVersion bool) error {
r := bufio.NewReader(reader) r := bufio.NewReader(reader)
// parse SGML headers before creating XML decoder // parse SGML headers before creating XML decoder

View File

@ -4,18 +4,18 @@ import (
"github.com/golang/go/src/encoding/xml" "github.com/golang/go/src/encoding/xml"
) )
type OfxProfileRequest struct { type ProfileRequest struct {
XMLName xml.Name `xml:"PROFTRNRQ"` XMLName xml.Name `xml:"PROFTRNRQ"`
TrnUID OfxUID `xml:"TRNUID"` TrnUID UID `xml:"TRNUID"`
ClientRouting OfxString `xml:"PROFRQ>CLIENTROUTING"` // Forced to NONE ClientRouting String `xml:"PROFRQ>CLIENTROUTING"` // Forced to NONE
DtProfup OfxDate `xml:"PROFRQ>DTPROFUP"` DtProfup Date `xml:"PROFRQ>DTPROFUP"`
} }
func (r *OfxProfileRequest) Name() string { func (r *ProfileRequest) Name() string {
return "PROFTRNRQ" return "PROFTRNRQ"
} }
func (r *OfxProfileRequest) Valid() (bool, error) { func (r *ProfileRequest) Valid() (bool, error) {
if ok, err := r.TrnUID.Valid(); !ok { if ok, err := r.TrnUID.Valid(); !ok {
return false, err return false, err
} }

View File

@ -5,25 +5,25 @@ import (
"github.com/golang/go/src/encoding/xml" "github.com/golang/go/src/encoding/xml"
) )
type OfxSignonRequest struct { type SignonRequest struct {
XMLName xml.Name `xml:"SONRQ"` XMLName xml.Name `xml:"SONRQ"`
Dtclient OfxDate `xml:"DTCLIENT"` // Overridden in OfxRequest.Request() Dtclient Date `xml:"DTCLIENT"` // Overridden in Request.Request()
UserId OfxString `xml:"USERID"` UserId String `xml:"USERID"`
UserPass OfxString `xml:"USERPASS,omitempty"` UserPass String `xml:"USERPASS,omitempty"`
UserKey OfxString `xml:"USERKEY,omitempty"` UserKey String `xml:"USERKEY,omitempty"`
Language OfxString `xml:"LANGUAGE"` // Defaults to ENG Language String `xml:"LANGUAGE"` // Defaults to ENG
Org OfxString `xml:"FI>ORG"` Org String `xml:"FI>ORG"`
Fid OfxString `xml:"FI>FID"` Fid String `xml:"FI>FID"`
AppId OfxString `xml:"APPID"` // Defaults to OFXGO AppId String `xml:"APPID"` // Defaults to OFXGO
AppVer OfxString `xml:"APPVER"` // Defaults to 0001 AppVer String `xml:"APPVER"` // Defaults to 0001
ClientUID OfxUID `xml:"CLIENTUID,omitempty"` ClientUID UID `xml:"CLIENTUID,omitempty"`
} }
func (r *OfxSignonRequest) Name() string { func (r *SignonRequest) Name() string {
return "SONRQ" return "SONRQ"
} }
func (r *OfxSignonRequest) Valid() (bool, error) { func (r *SignonRequest) Valid() (bool, error) {
if len(r.UserId) < 1 || len(r.UserId) > 32 { if len(r.UserId) < 1 || len(r.UserId) > 32 {
return false, errors.New("SONRQ>USERID invalid length") return false, errors.New("SONRQ>USERID invalid length")
} }
@ -59,14 +59,14 @@ func (r *OfxSignonRequest) Valid() (bool, error) {
return true, nil return true, nil
} }
type OfxStatus struct { type Status struct {
XMLName xml.Name `xml:"STATUS"` XMLName xml.Name `xml:"STATUS"`
Code OfxInt `xml:"CODE"` Code Int `xml:"CODE"`
Severity OfxString `xml:"SEVERITY"` Severity String `xml:"SEVERITY"`
Message OfxString `xml:"MESSAGE,omitempty"` Message String `xml:"MESSAGE,omitempty"`
} }
func (s *OfxStatus) Valid() (bool, error) { func (s *Status) Valid() (bool, error) {
switch s.Severity { switch s.Severity {
case "INFO", "WARN", "ERROR": case "INFO", "WARN", "ERROR":
return true, nil return true, nil
@ -75,26 +75,26 @@ func (s *OfxStatus) Valid() (bool, error) {
} }
} }
type OfxSignonResponse struct { type SignonResponse struct {
XMLName xml.Name `xml:"SONRS"` XMLName xml.Name `xml:"SONRS"`
Status OfxStatus `xml:"STATUS"` Status Status `xml:"STATUS"`
Dtserver OfxDate `xml:"DTSERVER"` Dtserver Date `xml:"DTSERVER"`
UserKey OfxString `xml:"USERKEY,omitempty"` UserKey String `xml:"USERKEY,omitempty"`
TsKeyExpire OfxDate `xml:"TSKEYEXPIRE,omitempty"` TsKeyExpire Date `xml:"TSKEYEXPIRE,omitempty"`
Language OfxString `xml:"LANGUAGE"` Language String `xml:"LANGUAGE"`
Dtprofup OfxDate `xml:"DTPROFUP,omitempty"` Dtprofup Date `xml:"DTPROFUP,omitempty"`
Dtacctup OfxDate `xml:"DTACCTUP,omitempty"` Dtacctup Date `xml:"DTACCTUP,omitempty"`
Org OfxString `xml:"FI>ORG"` Org String `xml:"FI>ORG"`
Fid OfxString `xml:"FI>FID"` Fid String `xml:"FI>FID"`
SessCookie OfxString `xml:"SESSCOOKIE,omitempty"` SessCookie String `xml:"SESSCOOKIE,omitempty"`
AccessKey OfxString `xml:"ACCESSKEY,omitempty"` AccessKey String `xml:"ACCESSKEY,omitempty"`
} }
func (r *OfxSignonResponse) Name() string { func (r *SignonResponse) Name() string {
return "SONRS" return "SONRS"
} }
func (r *OfxSignonResponse) Valid() (bool, error) { func (r *SignonResponse) Valid() (bool, error) {
if len(r.Language) != 3 { if len(r.Language) != 3 {
return false, errors.New("SONRS>LANGUAGE invalid length: " + string(r.Language)) return false, errors.New("SONRS>LANGUAGE invalid length: " + string(r.Language))
} }

View File

@ -11,9 +11,9 @@ import (
"time" "time"
) )
type OfxInt int64 type Int int64
func (oi *OfxInt) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { func (oi *Int) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var value string var value string
err := d.DecodeElement(&value, &start) err := d.DecodeElement(&value, &start)
if err != nil { if err != nil {
@ -23,7 +23,7 @@ func (oi *OfxInt) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
if err != nil { if err != nil {
return err return err
} }
*oi = (OfxInt)(i) *oi = (Int)(i)
return nil return nil
} }
@ -37,9 +37,9 @@ var ofxDateFormats = []string{
var ofxDateZoneFormat = "20060102150405.000 -0700" var ofxDateZoneFormat = "20060102150405.000 -0700"
var ofxDateZoneRegex = regexp.MustCompile(`^\[([+-]?[0-9]+)(\.([0-9]{2}))?(:([A-Z]+))?\]$`) var ofxDateZoneRegex = regexp.MustCompile(`^\[([+-]?[0-9]+)(\.([0-9]{2}))?(:([A-Z]+))?\]$`)
type OfxDate time.Time type Date time.Time
func (od *OfxDate) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { func (od *Date) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var value string var value string
err := d.DecodeElement(&value, &start) err := d.DecodeElement(&value, &start)
if err != nil { if err != nil {
@ -68,7 +68,7 @@ func (od *OfxDate) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
value = value[:len(ofxDateFormats[0])] + " " + fmt.Sprintf("%+d%02d", zonehours, zoneminutes) value = value[:len(ofxDateFormats[0])] + " " + fmt.Sprintf("%+d%02d", zonehours, zoneminutes)
t, err := time.Parse(ofxDateZoneFormat, value) t, err := time.Parse(ofxDateZoneFormat, value)
if err == nil { if err == nil {
tmpod := OfxDate(t) tmpod := Date(t)
*od = tmpod *od = tmpod
return nil return nil
} }
@ -77,7 +77,7 @@ func (od *OfxDate) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, format := range ofxDateFormats { for _, format := range ofxDateFormats {
t, err := time.Parse(format, value) t, err := time.Parse(format, value)
if err == nil { if err == nil {
tmpod := OfxDate(t) tmpod := Date(t)
*od = tmpod *od = tmpod
return nil return nil
} }
@ -85,7 +85,7 @@ func (od *OfxDate) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
return errors.New("OFX: Couldn't parse date:" + value) return errors.New("OFX: Couldn't parse date:" + value)
} }
func (od *OfxDate) String() string { func (od *Date) String() string {
t := time.Time(*od) t := time.Time(*od)
format := t.Format(ofxDateFormats[0]) format := t.Format(ofxDateFormats[0])
zonename, zoneoffset := t.Zone() zonename, zoneoffset := t.Zone()
@ -99,25 +99,25 @@ func (od *OfxDate) String() string {
return format + ":" + zonename + "]" return format + ":" + zonename + "]"
} }
func (od *OfxDate) MarshalXML(e *xml.Encoder, start xml.StartElement) error { func (od *Date) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(od.String(), start) return e.EncodeElement(od.String(), start)
} }
type OfxString string type String string
func (os *OfxString) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { func (os *String) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var value string var value string
err := d.DecodeElement(&value, &start) err := d.DecodeElement(&value, &start)
if err != nil { if err != nil {
return err return err
} }
*os = OfxString(strings.TrimSpace(value)) *os = String(strings.TrimSpace(value))
return nil return nil
} }
type OfxBoolean bool type Boolean bool
func (ob *OfxBoolean) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { func (ob *Boolean) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var value string var value string
err := d.DecodeElement(&value, &start) err := d.DecodeElement(&value, &start)
if err != nil { if err != nil {
@ -126,32 +126,32 @@ func (ob *OfxBoolean) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
tmpob := strings.TrimSpace(value) tmpob := strings.TrimSpace(value)
switch tmpob { switch tmpob {
case "Y": case "Y":
*ob = OfxBoolean(true) *ob = Boolean(true)
case "N": case "N":
*ob = OfxBoolean(false) *ob = Boolean(false)
default: default:
return errors.New("Invalid OFX Boolean") return errors.New("Invalid OFX Boolean")
} }
return nil return nil
} }
func (ob *OfxBoolean) MarshalXML(e *xml.Encoder, start xml.StartElement) error { func (ob *Boolean) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if *ob { if *ob {
return e.EncodeElement("Y", start) return e.EncodeElement("Y", start)
} }
return e.EncodeElement("N", start) return e.EncodeElement("N", start)
} }
type OfxUID string type UID string
func (ou *OfxUID) Valid() (bool, error) { func (ou *UID) Valid() (bool, error) {
if len(*ou) != 36 { if len(*ou) != 36 {
return false, errors.New("UID not 36 characters long") return false, errors.New("UID not 36 characters long")
} }
return true, nil return true, nil
} }
func RandomUID() (*OfxUID, error) { func RandomUID() (*UID, error) {
uidbytes := make([]byte, 16) uidbytes := make([]byte, 16)
n, err := rand.Read(uidbytes[:]) n, err := rand.Read(uidbytes[:])
if err != nil { if err != nil {
@ -160,6 +160,6 @@ func RandomUID() (*OfxUID, error) {
if n != 16 { if n != 16 {
return nil, errors.New("RandomUID failed to read 16 random bytes") return nil, errors.New("RandomUID failed to read 16 random bytes")
} }
uid := OfxUID(fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", uidbytes[:4], uidbytes[4:6], uidbytes[6:8], uidbytes[8:10], uidbytes[10:])) uid := UID(fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", uidbytes[:4], uidbytes[4:6], uidbytes[6:8], uidbytes[8:10], uidbytes[10:]))
return &uid, nil return &uid, nil
} }