diff options
| author | Patrick Spek <p.spek@tyil.nl> | 2024-12-08 08:18:43 +0100 |
|---|---|---|
| committer | Patrick Spek <p.spek@tyil.nl> | 2024-12-08 08:18:43 +0100 |
| commit | 504cc533cc0abb0dd4243ec5a77854134591ffca (patch) | |
| tree | ddb8dabda6f403119a7e391b80871b7d4278100a | |
| parent | 1bad7ba1ecc4c8a4d55e8c4c964ac1ff586ec9eb (diff) | |
| download | firefly-importer-master.tar.gz firefly-importer-master.tar.bz2 | |
| -rw-r--r-- | go.mod | 5 | ||||
| -rw-r--r-- | go.sum | 2 | ||||
| -rw-r--r-- | main.go | 69 |
3 files changed, 47 insertions, 29 deletions
@@ -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 +) @@ -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= @@ -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" |
