Use CurrSymbol instead of String to represent currencies

This commit is contained in:
Aaron Lindsay 2017-04-17 20:20:22 -04:00
parent faac776ca4
commit 2046fa32e5
10 changed files with 50 additions and 30 deletions

10
bank.go
View File

@ -95,8 +95,8 @@ type Transaction struct {
CCAcctTo *CCAcct `xml:"CCACCTTO,omitempty"` // If the transfer was to a credit card account we have the account information for
Memo String `xml:"MEMO,omitempty"` // Extra information (not in NAME)
ImageData []ImageData `xml:"IMAGEDATA,omitempty"`
Currency String `xml:"CURRENCY,omitempty"` // If different from CURDEF in STMTTRS
OrigCurrency String `xml:"ORIGCURRENCY,omitempty"` // If different from CURDEF in STMTTRS
Currency CurrSymbol `xml:"CURRENCY,omitempty"` // If different from CURDEF in STMTTRS
OrigCurrency CurrSymbol `xml:"ORIGCURRENCY,omitempty"` // If different from CURDEF in STMTTRS
Inv401kSource inv401kSource `xml:"INV401KSOURCE,omitempty"` // One of PRETAX, AFTERTAX, MATCH, PROFITSHARING, ROLLOVER, OTHERVEST, OTHERNONVEST (Default if not present is OTHERNONVEST. The following cash source types are subject to vesting: MATCH, PROFITSHARING, and OTHERVEST.)
}
@ -123,8 +123,8 @@ type PendingTransaction struct {
ExtdName String `xml:"EXTDNAME,omitempty"` // Extended name of payee or transaction description
Memo String `xml:"MEMO,omitempty"` // Extra information (not in NAME)
ImageData []ImageData `xml:"IMAGEDATA,omitempty"`
Currency String `xml:"CURRENCY,omitempty"` // If different from CURDEF in STMTTRS
OrigCurrency String `xml:"ORIGCURRENCY,omitempty"` // If different from CURDEF in STMTTRS
Currency CurrSymbol `xml:"CURRENCY,omitempty"` // If different from CURDEF in STMTTRS
OrigCurrency CurrSymbol `xml:"ORIGCURRENCY,omitempty"` // If different from CURDEF in STMTTRS
}
// PendingTransactionList represents a list of pending transactions, along with
@ -161,7 +161,7 @@ type StatementResponse struct {
Status Status `xml:"STATUS"`
CltCookie String `xml:"CLTCOOKIE,omitempty"`
// TODO `xml:"OFXEXTENSION,omitempty"`
CurDef String `xml:"STMTRS>CURDEF"`
CurDef CurrSymbol `xml:"STMTRS>CURDEF"`
BankAcctFrom BankAcct `xml:"STMTRS>BANKACCTFROM"`
BankTranList *TransactionList `xml:"STMTRS>BANKTRANLIST,omitempty"`
BankTranListP *PendingTransactionList `xml:"STMTRS>BANKTRANLISTP,omitempty"`

View File

@ -252,13 +252,18 @@ func TestUnmarshalBankStatementResponse(t *testing.T) {
balamt.SetFrac64(20029, 100)
availbalamt.SetFrac64(20029, 100)
usd, err := ofxgo.NewCurrSymbol("USD")
if err != nil {
t.Fatalf("Unexpected error creating CurrSymbol for USD\n")
}
statementResponse := ofxgo.StatementResponse{
TrnUID: "1001",
Status: ofxgo.Status{
Code: 0,
Severity: "INFO",
},
CurDef: "USD",
CurDef: *usd,
BankAcctFrom: ofxgo.BankAcct{
BankID: "318398732",
AcctID: "78346129",

View File

@ -75,12 +75,12 @@ func bankTransactions() {
}
}
func printTransaction(defCurrency ofxgo.String, tran *ofxgo.Transaction) {
func printTransaction(defCurrency ofxgo.CurrSymbol, tran *ofxgo.Transaction) {
currency := defCurrency
if len(tran.Currency) > 0 {
currency = tran.Currency
} else if len(tran.OrigCurrency) > 0 {
if ok, _ := tran.Currency.Valid(); ok {
currency = tran.Currency
} else if ok, _ := tran.OrigCurrency.Valid(); ok {
currency = tran.OrigCurrency
}
var name string
@ -94,5 +94,5 @@ func printTransaction(defCurrency ofxgo.String, tran *ofxgo.Transaction) {
name = name + " - " + string(tran.Memo)
}
fmt.Printf("%s %-15s %-11s %s\n", tran.DtPosted, tran.TrnAmt.String()+" "+string(currency), tran.TrnType, name)
fmt.Printf("%s %-15s %-11s %s\n", tran.DtPosted, tran.TrnAmt.String()+" "+currency.String(), tran.TrnType, name)
}

View File

@ -60,10 +60,10 @@ func ccTransactions() {
fmt.Println("Transactions:")
for _, tran := range stmt.BankTranList.Transactions {
currency := stmt.CurDef
if len(tran.Currency) > 0 {
currency = tran.Currency
} else if len(tran.OrigCurrency) > 0 {
if ok, _ := tran.Currency.Valid(); ok {
currency = tran.Currency
} else if ok, _ := tran.OrigCurrency.Valid(); ok {
currency = tran.OrigCurrency
}
var name string
@ -77,7 +77,7 @@ func ccTransactions() {
name = name + " - " + string(tran.Memo)
}
fmt.Printf("%s %-15s %-11s %s\n", tran.DtPosted, tran.TrnAmt.String()+" "+string(currency), tran.TrnType, name)
fmt.Printf("%s %-15s %-11s %s\n", tran.DtPosted, tran.TrnAmt.String()+" "+currency.String(), tran.TrnType, name)
}
}
}

View File

@ -178,12 +178,12 @@ func printInvTran(it *ofxgo.InvTran) {
fmt.Printf("%s", it.DtTrade)
}
func printInvBuy(defCurrency ofxgo.String, ib *ofxgo.InvBuy) {
func printInvBuy(defCurrency ofxgo.CurrSymbol, ib *ofxgo.InvBuy) {
printInvTran(&ib.InvTran)
currency := defCurrency
if ib.Currency != nil {
if ok, _ := ib.Currency.CurSym.Valid(); ok {
currency = ib.Currency.CurSym
} else if ib.OrigCurrency != nil {
} else if ok, _ := ib.OrigCurrency.CurSym.Valid(); ok {
currency = ib.Currency.CurSym
}
@ -191,15 +191,15 @@ func printInvBuy(defCurrency ofxgo.String, ib *ofxgo.InvBuy) {
// TODO print ticker instead of CUSIP
}
func printInvSell(defCurrency ofxgo.String, is *ofxgo.InvSell) {
func printInvSell(defCurrency ofxgo.CurrSymbol, is *ofxgo.InvSell) {
printInvTran(&is.InvTran)
currency := defCurrency
if is.Currency != nil {
if ok, _ := is.Currency.CurSym.Valid(); ok {
currency = is.Currency.CurSym
} else if is.OrigCurrency != nil {
} else if ok, _ := is.OrigCurrency.CurSym.Valid(); ok {
currency = is.Currency.CurSym
}
fmt.Printf(" %s (%s %s)@%s %s (Total: %s)\n", is.Units, is.SecID.UniqueIDType, is.SecID.UniqueID, is.UnitPrice, currency, is.Total)
fmt.Printf(" %s (%s %s)@%s %s (Total: %s)\n", is.Units, is.SecID.UniqueIDType, is.SecID.UniqueID, is.UnitPrice, currency.String(), is.Total)
// TODO print ticker instead of CUSIP
}

View File

@ -290,7 +290,7 @@ type InvAcct struct {
// Currency represents one ISO-4217 currency
type Currency struct {
XMLName xml.Name // CURRENCY or ORIGCURRENCY
CurRate Amount `xml:"CURRATE"` // Ratio of <CURDEF> currency to <CURSYM> currency
CurSym String `xml:"CURSYM"` // ISO-4217 3-character currency identifier
XMLName xml.Name // CURRENCY or ORIGCURRENCY
CurRate Amount `xml:"CURRATE"` // Ratio of <CURDEF> currency to <CURSYM> currency
CurSym CurrSymbol `xml:"CURSYM"` // ISO-4217 3-character currency identifier
}

View File

@ -48,7 +48,7 @@ type CCStatementResponse struct {
Status Status `xml:"STATUS"`
CltCookie String `xml:"CLTCOOKIE,omitempty"`
// TODO `xml:"OFXEXTENSION,omitempty"`
CurDef String `xml:"CCSTMTRS>CURDEF"`
CurDef CurrSymbol `xml:"CCSTMTRS>CURDEF"`
CCAcctFrom CCAcct `xml:"CCSTMTRS>CCACCTFROM"`
BankTranList *TransactionList `xml:"CCSTMTRS>BANKTRANLIST,omitempty"`
//BANKTRANLISTP

View File

@ -132,13 +132,18 @@ NEWFILEUID:NONE
balamt.SetFrac64(-933400, 100)
availbalamt.SetFrac64(763017, 100)
usd, err := ofxgo.NewCurrSymbol("USD")
if err != nil {
t.Fatalf("Unexpected error creating CurrSymbol for USD\n")
}
statementResponse := ofxgo.CCStatementResponse{
TrnUID: "59e850ad-7448-b4ce-4b71-29057763b306",
Status: ofxgo.Status{
Code: 0,
Severity: "INFO",
},
CurDef: "USD",
CurDef: *usd,
CCAcctFrom: ofxgo.CCAcct{
AcctID: "9283744488463775",
},

View File

@ -1163,7 +1163,7 @@ type InvStatementResponse struct {
CltCookie String `xml:"CLTCOOKIE,omitempty"`
// TODO `xml:"OFXEXTENSION,omitempty"`
DtAsOf Date `xml:"INVSTMTRS>DTASOF"`
CurDef String `xml:"INVSTMTRS>CURDEF"`
CurDef CurrSymbol `xml:"INVSTMTRS>CURDEF"`
InvAcctFrom InvAcct `xml:"INVSTMTRS>INVACCTFROM"`
InvTranList *InvTranList `xml:"INVSTMTRS>INVTRANLIST,omitempty"`
InvPosList PositionList `xml:"INVSTMTRS>INVPOSLIST,omitempty"`

View File

@ -417,6 +417,11 @@ func TestUnmarshalInvStatementResponse(t *testing.T) {
oounits2.SetFrac64(25, 1)
oolimitprice2.SetFrac64(1975, 100)
usd, err := ofxgo.NewCurrSymbol("USD")
if err != nil {
t.Fatalf("Unexpected error creating CurrSymbol for USD\n")
}
statementResponse := ofxgo.InvStatementResponse{
TrnUID: "1a0117ad-692b-4c6a-a21b-020d37d34d49",
Status: ofxgo.Status{
@ -424,7 +429,7 @@ func TestUnmarshalInvStatementResponse(t *testing.T) {
Severity: "INFO",
},
DtAsOf: *ofxgo.NewDateGMT(2017, 3, 31, 0, 0, 0, 0),
CurDef: "USD",
CurDef: *usd,
InvAcctFrom: ofxgo.InvAcct{
BrokerID: "invstrus.com",
AcctID: "91827364",
@ -861,6 +866,11 @@ NEWFILEUID: NONE
posunitprice2.SetFrac64(2987, 100)
posmktval2.SetFrac64(2987, 1)
usd, err := ofxgo.NewCurrSymbol("USD")
if err != nil {
t.Fatalf("Unexpected error creating CurrSymbol for USD\n")
}
statementResponse := ofxgo.InvStatementResponse{
TrnUID: "1283719872",
Status: ofxgo.Status{
@ -868,7 +878,7 @@ NEWFILEUID: NONE
Severity: "INFO",
},
DtAsOf: *ofxgo.NewDateGMT(2017, 4, 3, 12, 0, 0, 0),
CurDef: "USD",
CurDef: *usd,
InvAcctFrom: ofxgo.InvAcct{
BrokerID: "www.exampletrader.com",
AcctID: "12341234",