-
Notifications
You must be signed in to change notification settings - Fork 52
Fix: option KnownFields not being respected inside custom UnmarshalYAML() #332
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
Open
stoewer
wants to merge
10
commits into
yaml:main
Choose a base branch
from
stoewer:fix-known-fields
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a5929fa
Add benchmark to test different unmarshal options
stoewer 72c090a
Node.Decode() inherits options from parent decoder
stoewer 4f9c4d7
Set loader option in SetKnownFields()
stoewer 7ac2901
Remove optimization with filterLoadOptions()
stoewer 3f7089a
Add more unit test to verify option propatation
stoewer 060dc15
Inherit options in Loader.ComposeAndResolve()
stoewer a0d0efc
Call handleErr(&err) first in Node.Decode()
stoewer 947003e
Improve docs for Node.IsZero()
stoewer e2ed319
Improve docs for Node.Load()
stoewer e4db7de
Fix linter issues by running 'make fmt'
stoewer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Copyright 2025 The go-yaml Project Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package yaml_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| yaml "go.yaml.in/yaml/v4" | ||
| ) | ||
|
|
||
| // benchPlain is a decode target with no custom UnmarshalYAML. | ||
| type benchPlain struct { | ||
| Fields map[string]string `yaml:",inline"` | ||
| } | ||
|
|
||
| // benchCustom is a decode target whose UnmarshalYAML calls node.Decode — | ||
| // the path that will inherit loader options after the tree-stamp fix. | ||
| type benchCustom struct { | ||
| Fields map[string]string `yaml:",inline"` | ||
| } | ||
|
|
||
| func (b *benchCustom) UnmarshalYAML(node *yaml.Node) error { | ||
| type plain benchCustom | ||
| return node.Decode((*plain)(b)) | ||
| } | ||
|
|
||
| func makeKVDoc(n int) []byte { | ||
| var sb strings.Builder | ||
| for i := 0; i < n; i++ { | ||
| fmt.Fprintf(&sb, "key%d: value%d\n", i, i) | ||
| } | ||
| return []byte(sb.String()) | ||
| } | ||
|
|
||
| var ( | ||
| benchSmallDoc = makeKVDoc(10) | ||
| benchMediumDoc = makeKVDoc(100) | ||
| benchLargeDoc = makeKVDoc(1000) | ||
| ) | ||
|
|
||
| func BenchmarkDecode(b *testing.B) { | ||
| targets := []struct { | ||
| name string | ||
| decode func(data []byte, known bool) error | ||
| }{ | ||
| { | ||
| name: "plain", | ||
| decode: func(data []byte, known bool) error { | ||
| var v benchPlain | ||
| dec := yaml.NewDecoder(bytes.NewReader(data)) | ||
| dec.KnownFields(known) | ||
| return dec.Decode(&v) | ||
| }, | ||
| }, | ||
| { | ||
| name: "custom", | ||
| decode: func(data []byte, known bool) error { | ||
| var v benchCustom | ||
| dec := yaml.NewDecoder(bytes.NewReader(data)) | ||
| dec.KnownFields(known) | ||
| return dec.Decode(&v) | ||
| }, | ||
| }, | ||
| } | ||
|
stoewer marked this conversation as resolved.
|
||
|
|
||
| options := []struct { | ||
| name string | ||
| knownFields bool | ||
| }{ | ||
| {"default", false}, | ||
| {"known-fields", true}, | ||
| } | ||
|
|
||
| sizes := []struct { | ||
| name string | ||
| data []byte | ||
| }{ | ||
| {"small", benchSmallDoc}, | ||
| {"medium", benchMediumDoc}, | ||
| {"large", benchLargeDoc}, | ||
| } | ||
|
|
||
| for _, size := range sizes { | ||
| for _, target := range targets { | ||
| for _, opt := range options { | ||
| size, target, opt := size, target, opt | ||
| name := fmt.Sprintf("target=%s/option=%s/size=%s", target.name, opt.name, size.name) | ||
| b.Run(name, func(b *testing.B) { | ||
| b.ReportAllocs() | ||
| for i := 0; i < b.N; i++ { | ||
| if err := target.decode(size.data, opt.knownFields); err != nil { | ||
| b.Fatal(err) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding the unexported
options *Optionsfield toNodechanges the publicyaml.Node(it’s a type alias). This can break downstream code that (a) uses unkeyed composite literals foryaml.Node{...}and (b) relies onreflect.DeepEqual/cmp equality against expected node literals (the new hidden pointer will differ). If this compatibility impact is acceptable, it should be explicitly called out (e.g., in release notes); otherwise consider an approach that doesn’t add a field to the public struct (e.g., side-table keyed by *Node, or storing options only on decoder/constructor paths).