Skip to content

Commit

Permalink
Merge pull request #406 from adamdecaf/server-null-pointer
Browse files Browse the repository at this point in the history
server: cleanup null panics
  • Loading branch information
adamdecaf authored Dec 13, 2018
2 parents 3aef105 + 9059924 commit a28fd40
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions server/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package server

import (
"errors"
"fmt"
"sync"

"github.com/moov-io/ach"
Expand Down Expand Up @@ -81,52 +82,76 @@ func (r *repositoryInMemory) DeleteFile(id string) error {
func (r *repositoryInMemory) StoreBatch(fileID string, batch ach.Batcher) error {
r.mtx.Lock()
defer r.mtx.Unlock()

// Ensure the file does not already exist
if _, ok := r.files[fileID]; !ok {
file, ok := r.files[fileID]
if !ok || file == nil {
return ErrNotFound
}

// ensure the batch does not already exist
for _, val := range r.files[fileID].Batches {
for _, val := range file.Batches {
if val.ID() == batch.ID() {
return ErrAlreadyExists
}
}

// Add the batch to the file
r.files[fileID].AddBatch(batch)

return nil
}

// FindBatch retrieves a ach.Batcher based on the supplied ID
func (r *repositoryInMemory) FindBatch(fileID string, batchID string) (ach.Batcher, error) {
r.mtx.RLock()
defer r.mtx.RUnlock()
for _, val := range r.files[fileID].Batches {

file, ok := r.files[fileID]
if !ok || file == nil {
return nil, ErrNotFound
}

for _, val := range file.Batches {
if val.ID() == batchID {
return val, nil
}
}

return nil, ErrNotFound
}

// FindAllBatches
func (r *repositoryInMemory) FindAllBatches(fileID string) []ach.Batcher {
r.mtx.RLock()
defer r.mtx.RUnlock()
batches := make([]ach.Batcher, 0, len(r.files[fileID].Batches))
batches = append(batches, r.files[fileID].Batches...)

file, ok := r.files[fileID]
if !ok || file == nil {
return nil
}

batches := make([]ach.Batcher, 0, len(file.Batches))
batches = append(batches, file.Batches...)

return batches
}

func (r *repositoryInMemory) DeleteBatch(fileID string, batchID string) error {
r.mtx.Lock()
defer r.mtx.Unlock()

for i := len(r.files[fileID].Batches) - 1; i >= 0; i-- {
if r.files[fileID].Batches[i].ID() == batchID {
r.files[fileID].Batches = append(r.files[fileID].Batches[:i], r.files[fileID].Batches[i+1:]...)
//fmt.Println(r.files[fileID].Batches)
file, ok := r.files[fileID]
if !ok || file == nil {
return fmt.Errorf("%v: no file %s with batch %s found", ErrNotFound, fileID, batchID)
}

for i := len(file.Batches) - 1; i >= 0; i-- {
if file.Batches[i].ID() == batchID {
file.Batches = append(file.Batches[:i], file.Batches[i+1:]...)
return nil
}
}

return ErrNotFound
}

0 comments on commit a28fd40

Please sign in to comment.