Skip to content

switch to gopkg.in/yaml.v3#5752

Merged
thaJeztah merged 1 commit intodocker:masterfrom
thaJeztah:bump_yamlv3
Jan 15, 2025
Merged

switch to gopkg.in/yaml.v3#5752
thaJeztah merged 1 commit intodocker:masterfrom
thaJeztah:bump_yamlv3

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

This was attempted some time ago in #5629, but I don't recall what the issue was causing problems, so let's try and see what CI says

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

@thaJeztah thaJeztah force-pushed the bump_yamlv3 branch 2 times, most recently from 047a479 to 6e68e64 Compare January 14, 2025 22:41
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Jan 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 59.47%. Comparing base (ce293bd) to head (58bf0f1).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5752      +/-   ##
==========================================
- Coverage   59.51%   59.47%   -0.05%     
==========================================
  Files         346      346              
  Lines       29376    29367       -9     
==========================================
- Hits        17483    17465      -18     
- Misses      10923    10929       +6     
- Partials      970      973       +3     

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah thaJeztah added status/2-code-review area/stack kind/refactor PR's that refactor, or clean-up code labels Jan 14, 2025
@thaJeztah thaJeztah added this to the 28.0.0 milestone Jan 14, 2025
@thaJeztah thaJeztah self-assigned this Jan 14, 2025
@thaJeztah thaJeztah marked this pull request as ready for review January 14, 2025 23:00
@thaJeztah thaJeztah requested review from a team and silvin-lubecki as code owners January 14, 2025 23:00
@krissetto
Copy link
Copy Markdown
Contributor

overall LGTM..
it seems strange that many tests in the previous PR were failing with top-level object must be a mapping because the tests haven't changed and the compose file used at the time seem ok to me. Even the versions of the module are the same 🤷

return nil, err
}
cfgMap, ok := cfg.(map[any]any)
_, ok := cfg.(map[string]any)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

it seems strange that many tests in the previous PR were failing with top-level object must be a mapping because the tests haven't changed and the compose file used at the time seem ok to me. Even the versions of the module are the same

So this was the reason; YAML v2 returned a map[any]any whereas v3 always returns a map[string]any.

Honestly, I half-expected map[string]any to ALSO be a map[any]any (because string is also an any ? But that's not how Go asserts these.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

So actually, to be more correct; it returns a map[string]interface{} but Go is happy considering interface{} == any

Comment on lines 351 to -358
func convertToStringKeysRecursive(value any, keyPrefix string) (any, error) {
if mapping, ok := value.(map[any]any); ok {
if mapping, ok := value.(map[string]any); ok {
dict := make(map[string]any)
for key, entry := range mapping {
str, ok := key.(string)
if !ok {
return nil, formatInvalidKeyError(keyPrefix, key)
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

But while it makes sense too use map[string]any (i.e., YAML keys would be a string, always, even it it looks like an int or a weird string like {object}:, that also means that we can't produce custom errors for those ("you used an integer as key, did you mean something else?").

Comment on lines -388 to -396
func formatInvalidKeyError(keyPrefix string, key any) error {
var location string
if keyPrefix == "" {
location = "at top level"
} else {
location = "in " + keyPrefix
}
return errors.Errorf("non-string key %s: %#v", location, key)
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

So these errors could no longer work, unless we would reverse the direction ("string" -> "but is it an integer?")

Comment on lines 334 to 337
func TestNonStringKeys(t *testing.T) {
// FIXME(thaJeztah): opkg.in/yaml.v3, which always unmarshals to a map[string]any, so we cannot produce a customized error for invalid types.
t.Skip("not supported by gopkg.in/yaml.v3, which always unmarshals to a map[string]any")
_, err := loadYAML(`
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

So, I kept the test for our custom errors (this test runs with badly formed YAML files to test the custom errors).

But mostly as a "reminder" in case someone would like to bite their teeth into implementing something like that.

(but in all honesty, we can probably just remove the test and call it a day; just write proper YAML)

@thaJeztah thaJeztah merged commit f4a68da into docker:master Jan 15, 2025
@thaJeztah thaJeztah deleted the bump_yamlv3 branch January 15, 2025 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/stack kind/refactor PR's that refactor, or clean-up code status/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants