add //go:build directives to prevent downgrading to go1.16 language
This is a follow-up to 0e73168b7e6d1d029d76d05b843b1aaec46739a8
This repository is not yet a module (i.e., does not have a `go.mod`). This
is not problematic when building the code in GOPATH or "vendor" mode, but
when using the code as a module-dependency (in module-mode), different semantics
are applied since Go1.21, which switches Go _language versions_ on a per-module,
per-package, or even per-file base.
A condensed summary of that logic [is as follows][1]:
- For modules that have a go.mod containing a go version directive; that
version is considered a minimum _required_ version (starting with the
go1.19.13 and go1.20.8 patch releases: before those, it was only a
recommendation).
- For dependencies that don't have a go.mod (not a module), go language
version go1.16 is assumed.
- Likewise, for modules that have a go.mod, but the file does not have a
go version directive, go language version go1.16 is assumed.
- If a go.work file is present, but does not have a go version directive,
language version go1.17 is assumed.
When switching language versions, Go _downgrades_ the language version,
which means that language features (such as generics, and `any`) are not
available, and compilation fails. For example:
# github.com/docker/cli/cli/context/store
/go/pkg/mod/github.com/docker/cli@v25.0.0-beta.2+incompatible/cli/context/store/storeconfig.go:6:24: predeclared any requires go1.18 or later (-lang was set to go1.16; check go.mod)
/go/pkg/mod/github.com/docker/cli@v25.0.0-beta.2+incompatible/cli/context/store/store.go:74:12: predeclared any requires go1.18 or later (-lang was set to go1.16; check go.mod)
Note that these fallbacks are per-module, per-package, and can even be
per-file, so _(indirect) dependencies_ can still use modern language
features, as long as their respective go.mod has a version specified.
Unfortunately, these failures do not occur when building locally (using
vendor / GOPATH mode), but will affect consumers of the module.
Obviously, this situation is not ideal, and the ultimate solution is to
move to go modules (add a go.mod), but this comes with a non-insignificant
risk in other areas (due to our complex dependency tree).
We can revert to using go1.16 language features only, but this may be
limiting, and may still be problematic when (e.g.) matching signatures
of dependencies.
There is an escape hatch: adding a `//go:build` directive to files that
make use of go language features. From the [go toolchain docs][2]:
> The go line for each module sets the language version the compiler enforces
> when compiling packages in that module. The language version can be changed
> on a per-file basis by using a build constraint.
>
> For example, a module containing code that uses the Go 1.21 language version
> should have a `go.mod` file with a go line such as `go 1.21` or `go 1.21.3`.
> If a specific source file should be compiled only when using a newer Go
> toolchain, adding `//go:build go1.22` to that source file both ensures that
> only Go 1.22 and newer toolchains will compile the file and also changes
> the language version in that file to Go 1.22.
This patch adds `//go:build` directives to those files using recent additions
to the language. It's currently using go1.19 as version to match the version
in our "vendor.mod", but we can consider being more permissive ("any" requires
go1.18 or up), or more "optimistic" (force go1.21, which is the version we
currently use to build).
For completeness sake, note that any file _without_ a `//go:build` directive
will continue to use go1.16 language version when used as a module.
[1]: https://github.com/golang/go/blob/58c28ba286dd0e98fe4cca80f5d64bbcb824a685/src/cmd/go/internal/gover/version.go#L9-L56
[2]; https://go.dev/doc/toolchain#:~:text=The%20go%20line%20for,file%20to%20Go%201.22
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 70216b662dc440faaebab531deb35f0f0c633d17)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-12-14 07:51:57 -05:00
|
|
|
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
|
|
|
|
//go:build go1.19
|
|
|
|
|
2017-09-29 08:21:40 -04:00
|
|
|
package loader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/compose/types"
|
|
|
|
"github.com/imdario/mergo"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type specials struct {
|
|
|
|
m map[reflect.Type]func(dst, src reflect.Value) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *specials) Transformer(t reflect.Type) func(dst, src reflect.Value) error {
|
|
|
|
if fn, ok := s.m[t]; ok {
|
|
|
|
return fn
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func merge(configs []*types.Config) (*types.Config, error) {
|
|
|
|
base := configs[0]
|
|
|
|
for _, override := range configs[1:] {
|
|
|
|
var err error
|
|
|
|
base.Services, err = mergeServices(base.Services, override.Services)
|
|
|
|
if err != nil {
|
|
|
|
return base, errors.Wrapf(err, "cannot merge services from %s", override.Filename)
|
|
|
|
}
|
|
|
|
base.Volumes, err = mergeVolumes(base.Volumes, override.Volumes)
|
|
|
|
if err != nil {
|
|
|
|
return base, errors.Wrapf(err, "cannot merge volumes from %s", override.Filename)
|
|
|
|
}
|
|
|
|
base.Networks, err = mergeNetworks(base.Networks, override.Networks)
|
|
|
|
if err != nil {
|
|
|
|
return base, errors.Wrapf(err, "cannot merge networks from %s", override.Filename)
|
|
|
|
}
|
|
|
|
base.Secrets, err = mergeSecrets(base.Secrets, override.Secrets)
|
|
|
|
if err != nil {
|
|
|
|
return base, errors.Wrapf(err, "cannot merge secrets from %s", override.Filename)
|
|
|
|
}
|
|
|
|
base.Configs, err = mergeConfigs(base.Configs, override.Configs)
|
|
|
|
if err != nil {
|
|
|
|
return base, errors.Wrapf(err, "cannot merge configs from %s", override.Filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return base, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeServices(base, override []types.ServiceConfig) ([]types.ServiceConfig, error) {
|
|
|
|
baseServices := mapByName(base)
|
|
|
|
overrideServices := mapByName(override)
|
|
|
|
specials := &specials{
|
|
|
|
m: map[reflect.Type]func(dst, src reflect.Value) error{
|
|
|
|
reflect.TypeOf(&types.LoggingConfig{}): safelyMerge(mergeLoggingConfig),
|
|
|
|
reflect.TypeOf([]types.ServicePortConfig{}): mergeSlice(toServicePortConfigsMap, toServicePortConfigsSlice),
|
|
|
|
reflect.TypeOf([]types.ServiceSecretConfig{}): mergeSlice(toServiceSecretConfigsMap, toServiceSecretConfigsSlice),
|
|
|
|
reflect.TypeOf([]types.ServiceConfigObjConfig{}): mergeSlice(toServiceConfigObjConfigsMap, toSServiceConfigObjConfigsSlice),
|
2020-01-17 08:12:53 -05:00
|
|
|
reflect.TypeOf(&types.UlimitsConfig{}): mergeUlimitsConfig,
|
2020-09-23 21:37:13 -04:00
|
|
|
reflect.TypeOf([]types.ServiceVolumeConfig{}): mergeSlice(toServiceVolumeConfigsMap, toServiceVolumeConfigsSlice),
|
2020-09-22 20:33:30 -04:00
|
|
|
reflect.TypeOf(types.ShellCommand{}): mergeShellCommand,
|
2020-01-17 08:12:53 -05:00
|
|
|
reflect.TypeOf(&types.ServiceNetworkConfig{}): mergeServiceNetworkConfig,
|
2022-10-12 14:16:51 -04:00
|
|
|
reflect.PointerTo(reflect.TypeOf(uint64(1))): mergeUint64,
|
2017-09-29 08:21:40 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, overrideService := range overrideServices {
|
2019-10-29 08:37:58 -04:00
|
|
|
overrideService := overrideService
|
2017-09-29 08:21:40 -04:00
|
|
|
if baseService, ok := baseServices[name]; ok {
|
2018-04-05 04:37:35 -04:00
|
|
|
if err := mergo.Merge(&baseService, &overrideService, mergo.WithAppendSlice, mergo.WithOverride, mergo.WithTransformers(specials)); err != nil {
|
2017-09-29 08:21:40 -04:00
|
|
|
return base, errors.Wrapf(err, "cannot merge service %s", name)
|
|
|
|
}
|
|
|
|
baseServices[name] = baseService
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
baseServices[name] = overrideService
|
|
|
|
}
|
|
|
|
services := []types.ServiceConfig{}
|
|
|
|
for _, baseService := range baseServices {
|
|
|
|
services = append(services, baseService)
|
|
|
|
}
|
|
|
|
sort.Slice(services, func(i, j int) bool { return services[i].Name < services[j].Name })
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toServiceSecretConfigsMap(s interface{}) (map[interface{}]interface{}, error) {
|
|
|
|
secrets, ok := s.([]types.ServiceSecretConfig)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("not a serviceSecretConfig: %v", s)
|
|
|
|
}
|
|
|
|
m := map[interface{}]interface{}{}
|
|
|
|
for _, secret := range secrets {
|
|
|
|
m[secret.Source] = secret
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toServiceConfigObjConfigsMap(s interface{}) (map[interface{}]interface{}, error) {
|
|
|
|
secrets, ok := s.([]types.ServiceConfigObjConfig)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("not a serviceSecretConfig: %v", s)
|
|
|
|
}
|
|
|
|
m := map[interface{}]interface{}{}
|
|
|
|
for _, secret := range secrets {
|
|
|
|
m[secret.Source] = secret
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toServicePortConfigsMap(s interface{}) (map[interface{}]interface{}, error) {
|
|
|
|
ports, ok := s.([]types.ServicePortConfig)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("not a servicePortConfig slice: %v", s)
|
|
|
|
}
|
|
|
|
m := map[interface{}]interface{}{}
|
|
|
|
for _, p := range ports {
|
|
|
|
m[p.Published] = p
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2020-09-23 21:37:13 -04:00
|
|
|
func toServiceVolumeConfigsMap(s interface{}) (map[interface{}]interface{}, error) {
|
|
|
|
volumes, ok := s.([]types.ServiceVolumeConfig)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("not a serviceVolumeConfig slice: %v", s)
|
|
|
|
}
|
|
|
|
m := map[interface{}]interface{}{}
|
|
|
|
for _, v := range volumes {
|
|
|
|
m[v.Target] = v
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2017-09-29 08:21:40 -04:00
|
|
|
func toServiceSecretConfigsSlice(dst reflect.Value, m map[interface{}]interface{}) error {
|
|
|
|
s := []types.ServiceSecretConfig{}
|
|
|
|
for _, v := range m {
|
|
|
|
s = append(s, v.(types.ServiceSecretConfig))
|
|
|
|
}
|
|
|
|
sort.Slice(s, func(i, j int) bool { return s[i].Source < s[j].Source })
|
|
|
|
dst.Set(reflect.ValueOf(s))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toSServiceConfigObjConfigsSlice(dst reflect.Value, m map[interface{}]interface{}) error {
|
|
|
|
s := []types.ServiceConfigObjConfig{}
|
|
|
|
for _, v := range m {
|
|
|
|
s = append(s, v.(types.ServiceConfigObjConfig))
|
|
|
|
}
|
|
|
|
sort.Slice(s, func(i, j int) bool { return s[i].Source < s[j].Source })
|
|
|
|
dst.Set(reflect.ValueOf(s))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toServicePortConfigsSlice(dst reflect.Value, m map[interface{}]interface{}) error {
|
|
|
|
s := []types.ServicePortConfig{}
|
|
|
|
for _, v := range m {
|
|
|
|
s = append(s, v.(types.ServicePortConfig))
|
|
|
|
}
|
|
|
|
sort.Slice(s, func(i, j int) bool { return s[i].Published < s[j].Published })
|
|
|
|
dst.Set(reflect.ValueOf(s))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-23 21:37:13 -04:00
|
|
|
func toServiceVolumeConfigsSlice(dst reflect.Value, m map[interface{}]interface{}) error {
|
|
|
|
s := []types.ServiceVolumeConfig{}
|
|
|
|
for _, v := range m {
|
|
|
|
s = append(s, v.(types.ServiceVolumeConfig))
|
|
|
|
}
|
|
|
|
sort.Slice(s, func(i, j int) bool { return s[i].Target < s[j].Target })
|
|
|
|
dst.Set(reflect.ValueOf(s))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-29 11:21:51 -04:00
|
|
|
type (
|
|
|
|
tomapFn func(s interface{}) (map[interface{}]interface{}, error)
|
|
|
|
writeValueFromMapFn func(reflect.Value, map[interface{}]interface{}) error
|
|
|
|
)
|
2017-09-29 08:21:40 -04:00
|
|
|
|
|
|
|
func safelyMerge(mergeFn func(dst, src reflect.Value) error) func(dst, src reflect.Value) error {
|
|
|
|
return func(dst, src reflect.Value) error {
|
|
|
|
if src.IsNil() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if dst.IsNil() {
|
|
|
|
dst.Set(src)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return mergeFn(dst, src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeSlice(tomap tomapFn, writeValue writeValueFromMapFn) func(dst, src reflect.Value) error {
|
|
|
|
return func(dst, src reflect.Value) error {
|
|
|
|
dstMap, err := sliceToMap(tomap, dst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
srcMap, err := sliceToMap(tomap, src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := mergo.Map(&dstMap, srcMap, mergo.WithOverride); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return writeValue(dst, dstMap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sliceToMap(tomap tomapFn, v reflect.Value) (map[interface{}]interface{}, error) {
|
|
|
|
// check if valid
|
|
|
|
if !v.IsValid() {
|
|
|
|
return nil, errors.Errorf("invalid value : %+v", v)
|
|
|
|
}
|
|
|
|
return tomap(v.Interface())
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeLoggingConfig(dst, src reflect.Value) error {
|
|
|
|
// Same driver, merging options
|
|
|
|
if getLoggingDriver(dst.Elem()) == getLoggingDriver(src.Elem()) ||
|
|
|
|
getLoggingDriver(dst.Elem()) == "" || getLoggingDriver(src.Elem()) == "" {
|
|
|
|
if getLoggingDriver(dst.Elem()) == "" {
|
|
|
|
dst.Elem().FieldByName("Driver").SetString(getLoggingDriver(src.Elem()))
|
|
|
|
}
|
|
|
|
dstOptions := dst.Elem().FieldByName("Options").Interface().(map[string]string)
|
|
|
|
srcOptions := src.Elem().FieldByName("Options").Interface().(map[string]string)
|
|
|
|
return mergo.Merge(&dstOptions, srcOptions, mergo.WithOverride)
|
|
|
|
}
|
|
|
|
// Different driver, override with src
|
|
|
|
dst.Set(src)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:29:49 -04:00
|
|
|
//nolint:unparam
|
2020-01-17 08:12:53 -05:00
|
|
|
func mergeUlimitsConfig(dst, src reflect.Value) error {
|
|
|
|
if src.Interface() != reflect.Zero(reflect.TypeOf(src.Interface())).Interface() {
|
|
|
|
dst.Elem().Set(src.Elem())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:29:49 -04:00
|
|
|
//nolint:unparam
|
2020-09-22 20:33:30 -04:00
|
|
|
func mergeShellCommand(dst, src reflect.Value) error {
|
|
|
|
if src.Len() != 0 {
|
|
|
|
dst.Set(src)
|
2020-09-23 21:37:13 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:29:49 -04:00
|
|
|
//nolint:unparam
|
2020-01-17 08:12:53 -05:00
|
|
|
func mergeServiceNetworkConfig(dst, src reflect.Value) error {
|
|
|
|
if src.Interface() != reflect.Zero(reflect.TypeOf(src.Interface())).Interface() {
|
|
|
|
dst.Elem().FieldByName("Aliases").Set(src.Elem().FieldByName("Aliases"))
|
|
|
|
if ipv4 := src.Elem().FieldByName("Ipv4Address").Interface().(string); ipv4 != "" {
|
|
|
|
dst.Elem().FieldByName("Ipv4Address").SetString(ipv4)
|
|
|
|
}
|
|
|
|
if ipv6 := src.Elem().FieldByName("Ipv6Address").Interface().(string); ipv6 != "" {
|
|
|
|
dst.Elem().FieldByName("Ipv6Address").SetString(ipv6)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-12 14:16:51 -04:00
|
|
|
//nolint:unparam
|
|
|
|
func mergeUint64(dst, src reflect.Value) error {
|
|
|
|
if !src.IsNil() {
|
|
|
|
dst.Elem().Set(src.Elem())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-29 08:21:40 -04:00
|
|
|
func getLoggingDriver(v reflect.Value) string {
|
|
|
|
return v.FieldByName("Driver").String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapByName(services []types.ServiceConfig) map[string]types.ServiceConfig {
|
|
|
|
m := map[string]types.ServiceConfig{}
|
|
|
|
for _, service := range services {
|
|
|
|
m[service.Name] = service
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeVolumes(base, override map[string]types.VolumeConfig) (map[string]types.VolumeConfig, error) {
|
2018-04-05 04:37:35 -04:00
|
|
|
err := mergo.Map(&base, &override, mergo.WithOverride)
|
2017-09-29 08:21:40 -04:00
|
|
|
return base, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeNetworks(base, override map[string]types.NetworkConfig) (map[string]types.NetworkConfig, error) {
|
2018-04-05 04:37:35 -04:00
|
|
|
err := mergo.Map(&base, &override, mergo.WithOverride)
|
2017-09-29 08:21:40 -04:00
|
|
|
return base, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeSecrets(base, override map[string]types.SecretConfig) (map[string]types.SecretConfig, error) {
|
2018-04-05 04:37:35 -04:00
|
|
|
err := mergo.Map(&base, &override, mergo.WithOverride)
|
2017-09-29 08:21:40 -04:00
|
|
|
return base, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeConfigs(base, override map[string]types.ConfigObjConfig) (map[string]types.ConfigObjConfig, error) {
|
2018-04-05 04:37:35 -04:00
|
|
|
err := mergo.Map(&base, &override, mergo.WithOverride)
|
2017-09-29 08:21:40 -04:00
|
|
|
return base, err
|
|
|
|
}
|