summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2024-12-08 08:18:43 +0100
committerPatrick Spek <p.spek@tyil.nl>2024-12-08 08:18:43 +0100
commit504cc533cc0abb0dd4243ec5a77854134591ffca (patch)
treeddb8dabda6f403119a7e391b80871b7d4278100a
parent1bad7ba1ecc4c8a4d55e8c4c964ac1ff586ec9eb (diff)
downloadfirefly-importer-504cc533cc0abb0dd4243ec5a77854134591ffca.tar.gz
firefly-importer-504cc533cc0abb0dd4243ec5a77854134591ffca.tar.bz2
Apply some cachingHEADmaster
-rw-r--r--go.mod5
-rw-r--r--go.sum2
-rw-r--r--main.go69
3 files changed, 47 insertions, 29 deletions
diff --git a/go.mod b/go.mod
index bb210f5..d8de760 100644
--- a/go.mod
+++ b/go.mod
@@ -2,4 +2,7 @@ module git.tyil.nl/firefly-importer
go 1.22.6
-require golang.org/x/text v0.21.0
+require (
+ git.tyil.nl/go/cache.git v0.0.0-20241208071603-125410025f74
+ golang.org/x/text v0.21.0
+)
diff --git a/go.sum b/go.sum
index 0d8f5b2..a9ccfcd 100644
--- a/go.sum
+++ b/go.sum
@@ -1,2 +1,4 @@
+git.tyil.nl/go/cache.git v0.0.0-20241208071603-125410025f74 h1:wEq+kZon7Lgv12RjxuuaAr6neanQsR2yzalae38dmRg=
+git.tyil.nl/go/cache.git v0.0.0-20241208071603-125410025f74/go.mod h1:dQ57uMt/6qGNc65M7HsKNYBOUJV+Zca7d6+1F1ILVcs=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
diff --git a/main.go b/main.go
index 344b976..2fcb6e4 100644
--- a/main.go
+++ b/main.go
@@ -7,6 +7,8 @@ import (
"io"
"log/slog"
"os"
+
+ "git.tyil.nl/go/cache.git"
)
func main() {
@@ -37,6 +39,9 @@ func main() {
panic("Failed to parse XML")
}
+ // Instantiate cache object
+ cache := cache.New()
+
// Show the output header
if *header {
fmt.Printf(
@@ -64,15 +69,15 @@ func main() {
// Set the SourceName of the transaction based on the IBAN
sourceIban := ntry.SourceIban(doc)
if sourceIban != "" {
- sourceAccountName, err := fireflyApiAccountGetNameByIban(*baseUrl, *pat, sourceIban)
- if err != nil {
- fmt.Printf("%-6.6s (%s)\n", "!", err)
- continue
- }
-
- if sourceAccountName != "" {
- tx.SourceName = sourceAccountName
- }
+ tx.SourceName = cache.GetSet("iban:"+sourceIban, func() interface{} {
+ sourceAccountName, err := fireflyApiAccountGetNameByIban(*baseUrl, *pat, sourceIban)
+ if err != nil {
+ fmt.Printf("%-6.6s (%s)\n", "!", err)
+ return ""
+ }
+
+ return sourceAccountName
+ }).(string)
}
// Fall back to just using the given name in the Ntry
@@ -83,15 +88,15 @@ func main() {
// Set the DestinationName of the transaction based on the IBAN
destinationIban := ntry.DestinationIban(doc)
if destinationIban != "" {
- destinationAccountName, err := fireflyApiAccountGetNameByIban(*baseUrl, *pat, destinationIban)
- if err != nil {
- fmt.Printf("%-6.6s (%s)\n", "!", err)
- continue
- }
-
- if destinationAccountName != "" {
- tx.DestinationName = destinationAccountName
- }
+ tx.DestinationName = cache.GetSet("iban:"+destinationIban, func() interface{} {
+ destinationAccountName, err := fireflyApiAccountGetNameByIban(*baseUrl, *pat, destinationIban)
+ if err != nil {
+ fmt.Printf("%-6.6s (%s)\n", "!", err)
+ return ""
+ }
+
+ return destinationAccountName
+ }).(string)
}
// Fall back to just using the given name in the Ntry
@@ -123,17 +128,25 @@ func main() {
}
// Set the transfer type
- destinationType, err := fireflyApiAccountGetTypeByName(*baseUrl, *pat, tx.DestinationName)
- if err != nil {
- fmt.Printf("%-6.6s (%s)\n", "!", err)
- continue
- }
+ destinationType := cache.GetSet("type:"+tx.DestinationName, func() interface{} {
+ destinationType, err := fireflyApiAccountGetTypeByName(*baseUrl, *pat, tx.DestinationName)
+ if err != nil {
+ fmt.Printf("%-6.6s (%s)\n", "!", err)
+ return ""
+ }
- sourceType, err := fireflyApiAccountGetTypeByName(*baseUrl, *pat, tx.SourceName)
- if err != nil {
- fmt.Printf("%-6.6s (%s)\n", "!", err)
- continue
- }
+ return destinationType
+ }).(string)
+
+ sourceType := cache.GetSet("type:"+tx.SourceName, func() interface{} {
+ sourceType, err := fireflyApiAccountGetTypeByName(*baseUrl, *pat, tx.SourceName)
+ if err != nil {
+ fmt.Printf("%-6.6s (%s)\n", "!", err)
+ return ""
+ }
+
+ return sourceType
+ }).(string)
if sourceType == "asset" && destinationType == "asset" {
tx.Type = "transfer"