Skip to content

Commit f4a68da

Browse files
authored
Merge pull request #5752 from thaJeztah/bump_yamlv3
switch to gopkg.in/yaml.v3
2 parents ce293bd + 58bf0f1 commit f4a68da

28 files changed

Lines changed: 2760 additions & 1299 deletions

cli/command/stack/config.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package stack
22

33
import (
4+
"bytes"
45
"fmt"
56

67
"github.com/docker/cli/cli"
@@ -11,7 +12,7 @@ import (
1112
composeLoader "github.com/docker/cli/cli/compose/loader"
1213
composetypes "github.com/docker/cli/cli/compose/types"
1314
"github.com/spf13/cobra"
14-
yaml "gopkg.in/yaml.v2"
15+
"gopkg.in/yaml.v3"
1516
)
1617

1718
func newConfigCommand(dockerCli command.Cli) *cobra.Command {
@@ -54,9 +55,12 @@ func outputConfig(configFiles composetypes.ConfigDetails, skipInterpolation bool
5455
return "", err
5556
}
5657

57-
d, err := yaml.Marshal(&config)
58+
var buf bytes.Buffer
59+
enc := yaml.NewEncoder(&buf)
60+
enc.SetIndent(2)
61+
err = enc.Encode(&config)
5862
if err != nil {
5963
return "", err
6064
}
61-
return string(d), nil
65+
return buf.String(), nil
6266
}

cli/command/stack/config_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ services:
4545
services:
4646
foo:
4747
command:
48-
- cat
49-
- file2.txt
48+
- cat
49+
- file2.txt
5050
image: busybox:1.0
5151
`,
5252
},
@@ -69,8 +69,8 @@ services:
6969
services:
7070
foo:
7171
command:
72-
- cat
73-
- file2.txt
72+
- cat
73+
- file2.txt
7474
image: busybox:${VERSION}
7575
`,
7676
},

cli/compose/loader/loader.go

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"github.com/google/shlex"
2626
"github.com/pkg/errors"
2727
"github.com/sirupsen/logrus"
28-
yaml "gopkg.in/yaml.v2"
28+
"gopkg.in/yaml.v3"
2929
)
3030

3131
// Options supported by Load
@@ -53,11 +53,11 @@ func ParseYAML(source []byte) (map[string]any, error) {
5353
if err := yaml.Unmarshal(source, &cfg); err != nil {
5454
return nil, err
5555
}
56-
cfgMap, ok := cfg.(map[any]any)
56+
_, ok := cfg.(map[string]any)
5757
if !ok {
5858
return nil, errors.Errorf("top-level object must be a mapping")
5959
}
60-
converted, err := convertToStringKeysRecursive(cfgMap, "")
60+
converted, err := convertToStringKeysRecursive(cfg, "")
6161
if err != nil {
6262
return nil, err
6363
}
@@ -349,24 +349,20 @@ func createTransformHook(additionalTransformers ...Transformer) mapstructure.Dec
349349

350350
// keys needs to be converted to strings for jsonschema
351351
func convertToStringKeysRecursive(value any, keyPrefix string) (any, error) {
352-
if mapping, ok := value.(map[any]any); ok {
352+
if mapping, ok := value.(map[string]any); ok {
353353
dict := make(map[string]any)
354354
for key, entry := range mapping {
355-
str, ok := key.(string)
356-
if !ok {
357-
return nil, formatInvalidKeyError(keyPrefix, key)
358-
}
359355
var newKeyPrefix string
360356
if keyPrefix == "" {
361-
newKeyPrefix = str
357+
newKeyPrefix = key
362358
} else {
363-
newKeyPrefix = fmt.Sprintf("%s.%s", keyPrefix, str)
359+
newKeyPrefix = fmt.Sprintf("%s.%s", keyPrefix, key)
364360
}
365361
convertedEntry, err := convertToStringKeysRecursive(entry, newKeyPrefix)
366362
if err != nil {
367363
return nil, err
368364
}
369-
dict[str] = convertedEntry
365+
dict[key] = convertedEntry
370366
}
371367
return dict, nil
372368
}
@@ -385,16 +381,6 @@ func convertToStringKeysRecursive(value any, keyPrefix string) (any, error) {
385381
return value, nil
386382
}
387383

388-
func formatInvalidKeyError(keyPrefix string, key any) error {
389-
var location string
390-
if keyPrefix == "" {
391-
location = "at top level"
392-
} else {
393-
location = "in " + keyPrefix
394-
}
395-
return errors.Errorf("non-string key %s: %#v", location, key)
396-
}
397-
398384
// LoadServices produces a ServiceConfig map from a compose file Dict
399385
// the servicesDict is not validated if directly used. Use Load() to enable validation
400386
func LoadServices(servicesDict map[string]any, workingDir string, lookupEnv template.Mapping) ([]types.ServiceConfig, error) {

cli/compose/loader/loader_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ func TestInvalidTopLevelObjectType(t *testing.T) {
332332
}
333333

334334
func TestNonStringKeys(t *testing.T) {
335+
// FIXME(thaJeztah): opkg.in/yaml.v3, which always unmarshals to a map[string]any, so we cannot produce a customized error for invalid types.
336+
t.Skip("not supported by gopkg.in/yaml.v3, which always unmarshals to a map[string]any")
335337
_, err := loadYAML(`
336338
version: "3"
337339
123:

0 commit comments

Comments
 (0)