Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore whitespace when parsing examples #278

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion internal/xpkg/parser/examples/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (
"bufio"
"context"
"io"
"unicode"

"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/parser"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/yaml"
Expand Down Expand Up @@ -64,11 +66,35 @@ func (p *Parser) Parse(ctx context.Context, reader io.ReadCloser) (*Examples, er
if len(bytes) == 0 {
continue
}
if isWhiteSpace(bytes) {
continue
}
Comment on lines +69 to +71
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on adding a test case or two for the behavior you're seeing? I clearly failed to add tests in this package the first time through 😞 , but in the event we find a fix, it would be good to know if/when we accidentally regressed the changes 👍.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, will add tests before moving out of draft -- I discovered this is an issue upstream as well though so will likely fix then propagate 👍🏻

var obj unstructured.Unstructured
if err := k8syaml.Unmarshal(bytes, &obj); err != nil {
return ex, err
return ex, annotateErr(err, reader)
}
ex.objects = append(ex.objects, obj)
}
return ex, nil
}

// isWhiteSpace determines whether the passed in bytes are all unicode white
// space.
func isWhiteSpace(bytes []byte) bool {
empty := true
for _, b := range bytes {
if !unicode.IsSpace(rune(b)) {
empty = false
break
}
}
return empty
}

// annotateErr annotates an error if the reader is an AnnotatedReadCloser.
func annotateErr(err error, reader io.ReadCloser) error {
if anno, ok := reader.(parser.AnnotatedReadCloser); ok {
return errors.Wrapf(err, "%+v", anno.Annotate())
}
return err
}