-
Notifications
You must be signed in to change notification settings - Fork 2.1k
switch to gopkg.in/yaml.v3 #5752
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,7 @@ import ( | |
| "github.com/google/shlex" | ||
| "github.com/pkg/errors" | ||
| "github.com/sirupsen/logrus" | ||
| yaml "gopkg.in/yaml.v2" | ||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| // Options supported by Load | ||
|
|
@@ -53,11 +53,11 @@ func ParseYAML(source []byte) (map[string]any, error) { | |
| if err := yaml.Unmarshal(source, &cfg); err != nil { | ||
| return nil, err | ||
| } | ||
| cfgMap, ok := cfg.(map[any]any) | ||
| _, ok := cfg.(map[string]any) | ||
| if !ok { | ||
| return nil, errors.Errorf("top-level object must be a mapping") | ||
| } | ||
| converted, err := convertToStringKeysRecursive(cfgMap, "") | ||
| converted, err := convertToStringKeysRecursive(cfg, "") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -349,24 +349,20 @@ func createTransformHook(additionalTransformers ...Transformer) mapstructure.Dec | |
|
|
||
| // keys needs to be converted to strings for jsonschema | ||
| 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) | ||
| } | ||
|
Comment on lines
351
to
-358
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But while it makes sense too use |
||
| var newKeyPrefix string | ||
| if keyPrefix == "" { | ||
| newKeyPrefix = str | ||
| newKeyPrefix = key | ||
| } else { | ||
| newKeyPrefix = fmt.Sprintf("%s.%s", keyPrefix, str) | ||
| newKeyPrefix = fmt.Sprintf("%s.%s", keyPrefix, key) | ||
| } | ||
| convertedEntry, err := convertToStringKeysRecursive(entry, newKeyPrefix) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| dict[str] = convertedEntry | ||
| dict[key] = convertedEntry | ||
| } | ||
| return dict, nil | ||
| } | ||
|
|
@@ -385,16 +381,6 @@ func convertToStringKeysRecursive(value any, keyPrefix string) (any, error) { | |
| return value, nil | ||
| } | ||
|
|
||
| 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) | ||
| } | ||
|
Comment on lines
-388
to
-396
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?") |
||
|
|
||
| // LoadServices produces a ServiceConfig map from a compose file Dict | ||
| // the servicesDict is not validated if directly used. Use Load() to enable validation | ||
| func LoadServices(servicesDict map[string]any, workingDir string, lookupEnv template.Mapping) ([]types.ServiceConfig, error) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -332,6 +332,8 @@ func TestInvalidTopLevelObjectType(t *testing.T) { | |
| } | ||
|
|
||
| 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(` | ||
|
Comment on lines
334
to
337
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
||
| version: "3" | ||
| 123: | ||
|
|
||
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.
So this was the reason; YAML v2 returned a
map[any]anywhereas v3 always returns amap[string]any.Honestly, I half-expected
map[string]anyto ALSO be amap[any]any(becausestringis also anany? But that's not how Go asserts these.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.
So actually, to be more correct; it returns a
map[string]interface{}but Go is happy consideringinterface{} == any