From a7f2c5752e92e8269f2e25c8933afa4bfef046fe Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Thu, 28 Sep 2023 16:00:27 +0200 Subject: [PATCH] decode Healthcheck.Test using DecodeMapstructure Signed-off-by: Nicolas De Loof --- loader/loader.go | 12 ---------- types/healthcheck.go | 53 ++++++++++++++++++++++++++++++++++++++++++++ types/types.go | 16 ------------- 3 files changed, 53 insertions(+), 28 deletions(-) create mode 100644 types/healthcheck.go diff --git a/loader/loader.go b/loader/loader.go index 744519bb2..6b4734fe3 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -607,7 +607,6 @@ type Transformer struct { func createTransformHook(additionalTransformers ...Transformer) mapstructure.DecodeHookFuncType { transforms := map[reflect.Type]func(interface{}) (interface{}, error){ reflect.TypeOf(types.External{}): transformExternal, - reflect.TypeOf(types.HealthCheckTest{}): transformHealthCheckTest, reflect.TypeOf(map[string]string{}): transformMapStringString, reflect.TypeOf(types.UlimitsConfig{}): transformUlimits, reflect.TypeOf([]types.ServicePortConfig{}): transformServicePort, @@ -1253,17 +1252,6 @@ func transformValueToMapEntry(value string, separator string, allowNil bool) (st } } -var transformHealthCheckTest TransformerFunc = func(data interface{}) (interface{}, error) { - switch value := data.(type) { - case string: - return append([]string{"CMD-SHELL"}, value), nil - case []interface{}: - return value, nil - default: - return value, errors.Errorf("invalid type %T for healthcheck.test", value) - } -} - func toMapStringString(value map[string]interface{}, allowNil bool) map[string]interface{} { output := make(map[string]interface{}) for key, value := range value { diff --git a/types/healthcheck.go b/types/healthcheck.go new file mode 100644 index 000000000..1bbf5e9e2 --- /dev/null +++ b/types/healthcheck.go @@ -0,0 +1,53 @@ +/* + Copyright 2020 The Compose Specification Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package types + +import ( + "fmt" +) + +// HealthCheckConfig the healthcheck configuration for a service +type HealthCheckConfig struct { + Test HealthCheckTest `yaml:"test,omitempty" json:"test,omitempty"` + Timeout *Duration `yaml:"timeout,omitempty" json:"timeout,omitempty"` + Interval *Duration `yaml:"interval,omitempty" json:"interval,omitempty"` + Retries *uint64 `yaml:"retries,omitempty" json:"retries,omitempty"` + StartPeriod *Duration `yaml:"start_period,omitempty" json:"start_period,omitempty"` + StartInterval *Duration `yaml:"start_interval,omitempty" json:"start_interval,omitempty"` + Disable bool `yaml:"disable,omitempty" json:"disable,omitempty"` + + Extensions Extensions `yaml:"#extensions,inline" json:"-"` +} + +// HealthCheckTest is the command run to test the health of a service +type HealthCheckTest []string + +func (l *HealthCheckTest) DecodeMapstructure(value interface{}) error { + switch v := value.(type) { + case string: + *l = []string{"CMD-SHELL", v} + case []interface{}: + seq := make([]string, len(v)) + for i, e := range v { + seq[i] = e.(string) + } + *l = seq + default: + return fmt.Errorf("unexpected value type %T for healthcheck.test", value) + } + return nil +} diff --git a/types/types.go b/types/types.go index 0fda739fa..cad2dc0ee 100644 --- a/types/types.go +++ b/types/types.go @@ -526,22 +526,6 @@ type DeployConfig struct { Extensions Extensions `yaml:"#extensions,inline" json:"-"` } -// HealthCheckConfig the healthcheck configuration for a service -type HealthCheckConfig struct { - Test HealthCheckTest `yaml:"test,omitempty" json:"test,omitempty"` - Timeout *Duration `yaml:"timeout,omitempty" json:"timeout,omitempty"` - Interval *Duration `yaml:"interval,omitempty" json:"interval,omitempty"` - Retries *uint64 `yaml:"retries,omitempty" json:"retries,omitempty"` - StartPeriod *Duration `yaml:"start_period,omitempty" json:"start_period,omitempty"` - StartInterval *Duration `yaml:"start_interval,omitempty" json:"start_interval,omitempty"` - Disable bool `yaml:"disable,omitempty" json:"disable,omitempty"` - - Extensions Extensions `yaml:"#extensions,inline" json:"-"` -} - -// HealthCheckTest is the command run to test the health of a service -type HealthCheckTest []string - // UpdateConfig the service update configuration type UpdateConfig struct { Parallelism *uint64 `yaml:"parallelism,omitempty" json:"parallelism,omitempty"`