Skip to content

Commit

Permalink
feat: add the validate for DirectoryLoader (#119)
Browse files Browse the repository at this point in the history
Co-authored-by: Simone Vellei <henomis@gmail.com>
  • Loading branch information
FlyingDuck and henomis authored Sep 6, 2023
1 parent 7cba748 commit 5e29dde
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions loader/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package loader

import (
"context"
"fmt"

"github.com/henomis/lingoose/document"

Expand Down Expand Up @@ -33,6 +34,11 @@ func (d *DirectoryLoader) WithTextSplitter(textSplitter TextSplitter) *Directory

func (d *DirectoryLoader) Load(ctx context.Context) ([]document.Document, error) {

err := d.validate()
if err != nil {
return nil, err
}

regExp, err := regexp.Compile(d.regExPathMatch)
if err != nil {
return nil, err
Expand Down Expand Up @@ -62,3 +68,17 @@ func (d *DirectoryLoader) Load(ctx context.Context) ([]document.Document, error)

return docs, nil
}

func (d *DirectoryLoader) validate() error {

fileStat, err := os.Stat(d.dirname)
if err != nil {
return fmt.Errorf("%s: %w", ErrorInternal, err)
}

if !fileStat.IsDir() {
return fmt.Errorf("%s: %w", ErrorInternal, os.ErrNotExist)
}

return nil
}

0 comments on commit 5e29dde

Please sign in to comment.