Skip to content

Commit

Permalink
add support for wildcard filename
Browse files Browse the repository at this point in the history
  • Loading branch information
IanSmith123 committed Jul 22, 2021
1 parent 4da4f88 commit 6f7782b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
17 changes: 17 additions & 0 deletions examples/v2/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,23 @@ files:
- name: likeness
type: double

- path: ./glob-follow-*.csv
failDataPath: ./err/follow-glob.csv
batchSize: 2
type: csv
csv:
withHeader: false
withLabel: false
schema:
type: edge
edge:
name: follow
withRanking: true
props:
- name: likeness
type: double


- path: ./follow-with-header.csv
failDataPath: ./err/follow-with-header.csv
batchSize: 2
Expand Down
3 changes: 3 additions & 0 deletions examples/v2/glob-follow-1.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x200,y201,0,92.5
y201,x200,1,85.6
y201,z202,2,93.2
1 change: 1 addition & 0 deletions examples/v2/glob-follow-2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
y201,z202,1,96.2
35 changes: 35 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ func (config *YAMLConfig) ValidateAndReset(dir string) error {
return errors.New("There is no files in configuration")
}

//TODO(yuyu): check each item in config.Files
// if item is a directory, iter this directory and replace this directory config section by filename config section
if err := config.expandDirectoryToFiles(dir); err != nil {
logger.Errorf("%s", err)
}
for i := range config.Files {
if err := config.Files[i].validateAndReset(dir, fmt.Sprintf("files[%d]", i)); err != nil {
return err
Expand Down Expand Up @@ -786,3 +791,33 @@ func (t *Tag) validateAndReset(prefix string, start int) error {
}
return nil
}

// TODO(yuyu): discuss about the config schema for import by directory
func (config *YAMLConfig) expandDirectoryToFiles(dir string) (err error) {
// change config.Files in its own iter is not a good idea, so save value and change it later
var newFiles []*File

for i := range config.Files {
if !base.HasHttpPrefix(*config.Files[i].Path) {
// treat all string as glob pattern
files, err := filepath.Glob(filepath.Join(dir, *config.Files[i].Path))
if err != nil || len(files) == 0 {
err = errors.New(fmt.Sprintf("error string: %s", *config.Files[i].Path))
logger.Errorf("%s", err)
return err
}

logger.Infof("query file from pattern: %s, result: %v", *config.Files[i].Path, files)
for j := range files {
eachConf := config.Files[i]
eachConf.Path = &files[j]
newFiles = append(newFiles, eachConf)
}
} else {
newFiles = append(newFiles, config.Files[i])
}
}
config.Files = newFiles

return err
}

0 comments on commit 6f7782b

Please sign in to comment.