-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from golobby/yaml-feeder
adds yaml feeder
- Loading branch information
Showing
6 changed files
with
198 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package feeder | ||
|
||
import ( | ||
"gopkg.in/yaml.v2" | ||
"io/ioutil" | ||
) | ||
|
||
type Yaml struct { | ||
Path string | ||
} | ||
|
||
func (y *Yaml) Feed() (map[string]interface{}, error) { | ||
bs, err := ioutil.ReadFile(y.Path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
items := make(map[string]interface{}) | ||
|
||
err = yaml.Unmarshal(bs, items) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return items, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package feeder | ||
|
||
import ( | ||
"io/ioutil" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// YamlDirectory is a feeder that feeds using a directory of yaml files. | ||
type YamlDirectory struct { | ||
Path string | ||
} | ||
|
||
// Feed returns all the content. | ||
func (yd YamlDirectory) Feed() (map[string]interface{}, error) { | ||
files, err := ioutil.ReadDir(yd.Path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
all := map[string]interface{}{} | ||
|
||
for _, f := range files { | ||
if f.IsDir() { | ||
continue | ||
} | ||
|
||
j := Yaml{Path: filepath.Join(yd.Path, string(filepath.Separator), f.Name())} | ||
|
||
items, err := j.Feed() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
k := strings.Split(f.Name(), ".")[0] | ||
all[k] = items | ||
} | ||
|
||
return all, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters