Skip to content

Commit

Permalink
refactor: return empty slice instead of error
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Howard <jonathan.w.howard@lmco.com>
  • Loading branch information
jhoward-lm committed Jun 21, 2024
1 parent 61c3c58 commit 70be21d
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions backends/ent/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (

var (
errMultipleDocuments = errors.New("multiple documents matching ID")
errNoDocuments = errors.New("no documents matching IDs")
errMissingDocument = errors.New("no documents matching IDs")
)

// Retrieve implements the storage.Retriever interface.
func (backend *Backend) Retrieve(id string, _ *storage.RetrieveOptions) (*sbom.Document, error) {
func (backend *Backend) Retrieve(id string, _ *storage.RetrieveOptions) (doc *sbom.Document, err error) {
if backend.client == nil {
return nil, fmt.Errorf("%w", errUninitializedClient)
}
Expand All @@ -34,16 +34,18 @@ func (backend *Backend) Retrieve(id string, _ *storage.RetrieveOptions) (*sbom.D
backend.Options = NewBackendOptions()
}

documents, err := backend.GetDocumentsByID(id)
if err != nil {
return nil, fmt.Errorf("querying documents: %w", err)
}

if len(documents) > 1 {
return nil, fmt.Errorf("%w %s", errMultipleDocuments, id)
switch documents, getDocsErr := backend.GetDocumentsByID(id); {
case getDocsErr != nil:
err = fmt.Errorf("querying documents: %w", getDocsErr)
case len(documents) == 0:
err = fmt.Errorf("%w %s", errMissingDocument, id)
case len(documents) > 1:
err = fmt.Errorf("%w %s", errMultipleDocuments, id)
default:
doc = documents[0]
}

return documents[0], nil
return
}

func (backend *Backend) GetDocumentsByID(ids ...string) ([]*sbom.Document, error) {
Expand All @@ -62,10 +64,6 @@ func (backend *Backend) GetDocumentsByID(ids ...string) ([]*sbom.Document, error
return nil, fmt.Errorf("querying documents table: %w", err)
}

if len(entDocs) == 0 {
return nil, fmt.Errorf("%w %s", errNoDocuments, ids)
}

for _, entDoc := range entDocs {
entDoc.Edges.Metadata, err = entDoc.QueryMetadata().
WithAuthors().
Expand Down

0 comments on commit 70be21d

Please sign in to comment.