Skip to content

Commit

Permalink
Merge pull request #268 from hasheddan/yamlplus
Browse files Browse the repository at this point in the history
Use YAML document terminators and skip empty documents in parser
  • Loading branch information
hasheddan authored Jun 14, 2021
2 parents ded1778 + d76ee9b commit 0d9a40f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
4 changes: 3 additions & 1 deletion pkg/parser/fsreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,14 @@ func (r *FsReadCloser) Read(p []byte) (n int, err error) {
r.index++
r.position = 0
r.wroteBreak = false
n = copy(p, "\n---\n")
return n, nil
}
if r.index == len(r.paths) {
return 0, io.EOF
}
if r.writeBreak {
n = copy(p, "\n---\n")
n = copy(p, "\n...\n")
r.writeBreak = false
r.wroteBreak = true
return n, nil
Expand Down
13 changes: 13 additions & 0 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"io/ioutil"
"strings"
"unicode"

"github.com/pkg/errors"
"github.com/spf13/afero"
Expand Down Expand Up @@ -114,6 +115,18 @@ func (p *PackageParser) Parse(ctx context.Context, reader io.ReadCloser) (*Packa
if err != nil {
o, _, err := do.Decode(bytes, nil, nil)
if err != nil {
empty := true
for _, b := range bytes {
if !unicode.IsSpace(rune(b)) {
empty = false
break
}
}
// If the YAML document only contains Unicode White Space we
// ignore and do not return an error.
if empty {
continue
}
if anno, ok := reader.(AnnotatedReadCloser); ok {
return pkg, errors.Wrap(err, fmt.Sprintf("%+v", anno.Annotate()))
}
Expand Down
18 changes: 17 additions & 1 deletion pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ var _ Parser = &PackageParser{}
var (
crdBytes = []byte(`apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: test`)

whitespaceBytes = []byte(`---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: test
---
---
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: test`)

Expand All @@ -53,6 +68,7 @@ func TestParser(t *testing.T) {
allBytes := bytes.Join([][]byte{crdBytes, deployBytes}, []byte("\n---\n"))
fs := afero.NewMemMapFs()
_ = afero.WriteFile(fs, "crd.yaml", crdBytes, 0o644)
_ = afero.WriteFile(fs, "whitespace.yaml", whitespaceBytes, 0o644)
_ = afero.WriteFile(fs, "deployment.yaml", deployBytes, 0o644)
_ = afero.WriteFile(fs, "some/nested/dir/crd.yaml", crdBytes, 0o644)
_ = afero.WriteFile(fs, ".crossplane/bad.yaml", crdBytes, 0o644)
Expand Down Expand Up @@ -109,7 +125,7 @@ func TestParser(t *testing.T) {
backend: NewFsBackend(fs, FsDir("."), FsFilters(SkipDirs(), SkipNotYAML(), SkipPath(".crossplane/*"))),
pkg: &Package{
meta: []runtime.Object{deploy},
objects: []runtime.Object{crd, crd},
objects: []runtime.Object{crd, crd, crd, crd},
},
},
"FsBackendAll": {
Expand Down

0 comments on commit 0d9a40f

Please sign in to comment.