Repository navigation
feat(load): add a document loader that keeps source positions - #574
Open
GenerikUser wants to merge 1 commit into
Open
GenerikUser wants to merge 1 commit into
GenerikUser wants to merge 1 commit into
Conversation
Add NewDocumentLoader, SplitDocuments and Document to pkg/load. The new loader reads the same sources as NewLoader (a comma-separated list of files, directories searched recursively for .yaml and .yml files, and "-" for stdin) and splits each stream into documents exactly as YamlStream does. Instead of decoding the documents, it returns each one's raw bytes, comments included, with the file it came from and the line it starts on, so callers can report positions in the source. A WithStdin option sets the reader "-" reads, defaulting to os.Stdin, so callers and tests can supply stdin without replacing os.Stdin. Existing loaders and their callers are unchanged. Tests check that SplitDocuments returns the same documents as YamlStream and records exact start lines across separators, separators with trailing comments, leading comments, empty documents, CRLF line endings and a missing final newline, and that WithStdin is honoured. Signed-off-by: GenerikUser <generikuser@users.noreply.github.com>
GenerikUser
requested review from
a team,
jcogilvie and
tampakrap
as code owners
November 20, 2026 17:30
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of your changes
Adds a document-level loader to
pkg/load, alongside the existingLoad().pkg/loadtoday decodes each YAML document straight into an unstructured object. That loses what some callers need: which file a document came from, where in that file it starts, and its raw bytes with comments. A caller that reports positions in the source cannot get them from the existing loaders. The first such caller will becrossplane xrd lint(#481), which will be rebased onto this PR so that it reads its inputs throughpkg/loadrather than through a loader of its own.What it adds:
Document{Source, StartLine, Raw}: one YAML document as it appears in its source.Sourceis the file path, or-for stdin.NewDocumentLoader(input, ...DocumentLoaderOption): accepts the same sources asNewLoader: a comma-separated list of files, directories searched recursively for.yamland.ymlfiles, and-for stdin. It returns the documents without decoding them, so empty documents and documents that are not Kubernetes objects are returned too.SplitDocuments(source, reader): splits a stream into documents exactly asYamlStreamdoes, recording the line each document starts on.WithStdin(reader): sets the reader-reads, defaulting toos.Stdin, so callers and tests can supply stdin without replacingos.Stdin.Existing loaders (
NewLoader,NewCompositeLoader,NewExactCompositeLoader,YamlStream) and all their callers are unchanged.Tests.
SplitDocumentsis checked againstYamlStreamon every case, so the two always split a stream into the same documents. Start lines are checked for:The loader is also tested on comma-separated lists, recursive directories and a missing source, and
WithStdinwith-alone and in a list.For reviewers.
SplitDocumentsreimplements the separator rules ofk8s.io/apimachinery'sYAMLReader, because that reader does not report line numbers. The equivalence check in the tests is what keeps the two in step.Refs #481
I have:
./nix.sh flake checkto ensure this PR is ready for review.Linked a PR or a docs tracking issue to document this change.Not applicable: this adds a library API inpkg/loadand changes no command or user-facing behaviour.AddedNot applicable: this is a new feature, not a fix.backport release-x.ylabels to auto-backport this PR.Need help with this checklist? See the cheat sheet.