Skip to content

Commit

Permalink
Improve logic that parses file as json or plaintext
Browse files Browse the repository at this point in the history
  • Loading branch information
burnettekm committed Mar 5, 2024
1 parent 0c7118c commit 3e4771e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
9 changes: 7 additions & 2 deletions ACHDictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
9 changes: 7 additions & 2 deletions WIREDictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
10 changes: 1 addition & 9 deletions cmd/server/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/download/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 3e4771e

Please sign in to comment.