From 3e4771e073425d1ef875047ca0f493fdfdad1f2e Mon Sep 17 00:00:00 2001 From: Kristi Burnette Date: Tue, 5 Mar 2024 09:46:18 -0700 Subject: [PATCH] Improve logic that parses file as json or plaintext --- ACHDictionary.go | 9 +++++++-- WIREDictionary.go | 9 +++++++-- cmd/server/reader.go | 10 +--------- pkg/download/download.go | 6 +++--- pkg/download/download_test.go | 2 +- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/ACHDictionary.go b/ACHDictionary.go index a82440ab..9daf3e68 100644 --- a/ACHDictionary.go +++ b/ACHDictionary.go @@ -121,10 +121,15 @@ func (f *ACHDictionary) Read(r io.Reader) error { return err } - // Try reading the file as JSON and if that fails read as plaintext - if err := f.readJSON(bytes.NewReader(bs)); err == nil { + // Try validating the file as JSON and if that fails read as plaintext + if json.Valid(bs) { + err = f.readJSON(bytes.NewReader(bs)) + if err != nil { + return err + } return nil } + return f.readPlaintext(bytes.NewReader(bs)) } diff --git a/WIREDictionary.go b/WIREDictionary.go index f0c314c8..83e75a1c 100644 --- a/WIREDictionary.go +++ b/WIREDictionary.go @@ -119,10 +119,15 @@ func (f *WIREDictionary) Read(r io.Reader) error { return err } - // Try reading the file as JSON and if that fails read as plaintext - if err := f.readJSON(bytes.NewReader(bs)); err == nil { + // Try validating the file as JSON and if that fails read as plaintext + if json.Valid(bs) { + err = f.readJSON(bytes.NewReader(bs)) + if err != nil { + return err + } return nil } + return f.readPlaintext(bytes.NewReader(bs)) } diff --git a/cmd/server/reader.go b/cmd/server/reader.go index 6b822d23..d23f2fc0 100644 --- a/cmd/server/reader.go +++ b/cmd/server/reader.go @@ -55,16 +55,8 @@ func fedWireDataFile(logger log.Logger) (io.Reader, error) { } func attemptFileDownload(logger log.Logger, listName string) (io.Reader, error) { - routingNumber := os.Getenv("FRB_ROUTING_NUMBER") - downloadCode := os.Getenv("FRB_DOWNLOAD_CODE") - downloadURL := os.Getenv("FRB_DOWNLOAD_URL_TEMPLATE") - logger.Logf("download: attempting %s", listName) - client, err := download.NewClient(&download.ClientOpts{ - RoutingNumber: routingNumber, - DownloadCode: downloadCode, - DownloadURL: downloadURL, - }) + client, err := download.NewClient(nil) if err != nil { return nil, fmt.Errorf("client setup: %w", err) } diff --git a/pkg/download/download.go b/pkg/download/download.go index 557b21c4..6d45b855 100644 --- a/pkg/download/download.go +++ b/pkg/download/download.go @@ -53,12 +53,12 @@ func NewClient(opts *ClientOpts) (*Client, error) { downloadcd, dcdExists := os.LookupEnv("FRB_DOWNLOAD_CODE") downloadurltemp, urlExists := os.LookupEnv("FRB_DOWNLOAD_URL_TEMPLATE") - if !urlExists { - if !rnExists { + if !urlExists || downloadurltemp == "" { + if !rnExists || routingNum == "" { return nil, fmt.Errorf("%w: %w", ErrMissingConfigValue, ErrMissingRoutingNumber) } - if !dcdExists { + if !dcdExists || downloadcd == "" { return nil, fmt.Errorf("%w: %w", ErrMissingConfigValue, ErrMissingDownloadCD) } diff --git a/pkg/download/download_test.go b/pkg/download/download_test.go index 38a1e56a..919f0d31 100644 --- a/pkg/download/download_test.go +++ b/pkg/download/download_test.go @@ -95,7 +95,7 @@ func TestClient__fedwire(t *testing.T) { } func TestClient__wire_custom_url(t *testing.T) { - file, err := os.ReadFile(filepath.Join("..", "..", "data", "fedwiredir.json")) + file, err := os.ReadFile(filepath.Join("..", "..", "data", "fpddir.json")) if err != nil { t.Fatal(err) }