Skip to content

Commit

Permalink
Merge branch 'master' into Support-NOC-for-IAT-Entries-#328
Browse files Browse the repository at this point in the history
  • Loading branch information
bkmoovio committed Oct 30, 2018
2 parents d19c6e9 + ff055d8 commit b0bda25
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
33 changes: 28 additions & 5 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ func NewFile() *File {
}
}

type fileHeader struct {
Header FileHeader `json:"fileHeader"`
}

type fileControl struct {
Control FileControl `json:"fileControl"`
}

// FileFromJson attempts to return a *File object assuming the input is valid JSON.
//
// Callers should always check for a nil-error before using the returned file.
Expand All @@ -86,15 +94,30 @@ func FileFromJson(bs []byte) (*File, error) {
return nil, errors.New("no json data provided")
}

file := File{Header: NewFileHeader()}
// Read FileHeader
header := fileHeader{
Header: NewFileHeader(),
}
if err := json.NewDecoder(bytes.NewReader(bs)).Decode(&header); err != nil {
return nil, fmt.Errorf("problem reading FileHeader: %v", err)
}

// Read what we can and then custom read batches
json.NewDecoder(bytes.NewReader(bs)).Decode(&file)
// Read FileControl
control := fileControl{
Control: NewFileControl(),
}
if err := json.NewDecoder(bytes.NewReader(bs)).Decode(&control); err != nil {
return nil, fmt.Errorf("problem reading FileControl: %v", err)
}

// Build resulting file
file := NewFile()
if err := file.setBatchesFromJson(backup); err != nil {
return nil, err
}

return &file, nil
file.Header = header.Header
file.Control = control.Control
return file, nil
}

func (f *File) UnmarshalJSON(p []byte) error {
Expand Down
20 changes: 20 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,30 @@ func TestFile__readFromJson(t *testing.T) {
t.Fatal(err)
}

// Header
if file.Header.ImmediateOrigin != "121042882" || file.Header.ImmediateOriginName != "Wells Fargo" {
t.Errorf("origin=%s name=%s", file.Header.ImmediateOrigin, file.Header.ImmediateOriginName)
}
if file.Header.ImmediateDestination != "231380104" || file.Header.ImmediateDestinationName != "Citadel" {
t.Errorf("destination=%s name=%s", file.Header.ImmediateDestination, file.Header.ImmediateDestinationName)
}
if file.Header.FileCreationTime.IsZero() || file.Header.FileCreationDate.IsZero() {
t.Errorf("time=%v date=%v", file.Header.FileCreationTime, file.Header.FileCreationDate)
}

// Batches
if len(file.Batches) != 1 {
t.Errorf("got %d batches: %v", len(file.Batches), file.Batches)
}

// Control
if file.Control.BatchCount != 1 {
t.Errorf("BatchCount: %d", file.Control.BatchCount)
}
if file.Control.TotalDebitEntryDollarAmountInFile != 0 || file.Control.TotalCreditEntryDollarAmountInFile != 100000 {
t.Errorf("debit=%d credit=%d", file.Control.TotalDebitEntryDollarAmountInFile, file.Control.TotalCreditEntryDollarAmountInFile)
}

// ensure we error on struct tag unmarshal
var f File
err = json.Unmarshal(bs, &f)
Expand Down

0 comments on commit b0bda25

Please sign in to comment.