yaml: fix surgical Patch() for the empty-streams config round-trip - #2355
Open
firepinn wants to merge 2 commits into
Open
yaml: fix surgical Patch() for the empty-streams config round-trip#2355firepinn wants to merge 2 commits into
firepinn wants to merge 2 commits into
Conversation
addToEnd returned a fresh unnamed error value when asked to delete a path whose parent does not exist, so callers could not distinguish it from a real failure. Export it as the sentinel ErrPathNotExist and have PatchConfig treat a delete of a non-existent path (value == nil) as a no-op via errors.Is, instead of surfacing an error. Add a yaml test asserting that deleting a path with an absent parent returns ErrPathNotExist.
yaml.Marshal of an empty map emits the explicit token "key: null". The
surgical Patch() then cannot add the first child back: it pastes the
child mapping after the null scalar, yielding "key: null\n child: ...",
which fails to parse ("mapping values are not allowed in this context").
Strip the explicit null token so the key becomes a bare mapping parent
before appending the child. A bare/implicit-null value ("key:") carries
no token and keeps the previous behaviour.
Add tests covering an explicit-null parent both alongside a sibling key
and on its own.
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.
Problem
go2rtc edits its YAML config in place with the surgical
yaml.Patch()(used by thePATCH /api/configandstreamsAPI paths) rather than re-marshaling the whole document. Two edge cases in that patcher break the common "delete the last stream, then add one back" round-trip:Deleting a path whose parent doesn't exist returns an error.
addToEndreturns a fresherrors.New("yaml: path not exist"), whichPatchConfigsurfaces as a failure. A caller that deletes a possibly-absent key (e.g. removing a temporary stream) can't distinguish this from a real error.Adding a child under an explicit
nullscalar fails to parse.yaml.Marshalof an emptied map emits the explicit tokenkey: null.Patch()then pastes the new child mapping after that scalar, producingkey: null\n child: ..., which is invalid YAML (mapping values are not allowed in this context). The patch is rejected (the API returns HTTP 400) and the key can't get its first child back without resetting the file.Together these mean: delete every stream → the
streamsmap serializes tostreams: null→ adding a stream back fails.Fix
yaml.ErrPathNotExistand havePatchConfigtreat a delete (value == nil) of a non-existent path as a no-op viaerrors.Is, instead of returning an error.key:) is unaffected.Tests
Added unit tests in
pkg/yaml/yaml_test.go:ErrPathNotExist;key: null, both alongside a sibling key and on its own.go test ./pkg/yaml/passes.