Skip to content

Commit

Permalink
Merge pull request #234 from noborus/yaml-recover
Browse files Browse the repository at this point in the history
Preventing panic due to invalid yaml file
  • Loading branch information
noborus authored Sep 24, 2023
2 parents 9363368 + 7a74366 commit aa8232b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
- ubuntu-latest
- macos-latest
- windows-latest
exclude:
- go-version: "1.19"
platform: windows-latest
dbtest:
runs-on: ubuntu-latest
services:
Expand Down
21 changes: 16 additions & 5 deletions input_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ func jqParse(q string) (*gojq.Query, error) {
return query, nil
}

func (r *YAMLReader) wrapDecode(v interface{}) (err error) {
defer func() {
if rec := recover(); rec != nil {
err = fmt.Errorf("%s", rec)
}
}()

err = r.reader.Decode(v)
return
}

// yamlParse parses YAML and stores it in preRead.
func (r *YAMLReader) yamlParse(opts *ReadOpts) error {
r.limitRead = opts.InLimitRead
Expand All @@ -66,9 +77,9 @@ func (r *YAMLReader) yamlParse(opts *ReadOpts) error {

var top interface{}
for i := 0; i < opts.InPreRead; i++ {
if err := r.reader.Decode(&top); err != nil {
if err := r.wrapDecode(&top); err != nil {
if !errors.Is(err, io.EOF) {
return err
return fmt.Errorf("%w: %s", ErrInvalidYAML, err)
}
debug.Printf(err.Error())
return nil
Expand Down Expand Up @@ -209,8 +220,8 @@ func (r *YAMLReader) ReadRow(row []interface{}) ([]interface{}, error) {
return v, nil
}

func (r *YAMLReader) rowParse(row []interface{}, YAMLRow interface{}) []interface{} {
switch m := YAMLRow.(type) {
func (r *YAMLReader) rowParse(row []interface{}, yamlRow interface{}) []interface{} {
switch m := yamlRow.(type) {
case map[string]interface{}:
for i := range r.names {
row[i] = r.toString(m[r.names[i]])
Expand All @@ -219,7 +230,7 @@ func (r *YAMLReader) rowParse(row []interface{}, YAMLRow interface{}) []interfac
for i := range r.names {
row[i] = nil
}
row[0] = r.toString(YAMLRow)
row[0] = r.toString(yamlRow)
}
return row
}
Expand Down

0 comments on commit aa8232b

Please sign in to comment.